How to convert decimal to any base

johnpc

Member
Licensed User
Longtime User
How can a decimal input be converted to Hex, Octal,Binary or any Base.
I can do this in VB6 but can't seen to in BASIC4PPC. Problem lies in
trying to use the String Mid Function as given below.

Sub btnCalc_Click
Dim HexString
Dim DecValue

HexString = ""
DecValue = txtInput.Text

Do While DecValue > 0
'Get least significant character
HexString = mid("0123456789ABCDEF",Mod(DecValue,16 )+ 1,1) + HexString
'Remove least significant character

DecValue = Int(DecValue/16)
Loop

If len(HexString) < 2 Then
Decimal2Hex = "&H" + Right("00" + HexString,2)
Else
Decimal2Hex = "&H" + HexString
End If

txtOutput.Text = Decimal2Hex

End Sub
Any Help will be appreciated
 

specci48

Well-Known Member
Licensed User
Longtime User
Hello johnpc,

a conversion between decimal, hexadecimal and binary values can easily be done with the bitwise.dll.


specci48
 

pdablue

Active Member
Licensed User
Longtime User
Binary, Decimal, Hexadecimal Conversion.

Hi,

There is a Binary, Decimal, Hexadecimal conversion
program example listed in the "Share Your Creations"
section of the B4PPC Forums.

This program uses the Bitwise.dll.

Check out the postings by PDABLUE.
 

Zenerdiode

Active Member
Licensed User
To answer questions about your code:

B4X:
...
HexString = [COLOR="Red"]mid[/COLOR]("0123456789ABCDEF",Mod(DecValue,16 )+ 1,1) + HexString
If [COLOR="Red"]len[/COLOR](HexString) < 2 Then
Decimal2Hex = "&H" + [COLOR="Red"]Right[/COLOR]("00" + HexString,2)
mid, len and right are not functions of Basic4PPC. you should use StrAt or SubString and StrLength
 
Top