Wiki Home

Base X


Namespace: Wiki
Converts a number to a string representation of that number
* Program BaseX
* Demos function cBaseX()

for lnI = 0 to 50
	? lni, ;
	lcBase2( lnI ),  ;
	lcBase8( lnI ), ;
	lcBase10( lnI ), ;
	lcBase16( lnI ), ;
	lcBase62( lnI ), ;
	lcBaseAlpha( lnI ), ;
	lcBaseCarl( lnI )
endfor

? lcBase16( 16777215 )  && returns FFFFFF, the HTML code for white.

return

function lcBase2( tnVal )
return padl( lcBaseX( tnVal, "01" ), 16, 'o' )  && o not 0 to emphasize the pad

function lcBase8( tnVal )
return lcBaseX( tnVal, "01234567" )

function lcBase10( tnVal )
return lcBaseX( tnVal, "0123456789" )

function lcBase16( tnVal )
return lcBaseX( tnVal, "0123456789abcdef" )

function lcBase62( tnVal )
return lcBaseX( tnVal, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" )

function lcBaseAlpha( tnVal )
* 1 returns 'A', 26:'Z', 27:'AA', 28: 'AB'... 'AZ', 'BA'... which is how spread sheats lable columns.
return lcBaseX( tnVal, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" )

function lcBaseCarl( tnVal )
return padl( lcBaseX( tnVal, "Carl" ), 6, 'x' )

function lcBaseX( tnVal, tcDom )
* Converts a number to a string representation of that number
* tnVal - Number
* tcDom - The domain of characters to be used
* typical Domains are:
* "01" - Binary
* "0123456789" - Decimal
* "0123456789abcdef" - Hex
* "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" - Base62
* "23456789ABCDEFGHJKLMNPRSTUVWXYZ" - No {01IOQ} - they might confuse a user.

* If you want the result padded, use PADL()

local ;
	lnVal, ;
	lnDomSiz, ;
	lcRet

lnVal = tnVal
lnDomSiz = len( tcDom )

* Humans get restless if the value zero is displayed as an empty string (blank).
* The first char of the domain (generally 0) is normally used as a place holder,
* but in the case of the value zero, it fills in to keep the peace.
* This may have lead to the fall of Rome.

if lnVal = 0
	lcRet = substr( tcDom, 1, 1 )
else

	lcRet = ''

	do while lnVal <> 0

		lnDig = lnVal % lnDomSiz
		lnVal = int( lnVal/lnDomSiz )
		lcDig = substr( tcDom, lnDig+1, 1 )
		lcRet = lcDig + lcRet

	enddo

endif

return lcRet

Here's a different version that allows you to convert the string back to numeric.
FUNCTION BaseX( txVal, tcDom )
  * Converts a number to a string representation of that number
  * tnVal - Number
  * tcDom - The domain of characters to be used
  * typical Domains are:
  * "01" - Binary
  * "0123456789" - Decimal
  * "0123456789abcdef" - Hex
  * "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" - Base62
  * "23456789ABCDEFGHJKLMNPRSTUVWXYZ" - No {01IOQ} - they might confuse a user.

  * If you want the result padded, use PADL()

  LOCAL ;
    lnVal, ;
    lnDomSiz, ;
    lcRet, ;
    lnPosition, ;
    lnPlace

  IF VARTYPE(txVal) = "N"
    lnVal = txVal
    lnDomSiz = LEN( tcDom )

    * Humans get restless if the value zero is displayed as an empty string (blank).
    * The first char of the domain (generally 0) is normally used as a place holder,
    * but in the case of the value zero, it fills in to keep the peace.
    * This may have lead to the fall of Rome.

    IF lnVal = 0
      lcRet = SUBSTR( tcDom, 1, 1 )
    ELSE
      lcRet = ''
      DO WHILE lnVal <> 0
        lnDig = lnVal % lnDomSiz
        lnVal = INT( lnVal/lnDomSiz )
        lcDig = SUBSTR( tcDom, lnDig+1, 1 )
        lcRet = lcDig + lcRet
      ENDDO
    ENDIF
    lxRet=lcRet
  ELSE
    * Convert it back to decimal
    lnVal = 0
    lnPlace = 0
    FOR lnPosition = LEN(txVal) TO 1 STEP -1
      lnVal = lnVal + ;
        (AT( SUBSTR(txVal,lnPosition,1), tcDom)-1) * (LEN(tcDom)^lnPlace)
      lnPlace=lnPlace+1
    ENDFOR
    lxRet = lnVal
  ENDIF

  RETURN lxRet
ENDFUNC

The BINTOC() and CTOBIN() built-in functions to create 1, 2 or 4 byte strings from numbers. These functions will execute much faster.
For hex, here are two little known features of VFP:
? TRANSFORM(9999,"@0")  && 0x0000270F
? Val( "0xFFFF" ))      && 65535.0
? 0xFFFF                && 65535


Contributors Carl Karsten
Yes, BINTOC() and CTOBIN() built-in functions are much faster, but you will have some problems if you use them with some controls, like combobox.
As a example, you have a combobox, with column1 some text for the user, and column2 with the register id as a char(4). If you get this id by BINTOC(), combobox gets crazy vith some controls chars. You will need a function like above to use only secure chars in the conversion.
Category Code Samples Category UDF
( Topic last updated: 2006.10.20 10:24:29 AM )