Solved

Single to Byte HELP!

Posted on 2001-08-10
8
171 Views
Last Modified: 2008-03-03
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
0
Comment
Question by:clylv
  • 3
  • 3
  • 2
8 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 6372119
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
0
 
LVL 18

Expert Comment

by:deighton
ID: 6372132
'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
0
 
LVL 18

Expert Comment

by:deighton
ID: 6372151
'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
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 100 total points
ID: 6372263
Why all the dancing around with strings deighton?  Just make sure the byte array has 4 elements or you
can crash nicely.

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

cbCopy As Long)

public sub SngToByte(sngIn as single, bytOut() as byte)

 copymemory bytOut(lbound(bytout)), sngIn, 4

end sub

public sub ByteToSng(sngOut as single, bytIn() as byte)

 copymemory sngOut, bytIn(lbound(bytIn)), 4

end sub
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:clylv
ID: 6372433
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
0
 

Author Comment

by:clylv
ID: 6372462
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
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6372495
0
 

Author Comment

by:clylv
ID: 6376866
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
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now