Hex String to Integer

Popular in previous forums place your programming snippets here
Post Reply
User avatar
Zenerdiode
Posts: 81
Joined: Wed Jan 04, 2023 12:45 am
Location: Newcastle, England

Hex String to Integer

Post by Zenerdiode »

On the LZ we have the HEX$ function for returning a hexadecimal string representation of an integer - but not for the other way round. Here’s a function to do just that. Caveats are the hex letters must be in uppercase and string length must be four characters.

INTHEX%:
Syntax: x%=INTHEX%:(h$)
Returns a two byte integer value of the hexadecimal string expression in the brackets. For example: INTHEX%:(“20CD”) will return 8397.

Code: Select all

INTHEX%:(HX$)
LOCAL IN%(4),I%,M%,H$(1)
I%=1
DO
 H$=MID$(HX$,I%,1)
 IF ASC(H$) > 64 AND ASC(H$) < 71
  M%=ASC(H$) - 55
 ELSE
  M%=ASC(H$) - 48
 ENDIF
 IN%(I%)=M%
 I%=I%+1
UNTIL I%=5
RETURN (IN%(1)*4096)+(IN%(2)*256)+(IN%(3)*16)+IN%(4)
Christopher. - Check out my TRAP message, it’s not difficult to decode and is sometimes uttered under the breath when said message appears… :|
Post Reply