little endian , big endian , big headache

Discussion in 'C Programming' started by manishster@gmail.com, Aug 25, 2006.

  1. Guest

    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);
     
    , Aug 25, 2006
    #1
    1. Advertising

  2. Guest

    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 ]

    wrote:
    > 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);
     
    , Aug 25, 2006
    #2
    1. Advertising

  3. Guest

    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 !


    wrote:
    > 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 ]
    >
    > wrote:
    > > 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);
     
    , Aug 25, 2006
    #3
  4. Default User Guest

    wrote:

    > Alright , no need to break you heads



    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:
    <http://www.caliburn.nl/topposting.html>



    Brian (here we go again)
     
    Default User, Aug 25, 2006
    #4
  5. Lew Pitcher Guest

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


    wrote:
    [snip]
    > 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-----
     
    Lew Pitcher, Aug 25, 2006
    #5
  6. <> wrote in message
    news:...
    > 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

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Stephen Sprunk, Aug 31, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hicham
    Replies:
    2
    Views:
    9,028
    dxcoder
    Jul 2, 2003
  2. Ernst Murnleitner

    float: IEEE, big endian, little endian

    Ernst Murnleitner, Jan 13, 2004, in forum: C++
    Replies:
    0
    Views:
    870
    Ernst Murnleitner
    Jan 13, 2004
  3. invincible

    Little Endian to Big Endian

    invincible, Jun 14, 2005, in forum: C++
    Replies:
    9
    Views:
    14,356
    Old Wolf
    Jun 14, 2005
  4. invincible
    Replies:
    1
    Views:
    547
    red floyd
    Jun 14, 2005
  5. hicham

    convert from big-endian to little-endian

    hicham, Jun 30, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,525
    hicham
    Jun 30, 2003
Loading...

Share This Page