Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert to hex ??
Message
From
21/07/2008 22:17:03
 
 
To
21/07/2008 21:24:57
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 8 SP1
Miscellaneous
Thread ID:
01332438
Message ID:
01333019
Views:
14
>Your suggestion works very well with positive number (90% of database) but negative numbers don't show the right result.
>eg : -77442022681 should show up as EDF81882E7. Your help will be appreciated. TIA

From the wiki link I gave you in a previous post, modified to work with negative numbers, the way you want it:
?basex(249272136572 , "0123456789abcdef")
?basex(-77442022681, "0123456789abcdef")

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"
		If txVal < 0 Then
			txVal = txVal + 1
		Endif
		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 )
				If txVal > 0 Then
					lcDig = Substr( tcDom, lnDig+1, 1 )
				Else
					If lnDig = 0 Then
						lnDig = 16
					Endif
					lcDig = Substr( tcDom, lnDig, 1 )
				Endif

				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
Carlos Alloatti
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform