Converting an int into a 4 byte char array

unsigned char bytes[4];
unsigned long n = 175;

bytes[0] = (n >> 24) & 0xFF;
bytes[1] = (n >> 16) & 0xFF;
bytes[2] = (n >> 8 ) & 0xFF;
bytes[3] = n & 0xFF;

Serial.println(bytes[0]);
Serial.println(bytes[1]);
Serial.println(bytes[2]);
Serial.println(bytes[3]);

Expected Output: 0x00 0x00 0x00 0xaf

Note: It's not working as expected.
Please help me tweaking above code.

Serial.println(x,HEX);

RakeshArduino:
Note: It's not working as expected.

What does that mean?

unsigned char bytes[4];
unsigned long n = 175;

bytes[0] = (n >> 24) & 0xFF;
bytes[1] = (n >> 16) & 0xFF;
bytes[2] = (n  >> 8  )  & 0xFF;
bytes[3] = n & 0xFF;

Serial.write(bytes[0]);
Serial.write(bytes[1]);
Serial.write(bytes[2]);
Serial.write(bytes[3]);

jremington:
Serial.println(x,HEX);

Hi jremington,

I want to send the data in bytes format, How can i fix below?

byte x = 300;
Serial.println(x,HEX);

Byte supports 256, so 300 -256 = 44.
44 in Hex = 2C is printing now rather than 12C.

Can we try print using 2 bytes like unsigned char bytes[2];

bytes[0] = (n >> 8 ) & 0xFF;
bytes[1] = n & 0xFF;

Serial.write(bytes[0]);
Serial.write(bytes[1]);

or any other better approach?

why not just used union:

union{
unit16_t word;
uint8_t bytes[2];
}x;

x.word = your integer value

x.bytes[0] returns integer LSByte
x.bytes[1] returns integer MSByte

void setup()
{
  Serial.begin(115200);
  unsigned long n = 175;
  char buffer[4];
  sprintf(buffer, "%04x", n);
  Serial.println(buffer);
}

void loop()
{
}

UKHeliBob:

void setup()

{
  Serial.begin(115200);
  unsigned long n = 175;
  char buffer[4];
  sprintf(buffer, "%04x", n);
  Serial.println(buffer);
}

void loop()
{
}

Hi UkHeliBob,

Thanks a lot.

How do i fix below?

void setup()
{
Serial.begin(115200);
unsigned long n = 1000000;
char buffer[5];
sprintf(buffer, "%05x", n);
Serial.println(buffer);
}

void loop()
{
}

Expected output: ‭F4240‬, Actual output : 04240

Try

sprintf(buffer, "%05lx", n);

And please start using code tags when you post code. You have been shown how several times now.

First you fix an error in my program. The buffer needs to be declared large enough to hold the output characters plus the terminating zero added to the string.

Then you change the format specifier to accept an unsigned long

Try this

void setup()
{
  Serial.begin(115200);
  unsigned long n = 1000000;
  char buffer[9];
  sprintf(buffer, "%05lx", n);
  Serial.println(buffer);
  sprintf(buffer, "%05lX", n);
  Serial.println(buffer);
}

void loop()
{
}

UKHeliBob:
First you fix an error in my program. The buffer needs to be declared large enough to hold the output characters plus the terminating zero added to the string.

Then you change the format specifier to accept an unsigned long

Try this

void setup()

{
  Serial.begin(115200);
  unsigned long n = 1000000;
  char buffer[9];
  sprintf(buffer, "%05lx", n);
  Serial.println(buffer);
  sprintf(buffer, "%05lX", n);
  Serial.println(buffer);
}

void loop()
{
}

Hi UKHeliBob,

Thanks a lot, It's working.

Please explain me below lines and relation.

unsigned long n = 1000000;
char buffer[9];
sprintf(buffer, "%05lX", n);
Serial.println(buffer);

unsigned long n = 1000000;
char buffer[9];               //declare a character buffer large enough to hold the characters and terminating zero
sprintf(buffer, "%05lX", n);  //format the output and put it in the buffer.
                              //buffer the output goes here
                              //05 pad with leading zeroes to make the output 5 characters
                              //l  the input is an unsigned long
                              //X output is to be in uppercase HEX
                              //n the value to be formatted
                              //See http://www.cplusplus.com/reference/cstdio/printf/
Serial.println(buffer);       //print the contents of the buffer

UKHeliBob:

unsigned long n = 1000000;

char buffer[9];              //declare a character buffer large enough to hold the characters and terminating zero
sprintf(buffer, "%05lX", n);  //format the output and put it in the buffer.
                              //buffer the output goes here
                              //05 pad with leading zeroes to make the output 5 characters
                              //l  the input is an unsigned long
                              //X output is to be in uppercase HEX
                              //n the value to be formatted
                              //See http://www.cplusplus.com/reference/cstdio/printf/
Serial.println(buffer);      //print the contents of the buffer

Hi UKHeilBob,

You are amazing, thanks a lot.