pointers, binary data, and shifting

1
2
3
unsigned short foo = 1337;
//since unsigned short is 2 bytes, a char x[2]should be enough to hold the binary data
char foo2[2];


How do I have foo2, contain the binary equivilant of foo? (how can I point foo2 to = &foo)
You seem to be confusing two entirely separate concepts and/or asking two completely unrelated questions. I'll start with the pointer question:

(how can I point foo2 to = &foo)


foo2 can't point to foo because foo2 isn't a pointer.

If you want something to point to foo, then you can use a pointer:

1
2
3
unsigned short foo = 1337;

unsigned short* fooptr = &foo;  // <- now fooptr points to foo 




How do I have foo2, contain the binary equivilant of foo?


This has nothing really to do with pointers, and more to do with copying binary data.

1337 in hex is 0x0539. So the 2 bytes that you'd want assigned here are 0x05 and 0x39. (Note again that these are not pointers and this has nothing to do with pointers).



You have two options. The first is to use a dumb memory copy function to copy the binary data. IE, memcpy:

1
2
3
4
5
6
7
8
9
10
11
12
unsigned short foo = 1337;
char foo2[2];

// technique 1:  use memcpy
memcpy( foo2, &foo, 2 );

// at this point:
//  foo2[0] == 0x39
//     and
//  foo2[1] == 0x05
//     (assuming you're on a little endian system.  On a big endian system those will
//     be flipped) 


The other option is to do it manually:

1
2
3
4
5
6
7
8
9
10
11
12
unsigned short foo = 1337;
char foo2[2];

// technique 2:  manual copy
foo2[0] = char( foo & 0xFF );
foo2[1] = char( (foo >> 8) & 0xFF );

// at this point:
//  foo2[0] == 0x39
//     and
//  foo2[1] == 0x05
//     REGARDLESS of system endianness. 
Last edited on
Thanks for the reply, great information!

I'm dabbling in some winsock tutorials, and everything received in a char* buffer, regardless of datatype, so I was trying to figure out how to match the data to appropriate variables for handling.

Not used to using mem functions, looks like I gotta go back to some basics again before proceeding forward.
Also there is other thing which you need to know about.

since unsigned short is 2 bytes
is technically false. unsigned short size is equal or larger than 16 bits. It can be 1 (for systems with 16 bit chars) or 4 (for systems wwith 8 bit char but with 32 bit shorts)
Though sizeof(unsigned short) is 2 on majority PC compilers, you should not rely on this behavior.

You should either get size of type in compile time, or make sure it is 2 before doing anything:
1
2
3
4
5
6
7
8
9
unsigned short foo = 1337;
char foo2[sizeof(foo)];
memcpy(foo2, &foo, sizeof(foo));
//...
static_assert(sizeof(unsigned short) == 2, "Unexpected type size: unsigned short");
unsigned short foo = 1337;
char foo2[2]
foo2[0] = char( foo & 0xFF );
foo2[1] = char( (foo >> 8) & 0xFF );
This will not going to do any overhead because both sizeof operator and static_assert are working in compile time.
definitely very good to know. thanks MiiNiPaa!
Topic archived. No new replies allowed.