Skip to main content
gamedev.net
🔒 Locked

byte manipulation

Started by outRider Sep 4, 2000 at 5:51 PM 3 replies 550+ views
Original Post
outRider
outRider
How would I get access to the 4 component bytes of a long int or a float? And similarly, how would I reat 4 seperate char bytes as a single unit to make a long int or a float? For example, if I had unsigned char *value; short int value2; value = malloc(sizeof(short)); //2 Bytes read into value; How would I then stick those 2 seperate bytes into value2 to make a short int? long int *value; short int value2, value3, value4, value5; value3 = malloc(sizeof(long)*2); How would I take 4 bytes from the first long and stick them into value2 and 3, and then the other 4 bytes into 4 and 5? value[0] and value[1] would reference 4 byte chunks so how would I do it? ------------ - outRider -
J2xC
J2xC
I''m not sure quite what you want to know, but I think all you''ll need is the HIWORD, LOWORD, HIBYTE, and LOBYTE macros, along with the left shift and right shift bitwise operators...

J2xC (J. Connolly)

"Then study this: Borg provokes Klingon, Klingon breaks Borg's nose" Belanna Torres, Star Trek Voyager 5.11 "Someone to watch over me"

J2xC (J. Connolly) Ah! By popular demand, I shall no longer resist...
felonius
felonius

Do the following (I am just writing this out of my head so please don''t blame me if it contains errors)

const int bytemask = (1 << 8) - 1;

int byte0 = myint & bytemask;
int byte1 = (myint >> 8) & bytemask;
int byte2 = (myint >> 16) & bytemask;
int byte3 = (myint >> 24) & bytemask;

Now you have the 4 bytes.

It generally does not make sense to do this with float because
of the special format used. If you do you should do some reading on it first.

Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.