Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Single to Byte HELP!

Posted on 2001-08-10
8
Medium Priority
?
181 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 38

Accepted Solution

by:
PaulHews earned 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

971 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