Int To Hex

Monkey Forums/Monkey Code/Int To Hex

AdamRedwoods(Posted 2012) [#1]
Integer To Hex String

[monkeycode]

Function Main()

Print ToHex(0)
Print ToHex(1)
Print ToHex(-1)
Print ToHex(4)
Print ToHex(-4)
Print ToHex(64)
Print ToHex(128)
Print ToHex($ff0f0f0f)

End

Function ToHex:String(i:Int)

''p=32-bit
Local r%=i, s%, p%=32, n:Int[p/4+1]

While (p>0)

s = (r&$f)+48
If s>57 Then s+=7

p-=4
n[p Shr 2] = s
r = r Shr 4

Wend

Return String.FromChars(n)

End
[/monkeycode]


EdzUp(Posted 2013) [#2]
Hex To Dec function :D

	Method HexToDec:Int( Hex:String )
		Local Value:Int = 0
		Local Char:Int =0
		
		If Hex.Length() <8
			'add 0's to beginning of thing so FF becomes 000000FF
			Local TempHex:String = ""
			For Char =Hex.Length() To 7
				TempHex += "0"
			Next
			TempHex += Hex
			Hex = TempHex
		Endif
		
		Local Conversion:Int = 1		'starts at 1 then 16, 256, 4096, 
		'now the characters are 'eight bytes' now begin conversion
		For Char=7 To 0 Step -1
			If Hex[ Char ]<58
				Value += ( ( Hex[ Char ] -48 ) *Conversion )
			Else
				Value += ( ( Hex[ Char ] -55 ) *Conversion )
			Endif			
			Conversion *= 16	'multiply conversion by 16 for next byte
		Next
				
		Return Value
	End



Sub_Zero(Posted 2013) [#3]
Nice