Convert and include INT into chat array - solved

Dear All,

I'm struggling with converting 3 integer variables into char array in order to send it byte by byte.

I tried

int x0,x1,x2 ; 
char array [] ; 


 itoa(x0, array, 16);  
 itoa(x1, array+2, 16);
 itoa(x2, array+4, 16);   

but it is not working.

char array [] ; 

How many elements does this array have ?

It should be 6 bytes in total, two bytes for each integer.

Maybe it should have 6 bytes (maybe not), but how many bytes are actually allocated by your declaration ?

Why do you think that you need to use itoa() to convert the ints to ASCII in order to send the data somewhere ? Why not simply put the ints in an array and use Serial.write() to send the entire array at once ?

Because it has to be a char to send it as udp char array packet .

Do you understand what the itoa() function does ?

For instance, given this sketch, what values would be in the buffer array ?

void setup()
{
  char buffer[10];
  int a = 123;
  itoa(a, buffer, 10);
}

void loop()
{
}

it is missing setup and loop at minimum

Define "not working". Does it compile?

If you post your entire sketch we can help you much better.

If your sketch is huge, please consider producing an MRE. What's that?
Please read the first four posts in this category (the pinned ones!).

Helping with software questions benefits from a shared approach.
C

It will contain 1 2 3 in buffer[0] buffer[1] buffer[2], is that correct ?

Yes it does this is the code

char checksum;
unsigned char s0 = 0;

int32_t RINT0 = 10 ,RINT1= -100 , RINT2 = 300,RINT3= 600 ,RINT4=-10000,RINT5= 22000,RINT6=105,RINT7=30 ; 
char packet_ret[36]={1,36,0,checksum } ; 
void setup(){}
void loop() {

ltoa(RINT0, packet_ret+4, 16);
ltoa(RINT1, packet_ret+8, 16);
ltoa(RINT2, packet_ret+12, 16);
ltoa(RINT3, packet_ret+16, 16);
ltoa(RINT4, packet_ret+20, 16);
ltoa(RINT5, packet_ret+24, 16);
ltoa(RINT6, packet_ret+28, 16);
ltoa(RINT7, packet_ret+32, 16);


        for( int i = 0; i < 36; ++i )
         {  s0 += packet_ret[i];

         }
   
 checksum =  0xFF - s0 ;
     s0 = 0 ;   

  Serial.println(checksum,HEX);

    
   for (int i = 0; i < 36 ; i = i + 1) {
  if (packet_ret[i] < 16)  Serial.print("0");
  Serial.print(packet_ret[i],HEX);
  }
Serial.println();



 
  }
 

It is not huge, i'm working on this part like subroutine, once i solve it and i get the array ready, i will add this part to the udp server sketch.

this is the code

char checksum;
unsigned char s0 = 0;

int32_t RINT0 = 10 ,RINT1= -100 , RINT2 = 300,RINT3= 600 ,RINT4=-10000,RINT5= 22000,RINT6=105,RINT7=30 ; 
char packet_ret[36]={1,36,0,checksum } ; 
void setup(){}
void loop() {

ltoa(RINT0, packet_ret+4, 16);
ltoa(RINT1, packet_ret+8, 16);
ltoa(RINT2, packet_ret+12, 16);
ltoa(RINT3, packet_ret+16, 16);
ltoa(RINT4, packet_ret+20, 16);
ltoa(RINT5, packet_ret+24, 16);
ltoa(RINT6, packet_ret+28, 16);
ltoa(RINT7, packet_ret+32, 16);


        for( int i = 0; i < 36; ++i )
         {  s0 += packet_ret[i];

         }
   
 checksum =  0xFF - s0 ;
     s0 = 0 ;   

  Serial.println(checksum,HEX);

    
   for (int i = 0; i < 36 ; i = i + 1) {
  if (packet_ret[i] < 16)  Serial.print("0");
  Serial.print(packet_ret[i],HEX);
  }
Serial.println();



 
  }
 

Nearly. It will have '1', '2', '3' in the first 3 bytes of the array and will also have '\0' in buffer[3], which marks the end of the string and should not be ignored. Note too that the values are chars so will not be 1, 2 and 3

However, an int (on most Arduinos) consists of 2 bytes so you could send 123 as 2 bytes instead of 4 if you did it correctly. You also need to take into account than an int variable can have a negative value whereas an unsigned int can't

1 Like

First, your serial output will not work unless you do a Serial.begin() in setup(). Second, 4 characters is not near enough space for some of the conversions you are doing. Here are the results of those conversions:

RINT0: a 
RINT1: ffffff9c 
RINT2: 12c 
RINT3: 258 
RINT4: ffffd8f0 
RINT5: 55f0 
RINT6: 69 
RINT7: 1e

You must have room for all the characters AND the null terminator.

I see, i don't want to have 4 characters but 4 bytes for each variable, the message that i'm trying to form should be like this as i declare it packet_ret[36]={1,36,0,checksum,RINT0 ,RINT1,RINT2,RINT3,RINT4,RINT5,RINT6,RINT7 } ;

i'm sorry if i couldn't write clearly what is the issue.

Why does the array have 36 elements in your example ?

because i want to send 36 bytes from 0 to 35 .

So you need to transmit the values in binary? A great example of why we needed to see the entire script.

You just need to copy the values into the array (this is assuming the receiver of the data uses the same data "endianness" as you). The Arduino UNO is, by default, little endian. If your receiver is little endian as well you can just copy the values into the array.

  memcpy( packet_ret + 4, &RINT0, sizeof(RINT0));
  memcpy( packet_ret + 8, &RINT1, sizeof(RINT1));
  memcpy( packet_ret + 12, &RINT2, sizeof(RINT2));
  memcpy( packet_ret + 16, &RINT3, sizeof(RINT3));
  memcpy( packet_ret + 20, &RINT4, sizeof(RINT4));
  memcpy( packet_ret + 24, &RINT5, sizeof(RINT5));
  memcpy( packet_ret + 28, &RINT6, sizeof(RINT6));
  memcpy( packet_ret + 32, &RINT7, sizeof(RINT7));
1 Like

What platform is this on? Most use 16 bit ints, but some use 32 bit ints. It matters.
C