In your question, you stated that you want to convert a user input of 175 to
00000000 00000000 00000000 10101111
, which is big endian byte ordering, also known as network byte order.
A mostly portable way to convert your unsigned integer to a big endian unsigned char array, as you suggested from that "175" example you gave, would be to use C's htonl()
function (defined in the header <arpa/inet.h>
on Linux systems) to convert your unsigned int to big endian byte order, then use memcpy()
(defined in the header <string.h>
for C, <cstring>
for C++) to copy the bytes into your char (or unsigned char) array.
The htonl()
function takes in an unsigned 32-bit integer as an argument (in contrast to htons()
, which takes in an unsigned 16-bit integer) and converts it to network byte order from the host byte order (hence the acronym, Host TO Network Long, versus Host TO Network Short for htons
), returning the result as an unsigned 32-bit integer. The purpose of this family of functions is to ensure that all network communications occur in big endian byte order, so that all machines can communicate with each other over a socket without byte order issues. (As an aside, for big-endian machines, the htonl()
, htons()
, ntohl()
and ntohs()
functions are generally compiled to just be a 'no op', because the bytes do not need to be flipped around before they are sent over or received from a socket since they're already in the proper byte order)
Here's the code:
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
int main() {
unsigned int number = 175;
unsigned int number2 = htonl(number);
char numberStr[4];
memcpy(numberStr, &number2, 4);
printf("%x %x %x %x\n", numberStr[0], numberStr[1], numberStr[2], numberStr[3]);
return 0;
}
Note that, as caf said, you have to print the characters as unsigned characters using printf's %x
format specifier.
The above code prints 0 0 0 af
on my machine (an x86_64 machine, which uses little endian byte ordering), which is hex for 175.