Hex numbers

Blitz3D Forums/Blitz3D Programming/Hex numbers

Hey guys, I'm not a math guy so this one is a bit beyond me. How do I convert a Hexidecimal number to an integer. I see there's a command to do the opposite but I have a the hex code, I just need the number it represents.

A hex number is just a number in base-16. Blitz can do the conversion to base-10 for you. e.g.:
Print $ff ; 255


...or are you talking about converting from a string representation of a hex number to an integer? I'm not sure if there's a Blitz function for converting a hex number as a string to a decimal integer. But you probably don't need that since you say you already have the hex number.

with win calculater, you can put it in scientific mode, switch to hex mode, punch in your code, and switch back to decimal. voila. the number is converted.

Yep, looks like the number is in a string. Wonder how I can get the blitz code to convert. I'm pulling a list of hex numbers in a text file that have been generated by another program.

http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=218
http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=219

$1=1
$2=2
$3=3
$4=4
$5=5
$6=6
$7=7
$8=8
$9=9
$a=10
$b=11
$c=12
$d=13
$e=14
$f=15

$0b = 11 b-coz ($00=0x16)+($b=11) = ( 0+11) = 11
$13 = 19 b-coz ($10=1x16)+($3=03) = ( 16+03) = 19
$44 = 68 b-coz ($40=4x16)+($4=04) = ( 64+04) = 68
$a3 = 163 b-coz ($a0=10x16)+($3=03) = (160+03) = 163
$ff = 255 b-coz ($f0=16x16)+($f=15) = (240+15) = 255

$6e4a = 28234 b-coz ($6000=6x4096)+($e000=14x256)+($40=4x16))+($a=10) = (24576+3584+64+10) = 28234

OK ?

Function hex2dec(hexin$)
	Local c, dec, hexval$ = "0123456789ABCDEF"
	For c=1 To Len(hexin$)
		dec = (dec Shl 4) Or (Instr(hexval$, Upper$(Mid$(hexin$, c, 1))) - 1)
	Next
	Return dec
End Function
YAN

Print hex2int("0FFFF")
WaitKey()
End

Function hex2int(n$)
 Local hva$="0123456789ABCDEF",a,dig
 For i=Len(n$) To 1 Step -1
  b$=Upper$(Mid$(n$,i,1))
  For i2=0 To 15
   If b$=Mid$(hva$,i2+1,1)
    a=a+ i2 *(16^dig)
   EndIf
  Next
  dig=dig+1
 Next
 Return a
End Function


yan - again I am too late! ;) yours is very efficient btw.

I'm extremely lazy and try to type as little as possible ;o)


YAN