How to convert decimal to any base

Discussion in 'Questions (Windows Mobile)' started by johnpc, Jun 4, 2009.

  1. johnpc

    johnpc Member Licensed 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
     
  2. specci48

    specci48 Well-Known Member Licensed User

    Hello johnpc,

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


    specci48
     
  3. pdablue

    pdablue Active Member Licensed 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.
     
  4. Zenerdiode

    Zenerdiode Active Member Licensed User

    To answer questions about your code:

    Code:
    ...
    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
     
  5. johnpc

    johnpc Member Licensed User

    Thank You all - Resolved:
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice