CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2001
    Posts
    35

    Convert a unsigned long to Big Endian Format

    Hi

    I wanted to convert a unsigned long integer to Big Endian format. Can anybody help me with this.

    There is a Long data type but how do i specify a Unsigned Long Integer. The value range of Long and Unsigned Long is different. It would be better still if anybody could help me with the LongInt2BigEndian function.

    Thanks.


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Convert a unsigned long to Big Endian Format

    Use search option and look for "Bigendian". There's already been an answer for this here...

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Feb 2001
    Posts
    35

    Re: Convert a unsigned long to Big Endian Format

    I saw all the answers but I am sorry I couldn't find a perfect answer to this question. Would you be kind enough to tell me how i could solve this.

    I need to convert Intel based unsigned long integer to Motorola format (Big Endian).

    Example I need a function which would convert
    12 34 56 78 90 to 90 78 56 34 12

    It needs that the bits in the bytes be reversed and then the bytes themselves be reversed.

    How to do this ? Any help will be appreciated.


  4. #4
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Convert a unsigned long to Big Endian Format

    The solution I found was....

    public Function MakeInt(byval LoByte as Byte, byval hiByte as Byte) as Integer

    MakeInt = ((hiByte * &H100) + LoByte)

    End Function

    public Function MakeLong(byval LoWord as Integer, byval HiWord as Integer) as Long

    MakeLong = ((HiWord * &H10000) + LoWord)

    End Function

    '\\ --[LoWord]-----------------------------------------------------------------------------
    '\\ Returns the low word component of a long value
    '\\ Parameters:
    '\\ dw - The long of which we need the LoWord
    '\\
    '\\ --------------------------------------------------
    public Function LoWord(dw as Long) as Integer
    If dw And &H8000& then
    LoWord = &H8000 Or (dw And &H7FFF&)
    else
    LoWord = dw And &HFFFF&
    End If
    End Function

    '\\ --[HiByte]-----------------------------------------------------------------------------
    '\\ Returns the high byte component of an integer
    '\\ Parameters:
    '\\ w - The integer of which we need the HiByte
    '\\
    '\\ -------------------------------------------------
    public Function hiByte(byval w as Integer) as Byte
    If w And &H8000 then
    hiByte = &H80 Or ((w And &H7FFF) \ &HFF)
    else
    hiByte = w \ 256
    End If
    End Function

    '\\ --[HiWord]-----------------------------------------------------------------------------
    '\\ Returns the high word component of a long value
    '\\ Parameters:
    '\\ dw - The long of which we need the HiWord
    '\\
    '\\ --------------------------------------------------
    public Function HiWord(dw as Long) as Integer
    If dw And &H80000000 then
    HiWord = (dw \ 65535) - 1
    else
    HiWord = dw \ 65535
    End If
    End Function

    '\\ --[LoByte]-----------------------------------------------------------------------------
    '\\ Returns the low byte component of an integer value
    '\\ Parameters:
    '\\ w - The integer of which we need the loWord
    '\\
    '\\ --------------------------------------------------
    public Function LoByte(w as Integer) as Byte
    LoByte = w And &HFF
    End Function




    Then you can use these to swap your bytes and bits around as you desire:


    private Function BigEndian(lIn as long) as Long
    Dim nloWord as Integer, nhiWord as Integer

    nloWord = LoWord(lIn)
    nHiWord = HiWord(lIn)

    sDVal = MakeLong(MakeInt(HiByte(nLoWord), LoByte(nLoWord)), MakeInt(HiByte(nHiWord), LoByte(nHiWord)))

    End Function




    Hope this helps,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  5. #5
    Join Date
    Feb 2001
    Posts
    35

    Re: Convert a unsigned long to Big Endian Format

    Hi Duncan

    Thanks for the code. I get an overflow error.

    I want to do something like this

    [vbcode]

    dim varseconds as string

    varseconds=BigEndian(DateDiff("s", #1/1/1904#, Now))

    Where your code BigEndian should take the 10 digit value (seconds) and convert them to Big Endian format and return it to the variable varseconds.

    Thanks for the code once again. Please do help me with this if you can. Really appreciate it.


  6. #6
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Convert a unsigned long to Big Endian Format

    The number of seconds is too large to fit in a long integer value:

    ? DateDiff("s", #1/1/1904#, Now)
    3078144954




    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  7. #7
    Join Date
    Feb 2001
    Posts
    35

    Re: Convert a unsigned long to Big Endian Format

    Hi Duncan

    Yep i know that. But i need the output in Big endian Format.

    3078144954 should be 5449147830

    Wondering how to do this in VB



  8. #8
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Convert a unsigned long to Big Endian Format

    That isn't reallyt big-endian...but the effect you desire can be acheived by:

    private Sub Form_Load()

    Debug.print ReversepairString("3078144954")

    End Sub

    private Function ReversepairString(byval sIn as string) as string

    Dim sTmp as string

    While len(sIn) &gt;= 2
    sTmp = sTmp & Right$(sIn, 2)
    sIn = Left$(sIn, len(sIn) - 2)
    Wend

    ReversepairString = sTmp

    End Function




    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  9. #9
    Join Date
    Feb 2001
    Posts
    35

    Re: Convert a unsigned long to Big Endian Format

    Hi

    Thanks once again for the code. But this won't work in my case. I need the Big Endian format.

    I need a function which accepts a unsigned long value and outputs a four character string big endian representation of that number.

    Public Function MakeBigEndian(value as unsignedlong) as string

    Thanks for the reverse string code.



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