Skip to content

Instantly share code, notes, and snippets.

@CarlTBarnes
Last active July 13, 2022 18:34
Show Gist options
  • Save CarlTBarnes/a05f6318fd1c7eece9802b0aacda4e3d to your computer and use it in GitHub Desktop.
Save CarlTBarnes/a05f6318fd1c7eece9802b0aacda4e3d to your computer and use it in GitHub Desktop.
Hex conversion from LONG
MAP
Hex8(LONG Lng),STRING !Return 8 digit hex with trailing 'h'
HexFmt(LONG Lng),STRING !Return minimum hex pairs w/o 'h'
HexDigits(ULONG Lng),STRING !Return minimum Hex Digits, may be odd
HexPairs(ULONG Lng),STRING !Return minimum Hex Pairs
HexClarion(ULONG Lng),STRING !Clarion Valid Hex with trailing 'h', plus leading '0' for A-F
END
!=================================
Hex8 PROCEDURE(LONG Lng)!,STRING !Return 8 digit hex with trailing 'h'
LngAdj LONG,AUTO
L BYTE,DIM(4),OVER(LngAdj)
Dgt STRING('0123456789ABCDEF'),STATIC
HX STRING(9),AUTO
CODE
LngAdj = BAND(BSHIFT(Lng, -4),0F0F0F0Fh) + 01010101h
HX[1]=Dgt[L[4]] ; HX[3]=Dgt[L[3]] ; HX[5]=Dgt[L[2]] ; HX[7]=Dgt[L[1]]
LngAdj=BAND(Lng,0F0F0F0Fh) + 01010101h
HX[2]=Dgt[L[4]] ; HX[4]=Dgt[L[3]] ; HX[6]=Dgt[L[2]] ; HX[8]=Dgt[L[1]]
HX[9]='h'
RETURN HX
!=================================
HexFmt PROCEDURE(LONG Lng)!,STRING !Return minimum hex pairs w/o 'h'
LngAdj LONG,AUTO
L BYTE,DIM(4),OVER(LngAdj)
Dgt STRING('0123456789ABCDEF'),STATIC
HX STRING(8),AUTO
CODE
LngAdj = BAND(BSHIFT(Lng, -4),0F0F0F0Fh) + 01010101h
HX[1]=Dgt[L[4]] ; HX[3]=Dgt[L[3]] ; HX[5]=Dgt[L[2]] ; HX[7]=Dgt[L[1]]
LngAdj=BAND(Lng,0F0F0F0Fh) + 01010101h
HX[2]=Dgt[L[4]] ; HX[4]=Dgt[L[3]] ; HX[6]=Dgt[L[2]] ; HX[8]=Dgt[L[1]]
CASE Lng !Return minimum pairs
OF 0 TO 0FFh ; RETURN Hx[7:8]
OF 100h TO 0FFFFh ; RETURN Hx[5:8]
OF 10000h TO 0FFFFFFh ; RETURN Hx[3:8]
END
RETURN HX
!=================================
!Below loop to return minimum Hex using a Loop. The above HexFmt is faster but more code.
HexDigits PROCEDURE(ULONG Lng)!,STRING !Return Integer as minimum Hex Digits, may be odd
HX PSTRING(9)
Dgt STRING('0123456789ABCDEF')
CODE
LOOP
HX = Dgt[1+BAND(Lng,0Fh)] & HX
Lng = BSHIFT(Lng,-4)
UNTIL ~Lng
RETURN HX
HexPairs PROCEDURE(ULONG Lng)!,STRING !Return Integer as minimum Hex Pairs
HX PSTRING(9)
Dgt STRING('0123456789ABCDEF')
CODE
LOOP
HX = Dgt[1+BAND(BSHIFT(Lng,-4),0Fh)] & Dgt[1+BAND(Lng,0Fh)] & HX
Lng = BSHIFT(Lng,-8)
UNTIL ~Lng
RETURN HX
!=================================
! Clarion valid Hex Number with trailing 'h' and leading '0' for A-F
HexClarion PROCEDURE(ULONG Lng)!,STRING !Clarion Valid Hex with trailing 'h', plus leading '0' for A-F
HX PSTRING(10)
Dgt STRING('0123456789ABCDEF')
CODE
LOOP
HX = Dgt[1+BAND(Lng,0Fh)] & HX
Lng = BSHIFT(Lng,-4)
UNTIL ~Lng
IF HX[1] >= 'A' THEN HX='0' & HX. !A-F Req leading Zero
RETURN HX &'h'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment