Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary to Decimal

Status
Not open for further replies.

moonbase

Programmer
Nov 18, 2002
57
0
0
GB
I need to convert a binary number, read from a text file, to it's decimal equivalent. It's in two's complement format, i.e can be negative.
Does anybody have a suitable routine or can anyone point me in the right direction?

Many thanks
 
Thanks, this is along the right lines but when a say the right most byte is 5F, I don't mean a string of 2 ascii characters, I mean one byte, 8 bits. This can represented in this discussion as hex 5F or binary 0101 1111.
To get your idea to work I need to create an ascii string of 2 characters "5" and "F" from the byte. How do I do that?
The next problem is the number to be converted consists of 8 bytes, e.g. 00 00 00 00 00 00 00 5F.
Thanks
 
Right, so you've got an 8-byte 2s-complement number? Big-endian? Little-endian?
 
Okay, I'm close, but it'll give you a starting point. Read each set of eight bytes into a byte array and process:
Code:
    Dim i As Integer
    Dim dec As Double
    Dim mult As Double
    Dim dbyte As Double
    Dim bytes(8) As Byte
    
'   Largest positive number 

    bytes(1) = 127
    bytes(2) = 255
    bytes(3) = 255
    bytes(4) = 255
    bytes(5) = 255
    bytes(6) = 255
    bytes(7) = 255
    bytes(8) = 255
    
'   Largest negative number 

'    bytes(1) = 128
'    bytes(2) = 0
'    bytes(3) = 0
'    bytes(4) = 0
'    bytes(5) = 0
'    bytes(6) = 0
'    bytes(7) = 0
'    bytes(8) = 0
    
    If (bytes(1) And 128) = 128 Then
        bytes(1) = bytes(1) And 127
        dec = -2 ^ 63
    Else
        dec = 0
    End If
    For i = 1 To 8
        mult = 256 ^ (8 - i)
        dbyte = bytes(i)
        dec = dec + dbyte * mult
    Next
    Debug.Print Format(dec, "0000000000000000000")

See this link for an explanation:
I took a shortcut multiplying each byte by a power of 256, so the max positive and min negative come out to be the same. The max positive should be 2^63-1 and min negative -2^63.


"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Thanks for the help guys. It actually turned out to be fairly straightforward once I'd got my head round it. But I'd have struggled without your help. This is what I ended up with:
Code:
Public Function Hex2Currency(HexCurrency As String) As Currency

    Dim s As String
    Dim i As Integer

    ' capture error
    On Error GoTo ErrorHandler
    SaveErrNum = 0

    ' intitialise
    s = "&H"
    
    ' create hex string from each char
    For i = 1 To Len(HexCurrency)
        
         ' high order nibble
        s = s & Hex((Asc(Mid(HexCurrency, i, 1)) And &HF0) / 16)
        
        ' low order nibble
        s = s & Hex(Asc(Mid(HexCurrency, i, 1)) And &HF)
        
    Next i
    
    ' hex to currency
    Hex2Currency = CCur(s)

    Exit Function
    
ErrorHandler:
    Hex2Currency = 0
    SaveErrNum = 1

End Function
As suggested, negative numbers can be handled by subtracting from the highest value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top