big endian? little endian? ehm, loading a file saved from java...
hiya,
i have a file saved in java, and i'd like to load this in c++ using ifstream::read(...)..? help.., can't get the data's correctly.., what should i do? thanks,
[edited by - mickey on September 30, 2002 11:29:53 AM]
You either have to get the Java program to write out the file in little endian format, or read the data a byte at a time and shift it into the the appropriate data type.
ie if you know the first piece of data is an signed int, then you read in four bytes and shift them into an integer.
You can make this code tighter, but you get the idea.
---
Make it work.
Make it fast.
"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
ie if you know the first piece of data is an signed int, then you read in four bytes and shift them into an integer.
int final;unsigned char byte1,byte2,byte3,byte4;final=0;//read four bytesfinal |=byte4final << 8final |=byte3final << 8final |=byte2final << 8final |=byte1final << 8
You can make this code tighter, but you get the idea.
---
Make it work.
Make it fast.
"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
oh i see, thanks!!!
is that possible? specify in c++ if you''d like to save your data in big endian or little endian? if so, how? or hmmm..., before i save my datas i shift it? is this a good way to encrypt your datas? or maybe that''s not good because i''ll lose bits making my data useless anymore?
is that possible? specify in c++ if you''d like to save your data in big endian or little endian? if so, how? or hmmm..., before i save my datas i shift it? is this a good way to encrypt your datas? or maybe that''s not good because i''ll lose bits making my data useless anymore?
hiya,
could you pls comment on this one? this is what i did,
the file when saved in java has it''s 4 bytes like this,
0x00000002
so on my c++, i did this,
int value = value >> 24;
thus getting 2 which is what i needed.., ehm, sort of troubles would i get here if i''m not careful?
many thanks,
could you pls comment on this one? this is what i did,
the file when saved in java has it''s 4 bytes like this,
0x00000002
so on my c++, i did this,
int value = value >> 24;
thus getting 2 which is what i needed.., ehm, sort of troubles would i get here if i''m not careful?
many thanks,
Basically, your problems will start as soon as your numbers start getting >= 0x00000100.
The problem is that 0x01020304 on big-endian becomes 0x04030201 on little-endian machines and vice-versa, so only shifting the values would only work for values fitting in the last byte.
At least on Un*x machines, you get the handy functions ntohl, ntohs, htonl, htons to convert between host and network byte order no matter what machine you''re working on. Java always stores its stuff in network byte order, so for 32bit values it''d just be a call like
value = ntohl( value );
and you''re done.
Funny thing: PDPs used another ordering scheme that was something like 0x03040102. Luckily you''ll probably never ever have to write a program for that architecture.
The problem is that 0x01020304 on big-endian becomes 0x04030201 on little-endian machines and vice-versa, so only shifting the values would only work for values fitting in the last byte.
At least on Un*x machines, you get the handy functions ntohl, ntohs, htonl, htons to convert between host and network byte order no matter what machine you''re working on. Java always stores its stuff in network byte order, so for 32bit values it''d just be a call like
value = ntohl( value );
and you''re done.
Funny thing: PDPs used another ordering scheme that was something like 0x03040102. Luckily you''ll probably never ever have to write a program for that architecture.
More like:
value=((value>>24)&0x000F)|((value>>8)&0x00F0)|((value<<8)&0x0F00)|((value<<24)&0xF000)
You need to reverse the bytes, not just shift.
---
Make it work.
Make it fast.
"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
value=((value>>24)&0x000F)|((value>>8)&0x00F0)|((value<<8)&0x0F00)|((value<<24)&0xF000)
You need to reverse the bytes, not just shift.
---
Make it work.
Make it fast.
"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
wow, that was confusing! but i did it! thanks very much guys!!!
CaptainJester, i don''t know if you know but why are your hexes only in 16 bytes? also, apparently your ehm, formula, there''s something wrong,
this is what i did,
value = right | rightmost | left | leftmost;
one last thing plz before we end this discussion, what''s with the big endian and little endian thing?
CaptainJester, i don''t know if you know but why are your hexes only in 16 bytes? also, apparently your ehm, formula, there''s something wrong,
this is what i did,
int value = grid[x][y];int leftmost, left, right, rightmost;leftmost = (grid[x][y] << 24) & 0xFF000000;left = (grid[x][y] << 8) & 0x00ff0000 ;right = (grid[x][y] >> 8) & 0x0000ff00;rightmost = grid[x][y] >> 24; // i did & this anymore since it is not needed,
value = right | rightmost | left | leftmost;
one last thing plz before we end this discussion, what''s with the big endian and little endian thing?
quote:Original post by mickey
CaptainJester, i don''t know if you know but why are your hexes only in 16 bytes? also, apparently your ehm, formula, there''s something wrong,
Whoops. Shoulda been:
value=((value>>24)&0x000000FF)|((value>>8)&0x0000FF00)|((value<<8)&0x00FF0000)|((value<<24)&0xFF000000)
---
Make it work.
Make it fast.
"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement