Posted: 27th Oct 2011 14:11
Hi

I've a question about how to convert a hex string back to integer in AppGameKit?

I use HEX(number) to get a hex string from a given number. But how do I convert it back? Is there a command for that? Or do I've to recalculate it the long way back?
Posted: 27th Oct 2011 14:25
Here's what I use at the moment. What do you think, is it correct?

+ Code Snippet
Function HexToInt(hexdata$)

    If Len(hexdata$) > 0

        Result = 0
        tmpVal = 0

        For i = 1 To Len(hexdata$)

            tmpCurValVal = 0

            tmpCurVal$ = Upper(Mid(hexdata$,i,1))
            Select tmpCurVal$
                Case "0"
                    tmpCurValVal = 0
                EndCase
                Case "1"
                    tmpCurValVal = 1
                EndCase
                Case "2"
                    tmpCurValVal = 2
                EndCase
                Case "3"
                    tmpCurValVal = 3
                EndCase
                Case "4"
                    tmpCurValVal = 4
                EndCase
                Case "5"
                    tmpCurValVal = 5
                EndCase
                Case "6"
                    tmpCurValVal = 6
                EndCase
                Case "7"
                    tmpCurValVal = 7
                EndCase
                Case "8"
                    tmpCurValVal = 8
                EndCase
                Case "9"
                    tmpCurValVal = 9
                EndCase
                Case "A"
                    tmpCurValVal = 10
                EndCase
                Case "B"
                    tmpCurValVal = 11
                EndCase
                Case "C"
                    tmpCurValVal = 12
                EndCase
                Case "D"
                    tmpCurValVal = 13
                EndCase
                Case "E"
                    tmpCurValVal = 14
                EndCase
                Case "F"
                    tmpCurValVal = 15
                EndCase
            EndSelect

            tmpVal = tmpCurValVal + (tmpVal * 16)

            Result = tmpVal

        Next i

    Else
        Result = 0
    EndIf

EndFunction Result
Posted: 27th Oct 2011 17:38
Hi

Take x numbers:

example1: AF1

then

AF1 = (A * 16^2) + (F*16^1) + 1 = 2801 (dec)

example2: AAF1

then

AAF1 = (A * 16^3) + (A * 16^2) + (F*16^1) + 1 = 2801 (dec)


- Get the place of the digit. May be a For?

- (If) it's a letter, (then) translate it to it's corresponding value (else) calculate the number.

- Apply the formula.

See you.
Posted: 28th Oct 2011 4:56
All you need to do is grab the character and convert it ASCII.. Which gets rid of the string thrashing from the original loop.

(PB code.. but the principal is the same, will need re-ordering for the most optimal loop)

+ Code Snippet
print HexToDec("8")
print HexToDec("A")
print HexToDec("ff")
print HexToDec("ff00")
print hex$(HexToDec("12345678"))

sync
waitkey



Function HexToDec(HexString$)
	
	if Len(HexString$)
		HexString$=Upper$(HexString$)	

		For lp=1 to len(HexString$)
			Result=Result*16
			ThisChr=asc(Mid$(HexString$,lp,1))
		
			if ThisChr=>asc("0") and ThisChr<=asc("9")
				Result=REsult+(ThisChr-asc("0"))
				Continue		
			endif 

			if ThisChr=>asc("A") and ThisChr<=asc("F")
				Result=REsult+ThisChr-(asc("A")-10)		
			endif 

	   next
	endif	
EndFunction REsult

Posted: 28th Oct 2011 9:38
Hi Kevin, thanks, that loop looks quite optimized. So I'll use this one now. Thanks again!
Posted: 28th Oct 2011 18:10
This from the help on Val(), implying that you can just do the conversion with one command.

BIG EDIT - this command works just as expected. In fact, it is not limited to hex, binary, octal or decimal. Try it in base 5, or whatever. Neat.

+ Code Snippet
a$ ="20"

do
    print(val(a$, 8))
    print(val(a$, 10))
    print(val(a$, 16))
    print(val(a$, 5))
    Sync()
loop




"
Posted: 28th Oct 2011 19:08
Nice! Thank you. I saw the "base" in the VAL description but couldn't understand what the base is for Should be included in the manual.