Converting An Int Into A 4 Byte Char Array - Arduino Forum

Converting an int into a 4 byte char array Projects Programming September 11, 2018, 2:41pm 1

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.

September 11, 2018, 2:43pm 2

Serial.println(x,HEX);

September 11, 2018, 2:45pm 3

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

What does that mean?

September 11, 2018, 2:47pm 4 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]); September 11, 2018, 3:20pm 5

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?

September 11, 2018, 3:39pm 6

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

September 11, 2018, 4:14pm 7 void setup() {   Serial.begin(115200);   unsigned long n = 175;   char buffer[4];   sprintf(buffer, "%04x", n);   Serial.println(buffer); } void loop() { } September 12, 2018, 5:40am 8

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

September 12, 2018, 6:15am 9

Try

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

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

September 12, 2018, 6:20am 10

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() { } September 13, 2018, 8:58am 11

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);

September 13, 2018, 9:15am 12 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 September 13, 2018, 3:13pm 13

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.

Topic Replies Views Activity
Convert string or word to 4 bytes Programming 25 1517 August 14, 2023
Afficher un unsigned long sur le monitoe en hexa Français 11 102 August 11, 2025
Unsigned Long to Array General Guidance 16 277 June 26, 2025
Convert Char Array to byte Array Programming 21 1410 December 7, 2024
Trouble with LONG to BYTE array Programming 12 906 January 29, 2024
Unfortunately, your browser is unsupported. Please switch to a supported browser to view rich content, log in and reply.

Tag » Arduino Convert 4 Bytes To Int