little endian , big endian , big headache

M

manishster

I keep getting the following in my output file , regardless of whether
I convert endian-ness or not [ currently commented out in the code
below ]. How do i get "01 02 03 04" ....

Mahamannu



output : 04 03 02 01 b0 00 00


Code :
//nHeight = swap_script(nHeight);
//nWidth = swap_script(nWidth);
nHeight = 0x01020304 ;
// nHeight = (nHeight >> 24) | ((nHeight << 8) & 0x00FF0000) |
((nHeight >> 8) & 0x0000FF00) | (nHeight << 24) ;
printf("and the height value after changing endian-ess is ");
printf("0x%08x\n",nHeight) ;

fwrite(&nHeight, sizeof(unsigned int), 1, fOut);
fwrite(&nWidth, sizeof(unsigned int), 1, fOut);
 
M

manishster

As I said I have commented the code that converts the endian-ness, but
that code works, i.e. the printf in the next line does show the
opposite endian-ness, but after writing through the fwrite its always
same [ 04 03 02 01 ]
 
M

manishster

Alright , no need to break you heads, My code was doing whats shown
below in a loop, so it would flip the endian-ness everytime , Just so
happened the output files I looked up were the ones where the
endian-ness was reset.
hope ya'll have a good day ,

just wondering though WHY this little to big and big to little isn't
part of some standard library !


As I said I have commented the code that converts the endian-ness, but
that code works, i.e. the printf in the next line does show the
opposite endian-ness, but after writing through the fwrite its always
same [ 04 03 02 01 ]

I keep getting the following in my output file , regardless of whether
I convert endian-ness or not [ currently commented out in the code
below ]. How do i get "01 02 03 04" ....

Mahamannu



output : 04 03 02 01 b0 00 00


Code :
//nHeight = swap_script(nHeight);
//nWidth = swap_script(nWidth);
nHeight = 0x01020304 ;
// nHeight = (nHeight >> 24) | ((nHeight << 8) & 0x00FF0000) |
((nHeight >> 8) & 0x0000FF00) | (nHeight << 24) ;
printf("and the height value after changing endian-ess is ");
printf("0x%08x\n",nHeight) ;

fwrite(&nHeight, sizeof(unsigned int), 1, fOut);
fwrite(&nWidth, sizeof(unsigned int), 1, fOut);
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


just wondering though WHY this little to big and big to little isn't
part of some standard library !

Probably for the same reason that window management, robotics control,
communications stacks, and financial computations aren't part of "some
standard library"; that is to say, because they aren't needed in a
standard library, and if you /do/ need them, you can either write them
yourself or find a /non-standard/ library that supports them.

HTH
- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFE7z9NagVFX4UWr64RAk/6AJ9//F4uJMe7cQfCXrOwsvU97yYfSQCg5i/T
klSDY7o5kn/yeB2ErD2JFVw=
=RRqx
-----END PGP SIGNATURE-----
 
S

Stephen Sprunk

Alright , no need to break you heads, My code was doing whats shown
below in a loop, so it would flip the endian-ness everytime , Just so
happened the output files I looked up were the ones where the
endian-ness was reset.
hope ya'll have a good day ,

just wondering though WHY this little to big and big to little isn't
part of some standard library !

Because it's easy enough to write such a macro/function yourself if you
need one?

<OT> If your system supports sockets, which nearly all do these days,
then you'll almost certainly find the macros ntohl(), htonl(), htons(),
and ntohs() available in some header. These will convert between host
(native) and network (big-endian) byte orders if they differ. On POSIX
systems, you're likely to find them in <netinet/in.h>; I don't know
where they live on Windows systems.

If you don't have sockets, there's still good odds that the
implementation provides the macros bswap(), byteswap(), or something
similar. These, however, have the property that, if used on a
big-endian system, they will swap your data to little-endianness, which
probably isn't what you want. </OT>

S
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top