CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    Converting a little endian number into long

    Code:
    long x_size = buff2[3] << 24 | buff2[2] << 4 | buff2[1] >> 4 | buff2[0] >> 24;
    The original value: 6630836
    The value stored in file: b4 2d 65 00

    I read the number as
    Code:
    char *buff2[5] = { 0 };
    f.read(buff2, sizeof(char)*4);
    The result is ffffffff (-1)

    Are there any compact ways I can convert the number into the right form?
    I guess I am not doing it right for the moment....
    Thanks
    Jack
    Last edited by lucky6969b; May 6th, 2011 at 03:45 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Converting a little endian number into long

    The size of the char is 8 bits. So how do you shift it 24 bits left (or right)?

    /Edit: besides: why not just read this long value into the long variable?
    Last edited by VictorN; May 6th, 2011 at 03:48 AM. Reason: "besides" was added
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    Re: Converting a little endian number into long

    I thought I was doing this
    00000000 1100101 00101101 10110100
    -----------
    |________________________>24bits???

    Incorrect I think, what is your insight?

    Going to tell my story though,
    I have several x files to pack into one pck file to avoid multi-opening.
    So I read the file size by ftell then write it to the pck file.
    I must confess there is some inconsistency. I used fwrite (const void *)&filesize, sizeof(long),1, f_ptr);
    then the data. Then reading it back into memory using the above format.
    Any suggestions that I can do it better?
    Thanks
    Jack
    Last edited by lucky6969b; May 6th, 2011 at 03:54 AM.

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Converting a little endian number into long

    Also, you are reading into an array of 5 char pointers, not 5 chars. I think you meant char buff2[5].

    The algorithm you are trying to use is also wrong. Try to get that right to start with, i.e. work out by how much you need to multiply each byte to get what you're looking for, then think about how to code that.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Converting a little endian number into long

    Yes, you think right! You did it incorrect. See my previous post.
    Victor Nijegorodov

  6. #6
    Join Date
    Dec 2010
    Posts
    907

    Re: Converting a little endian number into long

    Sorry Alan, Yes, i meant char buff2[5];

    Victor, let me take a thought
    Code:
    x_size = (long) buff2;
    The value seems to be off though...
    Jack
    Last edited by lucky6969b; May 6th, 2011 at 04:04 AM.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Converting a little endian number into long

    Quote Originally Posted by lucky6969b View Post
    Sorry Alan, Yes, i meant char buff2[5];
    So what is your actual code?
    This one:
    Quote Originally Posted by lucky6969b View Post
    Code:
    char *buff2[5] = { 0 };
    f.read(buff2, sizeof(char)*4);
    The result is ffffffff (-1)
    Or this one:
    Code:
    char buff2[5] = { 0 };
    f.read(buff2, sizeof(char)*4);
    Victor Nijegorodov

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Converting a little endian number into long

    Quote Originally Posted by lucky6969b View Post
    Sorry Alan, Yes, i meant char buff2[5];

    Victor, let me take a thought
    Code:
    x_size = (long) buff2;
    The value seems to be off though...
    Jack
    No it is wrong!
    Why not just _open a file and then _read to the long buffer exactly 4 bytes (or the sizeof(long))?
    Victor Nijegorodov

  9. #9
    Join Date
    Dec 2010
    Posts
    907

    Re: Converting a little endian number into long

    This one:
    Code:
    char buff2[5] = { 0 };
    f.read(buff2, sizeof(char)*4);
    Thanks a lot

    Update
    Error 2 error C2664: 'std::basic_istream<_Elem,_Traits>::read' : cannot convert parameter 1 from 'long' to 'char *' l:\shader approach\partiii\chapter 14\xfiledemo\d3dutil.cpp 207 XFileDemo
    Code:
    long buff2;
    f.read (buff2, sizeof(long));
    Sorry, still couldn't make it

    I am using this method because I need Unicode support.

    Update:
    I also tried this with no luck
    Code:
    x_size = (*buff2 >> 24) | ((*buff2 & 0x00ff0000) >> 8) | ((*buff2 & 0x0000ff00) << 8) | (*buff2 << 24);
    Thanks
    Last edited by lucky6969b; May 6th, 2011 at 04:25 AM.

  10. #10
    Join Date
    Dec 2010
    Posts
    907

    Re: Converting a little endian number into long

    I found a workaround this.. Thanks a lot
    Code:
    f.read(reinterpret_cast<char *>(&buff2), sizeof(long));

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured