I adapted and it does work, but the bytes in the array are reversed. You can see how he uses k-- in order to display them as expected (unless I am missing something).
char array[] ="AABB"
Put through the code there and output into a byte array then iterated with a standard for loop;
for(int i = 0; i<3; i++) {
Serial.print(bytes[i], HEX);
}
Sure. Those arrays are fine though and are generated.
Some new info in my latest response, that almost gets me where I want to be.
Now that I think about it he is using i++ when generating the bytes. If I get the length, set i equal to that, and use i-- that might get me the correct order.
You just gave me an idea. I am really really close after hacking up that code to fit my needs. Let me try something a bit different. Well different from that code, but in line with what you are suggesting.
To answer your question, I am using an ESP32 to interface with a radio and the registers. I use serial input to trigger various commands to set radio registers. This piece is directly applicable to transmission.
I have 2 part commands; so for transmission Rfxmit(AABBCCDD). With the ascii in hex form between the () specifying what information to send. Already got the parsing of commands and parameters set up and working perfectly for all other commands/params in a very long sketch.
This almost works:
char temP[] = "AABB";
byte arr [4]; //the byte array will be oversized, maybe full maybe not which is why I
//left it oversized here.
int i = (strlen(temP) / 2); //The problem is I don't know how it will handle odd lengths.
//But first get it working then address that part
while (x != 0) {
arr[i] = x % 0x100;
x = x / 0x100;
i--;
}
The above code generates:
0
AA
BB
With
for (int i =0; i<3; i++) {
Serial.println(arr[i], HEX)
}
So it gets it there, but always with a 0 in arr[0].
Seems like a standard hex decode, aside from how to handle a half-byte. You'd be better off adapting the post just above that other one, and it's even simpler if you don't have any non-hex characters like spaces.
char temP[496];
byte arr[(sizeof(temP) + 1) / 2]; // +1 in case odd
const char *test[] = {
"AABBCCDDEE",
"ABBCCDDEE",
"210",
};
String space(" ");
void setup() {
Serial.begin(115200);
Serial.println(sizeof(temP));
Serial.println(sizeof(arr));
Serial.println(sizeof(test));
Serial.println(sizeof(char *));
}
size_t hexDecode() {
auto hex2num = [] (char c) {
switch (c) {
case '0' ... '9':
return c - '0';
case 'A' ... 'F':
return c - 'A' + 10;
case 'a' ... 'f':
return c - 'a' + 10;
default:
return 0;
}
};
int i = 0;
for (i = 0; i < sizeof(arr); i++) {
char left = temP[2 * i];
if (!left) { // NUL-terminator in source
break;
}
char right = temP[2 * i + 1];
arr[i] = 16 * hex2num(left) + hex2num(right);
if (!right) { // Only half a byte
i++; // Skip x0 just-assigned
break;
}
}
return i;
}
void hexPrint(size_t len) {
char buf[] = "12 ";
for (int i = 0; i < len; i++) {
sprintf(buf, "%02x ", arr[i]);
Serial.print(buf);
}
}
void loop() {
static unsigned t = 0;
if (t < sizeof(test) / sizeof(char *)) {
Serial.println(test[t] + space + strlen(test[t]));
strcpy(temP, test[t]);
size_t len = hexDecode();
hexPrint(len);
Serial.println(len);
delay(50);
} else {
delay(1000);
}
t++;
}
yields
AABBCCDDEE 10
aa bb cc dd ee 5
ABBCCDDEE 9
ab bc cd de e0 5
210 3
21 00 2
Are you sure that the zero should be add at the end? Usually the leading zero disappears, since 0x0A and 0xA the same, but 0xA and 0xA0 are not.
Most likely null should be added to the first element of the array:
Your last question regarding preceeding 0's got me thinking. The way it is currently working isn't very good.
The example of "ABC" could work out to any number of possibilities eg 0x0A, 0x0B, 0x0C, or 0x0A, 0xBC, or 0xAB, 0x0C.
Thank you for getting me thinking about that.
If I require the hex to be input in xx hex format eg 0ABC0D the char array will always have an even length, which should make things a bit easier to translate to the desired and correct output 0x0A 0xBC and 0x0D.
This post is definately going to come in handy Print hex with leading zeroes as my signal RX function does not produce proceeding 0's which is what started this post.
No thanks. I am not posting a thousand lines for a single function.
I didn't imagine I would have to explain that RF signals come in all different lengths, hence the temP[] array and byte array would be all different lengths up to the max size of each array.
That why I didn't post code at the start, because I cant use something that produces only 4 bytes when the input is much longer, and there is no "solution" that doesn't iterate. You just jumped on an example that happened to be only 4 char long and pretended it would always be 4 char long.
Feel free to "not waste your time".
@b707 thanks for your help, you definitely pointed out some issues I hadn't thought of and that once fixed should get me where I can finish it off.