0

I have an int that I need to store as two chars.

So is there some way to get the first 8 bits and the second 8 bits and store them as chars.

And if so, how would I recombine them later to get a meaningful int value?

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

what makes you think an int is only 16 bits? Depending on your compiler, it might, but then again it might not.

Okay, then store an int as however many chars it needs to be...that's easily determinable using sizeof

0
char buf[sizeof(int)];
int x = 1234;
memcpy(buf, &x, sizeof(int));

This might also work: *(int *)buf = x;

Edited by Ancient Dragon: n/a

0
char buf[sizeof(int)];
int x = 1234;
memcpy(buf, &x, sizeof(int));

Okay...how do I get from buf to the original int?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.