the byte be reversed! why?

D

David Li

I use VC++6.0 to code.
When I defined: struct Prop { UINT32 obj;} , and I read bytes from
file: {
char buf[4]); fread(buf,4); } then I use (Prop*)buf to set m_prop
which is declared as Prop;(m_prop = (Prop*)buf),but the value of m_prop
is reversed to buf. Why?

for example: buf is 0x50,0x51,0x52,x053, and m_prop->ojb is:
0x53,0x52,0x51,0x50.
my OS is winxp.
 
A

Alf P. Steinbach

* David Li:
I use VC++6.0 to code.
When I defined: struct Prop { UINT32 obj;} , and I read bytes from
file: {
char buf[4]); fread(buf,4); } then I use (Prop*)buf to set m_prop
which is declared as Prop;(m_prop = (Prop*)buf),but the value of m_prop
is reversed to buf. Why?

for example: buf is 0x50,0x51,0x52,x053, and m_prop->ojb is:
0x53,0x52,0x51,0x50.
my OS is winxp.

This has nothing to do with C++, hence, off-topic in [clc++].

You're presenting the bytes of 'buf' in one order, and those of
'obj' in the opposite order.

Reverse one of the presentations to get the same order in both.

XFUT: [comp.programming]
 
B

Bart Samwel

David said:
for example: buf is 0x50,0x51,0x52,x053, and m_prop->ojb is:
0x53,0x52,0x51,0x50.
my OS is winxp.

Intel/x86 processors are little-endian, i.e., they store the bytes of
an integer with the least significant byte first. You are obviously
expecting the reverse, which only happens in big-endian hardware. There
is no way around this except to swap the byte order yourself.

--Bart
 
D

David Li

Thanks,I will swap them by myself.
And i already swap them in my codes.I only swap four bytes,and if i
operate many bytes,it's an extra works?
ps: another question: when i use (long)buf, i will not get the correct
result. I have to use the mothod memcpy(long*,void*,size_t) to get
it.Is it the only way??
 
B

Bart Samwel

David said:
I only swap four bytes,and if i operate many bytes,it's an extra
works?

Of course it takes extra work when you want to swap more bytes, you
have to perform the work for every group of 4 bytes. Just be sure to
only reverse the order of every group of 4 bytes, not of the whole
buffer. :)
another question: when i use (long)buf, i will not get the correct
result. I have to use the mothod memcpy(long*,void*,size_t) to get
it.Is it the only way??

That's because "buf" in this case does not refer to the array itself,
but it is the address of the first element of the array "buf", i.e.,
buf = &buf[0]. So you'll have to do (long*)buf, or in modern C++ style,
reinterpret_cast<long*>(buf).

--Bart
 
D

David Li

yeah,i see.
I will try it, is (long*) not (long),oh,it's......... :p
oh,my god,i am a foolish boy. :p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top