Solved

Single to Byte HELP!

Posted on 2001-08-10
8
173 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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
 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

770 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