Link to home
Start Free TrialLog in
Avatar of clylv
clylv

asked on

Single to Byte HELP!

OK,
My prob is this:
I get a single as in-parameter in (32 Bits)
I need to convert this to four seperate bytes (8*4 bits)

in-param Single = [10101010101010101010101010101010]
the trick is that this has no meaning at all, i need to put the first 8 bits in to a byte and the next 8 into another, like this:
Byte_1 = [10101010]
Byte_2 = [10101010]
Byte_3 = [10101010]
Byte_4 = [10101010]

Now I can interpret the byte as four different chars

need this fast
Thankyou

Claes Ylving
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

Private Sub Command2_Click()

    Dim bytes(0 To 7)
    Dim c As Long
   
    dHex 3.141592654, bytes
    For c = 0 To 7
        Print bytes(c)
    Next
   
   

End Sub

'in a .bas


Option Explicit

Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Public Function dHex(dblVal As Double, byteArray) As String
'double variable ONLY to hex
Dim c As Long, dHex2 As String
Dim dVal As Double, sTemp As String

Dim b(0 To 8) As Byte

dVal = CDbl(dblVal)
CopyMemory b(0), dVal, 8

For c = 0 To 7
    byteArray(c) = b(c)
Next

'Debug.Print b(0), b(1), b(2), b(3), b(4), b(5), b(6), b(7)


For c = 0 To 7
    sTemp = Hex(b(c))
    sTemp = String(2 - Len(sTemp), "0") & sTemp
    dHex2 = dHex2 & sTemp
   
Next
dHex = dHex2

For c = 1 To 8
    If Mid(dHex2, c, 1) = "0" Then
        dHex = Mid(dHex, 2)
    Else
        Exit For
    End If
Next
If dHex = "" Then dHex = "0"
       


End Function
'sorry I see you are using a single (4 bytes)

Private Sub Command2_Click()

    Dim bytes(0 To 3)
    Dim c As Long
    Dim x As String
   
    x = sHex(3.14159, bytes)
    For c = 0 To 3
        Print bytes(c)
    Next
    Print x
   

End Sub

in a .bas

Option Explicit

Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Public Function sDec(ByVal strVal As String) As Single
'8 character hex to double
Dim c As Long
Dim dblVal As Single

Dim b(0 To 3) As Byte

If Len(strVal) < 8 Then strVal = String(8 - Len(strVal), "0") & strVal

For c = 0 To 3
    b(c) = Val("&H" & Mid(strVal, c * 2 + 1, 2))
Next


'Debug.Print b(0), b(1), b(2), b(3)

CopyMemory dblVal, b(0), 4

'Debug.Print dblVal
sDec = dblVal


End Function
'hold on, third time lucky maybe

Private Sub Command2_Click()

   Dim bytes(0 To 3)
   Dim c As Long
   Dim x As String
   
   x = sHex(3.14159, bytes)
   For c = 0 To 3
       Print bytes(c)
   Next
   Print x
   

End Sub

'in a .bas

Public Function sHex(sngVal As Single, bytearray) As String
'single variable ONLY to hex
Dim c As Long, dHex2 As String
Dim sVal As Single, sTemp As String

sVal = CSng(sngVal)

Dim b(0 To 3) As Byte

Dim z As Single

CopyMemory b(0), sVal, 4

'Debug.Print b(0), b(1), b(2), b(3)

For c = 0 To 3

    sTemp = Hex(b(c))
    sTemp = String(2 - Len(sTemp), "0") & sTemp
    dHex2 = dHex2 & sTemp
    bytearray(c) = b(c)
   
Next
sHex = dHex2

For c = 1 To 4
    If Mid(dHex2, c, 1) = "0" Then
        sHex = Mid(sHex, 2)
    Else
        Exit For
    End If
Next
If sHex = "" Then sHex = "0"
   

End Function
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of clylv
clylv

ASKER

THANKYOU PaulHews AND deighton!!

both gave me EXCELENT answers and fast ones! I would like to split the points between u two but since I have to choose the vote will fall on Paul's as his answer has less code. Thanx!

Regards
Claes Ylving
Avatar of clylv

ASKER

THANKYOU PaulHews AND deighton!!

both gave me EXCELENT answers and fast ones! I would like to split the points between u two but since I have to choose the vote will fall on Paul's as his answer has less code. Thanx!

Regards
Claes Ylving
Avatar of clylv

ASKER

THANKYOU PaulHews AND deighton!!

both gave me EXCELENT answers and fast ones! I would like to split the points between u two but since I have to choose the vote will fall on Paul's as his answer has less code. Thanx!

Regards
Claes Ylving