how to split Float,Long and Int into set of Bytes
hi guys,
I have done this in C With Unions and Structure but how to do this in VIsual Basic.
How To split a 4 byte IEEE format Float number to 4 respective character bytes. e.g. 1234.56 = hex= 0x449A51EB this is a floating no convert this in
ch1 = 0x44h,
ch2 = 0x9Ah
ch3 = 0x51h
ch3 = 0xEBh
Also how to split Long integer which is also 4 bytes
I can do this in C by using union, by declaring inside , a floating point number and then structure of 4 characters.
please help
Yogi
Re: how to split Float,Long and Int into set of Bytes
The shortest and quickest method is to write a C++ dll to do this conversion for you.
Re: how to split Float,Long and Int into set of Bytes
You can do this in VB too. Just build 2 types, one with a set of bytes and one with just the variable type. Then use LSet to convert between them. Since both Singles (IEEE float) and Longs are 4 bytes, you can use the same structure to convert them into:
Code:
Private Type LongType
Value As Long
End Type
Private Type SingleType
Value As Single
End Type
Private Type FourBytes
Byte1 As Byte
Byte2 As Byte
Byte3 As Byte
Byte4 As Byte
End Type
Private Sub SplitIEEEFloat()
Dim uSingle As SingleType, uBytes As FourBytes
uSingle.Value = 1234.56
LSet uBytes = uSingle
Debug.Print "0x" & Hex(uBytes.Byte1) & "h"
Debug.Print "0x" & Hex(uBytes.Byte2) & "h"
Debug.Print "0x" & Hex(uBytes.Byte3) & "h"
Debug.Print "0x" & Hex(uBytes.Byte4) & "h"
End Sub
Private Sub SplitLong()
Dim uLong As LongType, uBytes As FourBytes
uLong.Value = 123456
LSet uBytes = uLong
Debug.Print "0x" & Hex(uBytes.Byte1) & "h"
Debug.Print "0x" & Hex(uBytes.Byte2) & "h"
Debug.Print "0x" & Hex(uBytes.Byte3) & "h"
Debug.Print "0x" & Hex(uBytes.Byte4) & "h"
End Sub
Re: how to split Float,Long and Int into set of Bytes
Forgot the Integer ;)
Code:
Private Type IntegerType
Value As Integer
End Type
Private Type TwoBytes
Byte1 As Byte
Byte2 As Byte
End Type
Private Sub SplitInteger()
Dim uInt As IntegerType, uBytes As TwoBytes
uInt.Value = 12345
LSet uBytes = uInt
Debug.Print "0x" & Hex(uBytes.Byte1) & "h"
Debug.Print "0x" & Hex(uBytes.Byte2) & "h"
End Sub
This link might also come in handy.
Re: how to split Float,Long and Int into set of Bytes
Never thought it could be done in VB. Never heard of LSet before :blush: . That was a good one Comintern. Thanks for enlightening me as well :wave:
Re: how to split Float,Long and Int into set of Bytes
hii guys,
thanks for the replies,
but answer to the last reply
yes it can be done, and i foound out,
There is a Sub Procedure in VB called CopyMemory,
Copies a long , or a float or Int into Array of Bytes,
and Vice Versa can also be done
yogi