Reputation: 6281
I want to take the value stored in a 32 bit unsigned int, put it into four chars and then store the integer value of each of these chars in a string.
I think the first part goes like this:
char a = orig << 8;
char b = orig << 8;
char c = orig << 8;
char d = orig << 8;
Upvotes: 4
Views: 3263
Reputation: 140122
If you really want to extract the individual bytes first:
unsigned char a = orig & 0xff;
unsigned char b = (orig >> 8) & 0xff;
unsigned char c = (orig >> 16) & 0xff;
unsigned char d = (orig >> 24) & 0xff;
Or:
unsigned char *chars = (unsigned char *)(&orig);
unsigned char a = chars[0];
unsigned char b = chars[1];
unsigned char c = chars[2];
unsigned char d = chars[3];
Or use a union of an unsigned long and four chars:
union charSplitter {
struct {
unsigned char a, b, c, d;
} charValues;
unsigned int intValue;
};
charSplitter splitter;
splitter.intValue = orig;
// splitter.charValues.a will give you first byte etc.
Update: as friol pointed out, solutions 2 and 3 are not endianness-agnostic; which bytes a
, b
, c
and d
represent depend on the CPU architecture.
Upvotes: 10
Reputation: 182822
Not quite:
char a = orig & 0xff;
orig >>= 8;
char b = orig & 0xff;
orig >>= 8;
char c = orig & 0xff;
orig >>= 8;
char d = orig & 0xff;
Not exactly sure what you mean by "store the integer values of each of these values into a string. Do you want turn 0x10111213
into "16 17 18 19"
, or what?
Upvotes: 1
Reputation: 16508
Use a union
. (As requested here is the sample program.)
#include <<iostream>>
#include <<stdio.h>>
using namespace std;
union myunion
{
struct chars
{
unsigned char d, c, b, a;
} mychars;
unsigned int myint;
};
int main(void)
{
myunion u;
u.myint = 0x41424344;
cout << "a = " << u.mychars.a << endl;
cout << "b = " << u.mychars.b << endl;
cout << "c = " << u.mychars.c << endl;
cout << "d = " << u.mychars.d << endl;
}
As James mentioned this is platform specific.
Upvotes: 4
Reputation: 7096
Let's say "orig" is a 32bit variable containing your value.
I imagine you want to do something like this:
unsigned char byte1=orig&0xff;
unsigned char byte2=(orig>>8)&0xff;
unsigned char byte3=(orig>>16)&0xff;
unsigned char byte4=(orig>>24)&0xff;
char myString[256];
sprintf(myString,"%x %x %x %x",byte1,byte2,byte3,byte4);
I'm not sure this is always endian correct, by the way. (Edit: indeed, it is endian correct, since bitshift operations shouldn't be affected by endianness)
Hope this helps.
Upvotes: 10
Reputation: 140122
For hexadecimal:
sprintf(buffer, "%lX", orig);
For decimal:
sprintf(buffer, "%ld", orig);
Use snprintf
to avoid a buffer overflow.
Upvotes: 0