Solved

Single to Byte HELP!

Posted on 2001-08-10
8
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

735 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