scripting@wanadoo.nl




CnvtBase()

http://www.scriptlogic.com/kixtart/FunctionLibrary_FunctionList.aspx


actual FunctionLibrary list on 'Scriptlogic' site printer-friendly mirror of UDF topic on 'scripting@wanadoo.nl' site close
5 February 2002 CnvtBase()
AuthorW.M. Hinsch (New Mexico Mark)
Action Converts numbers from any base (2-36) to any base (2-36). i.e. base 2=binary, 8=octal, 10=decimal, 16=hex, etc.
SyntaxCnvtBase (number, [oldbase], [newbase]) 
Parameters
  • number (Required / String) The number (passed as a string) to convert from.
  • oldbase (Required / String) The base that number is in. (If not supplied or out of range, cnvtbase will try to "guess" whether it is binary, octal, decimal, hexadecimal, or some base between 17 and 36).
  • newbase (Required / String) The base to which the number will be converted. If not supplied or out of range, decimal (base 10) will be used. 
  • RemarksUse this function to convert among several bases. I.e. when a number must be represented in decimal, hex, and binary formats. I originally wrote this as a training exercise when learning a different programming language, but it has proven helpful in many cases, so I've converted the logic to KiXtart. The key information contained is the generalized algorithm for converting to base 10 and to other bases containing alpha characters. It also demonstrates the need for knowing which data type you are working with at any given time! KiXtart is pretty flexible, but it can't read minds.
    "Number" is a string representing a number, base 2 - 36. Oldbase and newbase may be any number 2 - 36. If oldbase is not supplied, the function will "guess" the old base as base 2, 8, 10, 16, or 17-36 depending on the highest value "digit" contained in the string. If the string is prepended with a hyphen "-" (i.e. negative number), the hyphen will be preserved in the return string.

    IMPORTANT: The function returns the converted number as a STRING in order to correctly handle bases greater than 10.

    One final note: The function will return a "0" if the supplied number string contains any non-alphanumeric characters besides a leading hyphen (-). This means the function could even be used to test whether a string is alphanumeric.
    Unusual behaviors: This function will produce unreliable results for numbers that evaluate to a base 10 number outside the range of -2,147,483,647 to 2,147,483,647. Also: If newbase is not supplied or is out of range (< 2 or > 36), the function will convert to decimal (base 10). 
    ReturnsThe function returns a string representing the number in the new base specified (or base 10 if none specified). 
    DependenciesNone. 
    Examples
    CnvtBase("5A23")             ; Returns "23075" (base 16 to base 10)
    CnvtBase("1010")             ; Returns "10" (base 2 to base 10)
    CnvtBase("1010",10,2)        ; Returns "1111110010" (base 10 to base 2)
    CnvtBase("54321",10,16)      ; Returns "D431" (base 10 to base 16)
    CnvtBase("T#NST##FL")        ; Returns "0" (The supplied string is not alphanumeric)
    $strTest = CnvtBase("54321") ; $strTest will contain "22737" (converted octal to decimal.)
    
    ; -- Code Example: --
    ? "Please supply an integer to convert to Hex, Octal, and Binary: "
    gets $TempStr
    
    "Hex value: " + CnvtBase($TempStr,10,16) ?
    "Octal value: " + CnvtBase($TempStr,10,8) ?
    "Binary value: " + CnvtBase($TempStr,10,2) ?
    "And just for fun... trinary value: " + CnvtBase($TempStr,10,3) ?
    sleep 5
    
    -- Return screen from above Code Example when 1024 is entered: --
    
    Please supply an integer to convert to Hex, Octal, and Binary: 1024
    Hex value: 400
    Octal value: 2000
    Binary value: 10000000000
    And just for fun... trinary value: 1101221
    
     
    Source
    FUNCTION CnvtBase ($strnum, OPTIONAL $intob, OPTIONAL $intnb)
      ;*********************************************************
      ;* Function name..: CnvtBase()
      ;* Author.........: W.M. Hinsch (wmarkh@aol.com)
      ;* Created........: 2001/03/19
      ;* Modified.......: 2001/04/27
      ;* Called by......: None
      ;* Calls..........: None
      ;* Comment........: Converts any base (2-36)
      ;                   into any other base (2-36)
      ;*********************************************************
      ; Limit the scope of variables to this function...
      ; these should probably be retained even if the code is trimmed.
    
      DIM $stralpha,$strnew,$strdigit,$stroldnum,$strsign
      DIM $intpos,$intdigval,$intb10,$intplcnum,$intctr
      DIM $intoldbase,$intnewbase,$varremain
    
      ; Assign parameters to local variables. This does some basic error-
      ; prevention by forcing the variables to the correct data type.
      $stroldnum = "" + $strnum ; cast to string
      $intoldbase = 0 + $intob ; cast to number
      $intnewbase = 0 + $intnb ; cast to number
    
      ; Initialize variables. The only variables which really need to be
      ; initialized are $strAlpha and $intPlcNum. The rest are for clarity
      ; and to make converting to other languages easier.
      $intpos = 0 ; Position starting at LSD to MSD
      $intdigval = 0 ; Base 10 Value of digit
      $intb10 = 0 ; Base 10 Number
      $intplcnum = 1 ; Value of oldbase raised to intPos power
      $stralpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      $strnew = "" ; "Number" in new base
      $strsign = "" ; Holds the leading hyphen (-) from strOldNum
      $strdigit = "" ; "Digit" at intPos, including Alpha characters
    
      ; If negative numbers will not be used, this may be eliminated.
      IF Substr($stroldnum,1,1) = "-"
        $strsign = Substr($stroldnum,1,1)
        $stroldnum = Substr($stroldnum,2,Len($stroldnum) - 1)
      ELSE
        $strsign = ""
      ENDIF
    
      ; This loop checks to make sure all the characters in the supplied
      ; number are alphanumeric. If not, the function exits with a 0 return.
      FOR $intctr = 1 TO Len($stroldnum)
        $strdigit = Substr($stroldnum,$intctr,1)
        IF InStr($stralpha,$strdigit) = 0
          $strnew = 0
          GOTO func_end
        ENDIF
      NEXT
    
      ; This portion attempts to "guess" the base of the number supplied.
      ; It also handles a bad range supplied for the new base.
      ; If this will be explicitly given at all times, this portion may
      ; be eliminated.
      FOR $intctr = 1 TO 36
        $strdigit = Substr($stralpha,$intctr,1)
        IF InStr($stroldnum,$strdigit)
          $intob = $intctr ; set to the highest digit found so far
        ENDIF
      NEXT
      IF ($intoldbase < 2) OR ($intoldbase > 36)
        SELECT
        CASE ($intob <= 2)
          $intoldbase = 2
        CASE ($intob <= 8)
          $intoldbase = 8
        CASE ($intob <= 10)
          $intoldbase = 10
        CASE ($intob <= 16)
          $intoldbase = 16
        CASE 1
          $intoldbase = $intob
        ENDSELECT
      ENDIF
    
      ; If the user does not specify a new base, or if the new base is
      ; out of range, base 10 will be used as the default.
      IF ($intnewbase < 2) OR ($intnewbase > 36)
        $intnewbase = 10
      ENDIF
    :to_base_10
      $strdigit = Substr("$strOldNum",Len("$strOldNum") - $intpos,1)
      WHILE ("$strDigit" <> "")
        $intdigval = InStr($stralpha,"$strDigit") - 1
        $intb10 = $intb10 + ($intdigval * $intplcnum)
        $intplcnum = $intplcnum * $intoldbase
        $intpos = $intpos + 1
        $strdigit = Substr("$strOldNum",Len("$strOldNum") - $intpos,1)
      LOOP
    :to_new_base
      WHILE ($intb10 > 0)
        ; The following line emulates a "$intB10 mod intNewBase" expression
        $varremain = $intb10 - (($intb10 / $intnewbase) * $intnewbase)
        ; This puts the remainder digit in the correct format for bases > 10
        $varremain = Substr($stralpha,$varremain + 1,1)
        $intb10 = $intb10 / $intnewbase
        $strnew = "$varRemain" + "$strNew"
      LOOP
    :func_end
      $cnvtbase = $strsign + $strnew
    ENDFUNCTION ; - CnvtBase -
     
      original source of UDF topic. show actual FunctionLibrary list on Scriptlogic site close top
              printer-friendly mirror of UDF topic on scripting@wanadoo.nl site  




    Copyright © 2003 www.scriptlogic.com & scripting@wanadoo.nl - last updated on 20 May 2003


    Site Meter