Hello ,
I need to add 00 to each character in a message and then write them as a byte in HEX:
68 07 01 01 00 00 00 0A 09 00 00 65 31 00 32 00 33 00 10
31 00 32 00 33 00 = Text "123"
00 0A = The length of the total payload "09 00 00 65 31 00 32 00 33 00"
I am not familiar with C, how can i char msgArray[] = "123"; add the 00 after each char and calculate the length of the payload?
I have tried the serial.write and this works when sending each byte separately but i want a function to enter the the text msgArray .
Any help is appreciated.
philipprins:
msgArray
You could use a simple for loop and "strlen(msgArray)"
You could start with this:
char msgArray[30];
byte value = 123;
sprintf(msgArray,"%02x",value); //format in ASCII hex
31 00 32 00 33 00 = Text "123"
Not clear what you mean by the above. Those two representations are not the same.
char msgArray[] = "123" is the same as
char msgArray[] = {0x31,0x32,0x33,0x00}
Please give a better example of what you want to do. The original post is contradictory and confusing.
how can i char msgArray[] = "123"; add the 00 after each char
Maybe this will help
const char msgArray[] = "123";
char output[30]={0};
byte j=0; //output index
for (byte i=0; i<strlen(msgArray); i++) {
output[j++]=msgArray[i];
output[j++]=0;
}
byte output_len=j--; //length of output array
The above requirements can be made by the following codes:
char srcA[] = {'1', '2', '3'};
char desA[6];
void setup()
{
Serial.begin(9600);
for(int i = 0, j = 0; i<3; i++, j++) //building new array
{
desA[j++] = srcA[i];
desA[j] = '\0';
}
for(int i = 0; i<sizeof desA; i++) //displaying contents of the new array
{
byte y = desA[i];
if(y < 0x10)
{
Serial.print('0');
}
Serial.print(desA[i], HEX);
}
}
void loop() {}
Output:
310032003300
Thanks
Instead of serial.print how can I make an array that i can use with with serial.write as hex(byte)
Did you see post #4 , which does exactly what you asked?
Thanks, it has been a long time since i used Arduino and C,more than 10 years.
Will try it tomorrow , goodnight.
Given:
char srcA[] = {'1', '2', '3'};
What do you expect to see on Serial Monitor if the following code is executed:
Serial.write(srcA);
A somewhat cryptic method that relies upon the processor storing integers little-endian.
char srcA[] = "123";
void setup(){
Serial.begin(9600);
uint16_t dest[strlen(srcA)] = {0};
for (size_t i = 0; i < strlen(srcA); i++){
dest[i] = srcA[i];
}
Serial.println();
Serial.write((byte*)dest, sizeof(dest));
Serial.println();
byte* b = (byte*)dest;
for (size_t i = 0; i < sizeof(dest); i++){
if (*b < 16) Serial.print('0');
Serial.print(*b, HEX);
b++;
Serial.print(' ');
}
}
void loop(){
}
david_2018:
char srcA[] = "123";
Is the above declaration same as the following?
char srcA[] = {'1', '2', '3'};
GolamMostafa:
david_2018:
char srcA[] = "123";
Is the above declaration same as the following?
char srcA[] = {'1', '2', '3'};
Roughly equivalent, the first adds a terminating null, so you can use strlen() to get the size instead of sizeof() for the 2nd, but after copying to the destination array I was using the output has no difference.
1 Like
system
Closed
August 7, 2023, 11:55am
14
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.