Codesnippets > Math

Hex To Decimal and Decimal to Hex

<< < (2/3) > >>

ketil:
Ahhh ... didn't think about that.

Qedo:
the old 2009 hexToDec function fails with value xFFFFFFFF because it returns value -1 instead of 4294967295.
This instead is the one that returns the correct value. For those who had used the old one please check
ad maiora

FUNCTION hexToDec#:_HEX$
LOCAL i%=0
LOCAL j#=0
LOCAL loop%
   _HEX$=UCASE$(_HEX$)
   LOCAL start%=LEN(_HEX$)-1
   FOR loop%=start TO 0 STEP-1
      i=ASC(MID$(_HEX$,loop,1))-48   //0=48 A=65
      IF i>9
         DEC i,7
      ENDIF
      j=j+i*POW(16,start-loop)
   NEXT
RETURN j
ENDFUNCTION

Kitty Hello:
There is a builtins HEX$(), are you aware of that?

Qedo:
HEX$() ? I thought it only transforms from decimal to hexadecimal.
Or am I wrong?
I need the opposite. HEX2DEC

Kitty Hello:
Ah. Try this:

--- Code: (glbasic) ---IMPORT "C" int strtol(const char* str, int endptr, int base)
LOCAL str$ = "ff"
LOCAL n% = strtol(str$, 0, 16)

--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version