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

    Question Converting BYTE[] to ULING

    Hi,

    suppose I have the following buffer:

    Code:
    BYTE b[4];
    
    b[0] = 0x40;
    b[1] = 0xe2;
    b[2] = 0x01;
    b[3] = 0x00;
    This buffer represents the number "123456".

    How do I convert this buffer into a ULONG ??

  2. #2
    Join Date
    Apr 2009
    Posts
    5

    Re: Converting BYTE[] to ULING

    Good question, is ulong associated with an Unsigned 64-bit integer? I am new to programming in a sense. At least, every time I turn around, I feel like im noob.

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Converting BYTE[] to ULING

    Don't know what exactly the correct way is, but you can always shift them into the ulong.

    Code:
    ULONG b;
    
    b= 0x40;
    b+= (0xe2 << 8);
    b+= (0x01 << 16);
    b+= (0x00 << 24);

  4. #4
    Join Date
    Apr 2009
    Posts
    5

    Re: Converting BYTE[] to ULING

    Why would you do that? and also, was I correct in the statement I made earlier?

  5. #5
    Join Date
    Dec 2004
    Posts
    293

    Re: Converting BYTE[] to ULING

    Thanks - It works fine !

  6. #6
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Converting BYTE[] to ULING

    Although some will recommend against it, you could do a simple cast:
    Code:
    BYTE b[4];
    ULONG lVal = 0;
    
    b[0] = 0x40;
    b[1] = 0xe2;
    b[2] = 0x01;
    b[3] = 0x00;
    
    lVal = *(ULONG*)&b[0];
    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  7. #7
    Join Date
    Apr 2009
    Posts
    47

    Re: Converting BYTE[] to ULING

    krmed, why do ppl argue against your proposed method ? After all, wouldn't that work on both big and little endian architecture *** opposed to just shifting them up which only works on BIG endian ?

  8. #8
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Converting BYTE[] to ULING

    After all, wouldn't that work on both big and little endian architecture *** opposed to just shifting them up which only works on BIG endian ?
    Nope, exactly the other way around. Shifting works on both big and little endian and casting works only by accident.

  9. #9
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Converting BYTE[] to ULING

    The casting as noted works on little endian systems, and will fail with big endian. Simple casting as I showed can be unsafe (that's why some will say don't do it), and you must therefore understand when a simple cast is acceptable in order to prevent big time problems.

    However, it is not "by accident" that it works on little endian systems...it works because the layout of the data is correct for simple casting on little endian systems.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  10. #10
    Join Date
    Jul 2005
    Posts
    266

    Re: Converting BYTE[] to ULING

    a simple union would also do

    union ul
    {
    BYTE b[4];
    ULONG result;
    } ;

    setting the correct bytes in b in the correct order ( according to the endianess of the machine ofc)
    you can the access result as an ulong.

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