Click to See Complete Forum and Search --> : Convert a unsigned long to Big Endian Format
author
July 16th, 2001, 06:30 AM
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.
Cimperiali
July 16th, 2001, 07:26 AM
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.
author
July 16th, 2001, 08:54 AM
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.
Clearcode
July 16th, 2001, 09:06 AM
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.
author
July 16th, 2001, 09:39 AM
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.
Clearcode
July 16th, 2001, 10:20 AM
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.
author
July 16th, 2001, 10:25 AM
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
Clearcode
July 16th, 2001, 10:33 AM
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) >= 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.
author
July 16th, 2001, 10:42 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.