hextodec convert hex to decimal

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

hextodec convert hex to decimal

Post by idle »

convert hex strings to decimal, should work for any length.
ignores non hex chars so you could have a string like "$10 $20" or "0x1020"

Code: Select all

Structure m128 
  StructureUnion 
    a.a[16]
    u.u[8] 
    l.l[4] 
    q.q[2] 
  EndStructureUnion
EndStructure    

Structure m256 
  StructureUnion 
    a.a[32]
    u.u[16] 
    l.l[8] 
    q.q[4] 
  EndStructureUnion
EndStructure    

Procedure HEXTODEC(hex.s) 
  
  If hex <> "" 
    
    Protected *p.Unicode
    Protected  len = Len(hex)
    Protected *out = AllocateMemory(len)
    Protected *buff.Ascii = *out
    Protected *hex=@hex + (StringByteLength(hex)-2) 
    Protected ct,v 
    
    Static Dim lut(102) 
    If lut(48) <> 1
      lut(48)=1 
      lut(49)=2 
      lut(50)=3
      lut(51)=4 
      lut(52)=5 
      lut(53)=6
      lut(54)=7 
      lut(55)=8 
      lut(56)=9
      lut(57)=10 
      lut(65)=11 
      lut(66)=12 
      lut(67)=13
      lut(68)=14
      lut(69)=15
      lut(70)=16 
      lut(97)=11
      lut(98)=12 
      lut(99)=13
      lut(100)=14
      lut(101)=15
      lut(102)=16 
    EndIf  
    
    *p=*hex  
    While ct < Len  
      If (*p\u >= 48 And *p\u <= 102) 
        If lut (*p\u) <> 0 
          v = lut (*p\u)-1
          If (ct & 1) 
            *buff\a | v << 4   
            *buff + 1     
          Else 
            *buff\a | v
          EndIf   
          ct+1 
        Else 
          len+1
        EndIf 
      Else 
        len+1
      EndIf
      *p-2
      If *p\u = 0 
        Break 
      EndIf   
    Wend 
    
    ProcedureReturn ReAllocateMemory(*out,ct)    
    
  EndIf 
  
EndProcedure   

CompilerIf #PB_Compiler_IsMainFile 
  
  Global m128.s ="$1$2B0D868D117D7C8379DE50FA97A7b x ;GA0;"  ;ignores non hex chars 
  Global *m128.m128 = HEXTODEC(m128)  
  Global m256.s = "12B0D868D117D7C8379DE50FA97A7BA012B0D868D117D7C8379DE50FA97A7BAB"
  Global *m256.m256 =  HEXTODEC(m256)  
  
  Debug Hex(*m128\q[1]) + Hex(*m128\q[0])  
  Debug Hex(*m256\q[3]) + Hex(*m256\q[2]) + Hex(*m256\q[1]) + Hex(*m256\q[0]) 
  
  FreeMemory(*m128)
  FreeMemory(*m256)
  
  Debug HEXTODEC("") 
  
  Debug Hex(PeekQ(HexToDec("F")))		
  Debug Hex(PeekQ(HexToDec("DEF")))		
  Debug Hex(PeekQ(HexToDec("01234")))	
  Debug Hex(PeekQ(HexToDec("9abcd")))	
  Debug Hex(PeekQ(HexToDec("1FFFFFFFFFFFFFDC")))
  
CompilerEndIf 

User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: hextodec convert hex to decimal

Post by Caronte3D »

Thanks, idle :wink:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: hextodec convert hex to decimal

Post by RASHAD »

Hi idle
Perfect except you need to check if the string include only the hex alphabets or raise an error message
Thanks for posting :)

Try :

Code: Select all

Debug PeekQ(HexToDec("FG"))
Egypt my love
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: hextodec convert hex to decimal

Post by idle »

RASHAD wrote: Sat Jan 20, 2024 5:19 am Hi idle
Perfect except you need to check if the string include only the hex alphabets or raise an error message
Thanks for posting :)

Try :

Code: Select all

Debug PeekQ(HexToDec("FG"))
Good point.
At the expense of another if

Code: Select all

Structure m128 
  StructureUnion 
    a.a[16]
    u.u[8] 
    l.l[4] 
    q.q[2] 
  EndStructureUnion
EndStructure    

Structure m256 
  StructureUnion 
    a.a[32]
    u.u[16] 
    l.l[8] 
    q.q[4] 
  EndStructureUnion
EndStructure    

Procedure HEXTODEC(hex.s) 
  Protected *p.Unicode
  Protected  blen = (Len(hex)+1) >> 1  
  Protected  len = Len(hex)
  Protected *out = AllocateMemory(len)
  Protected *buff.Ascii = *out
  Protected *hex=@hex + (StringByteLength(hex)-2) 
  Protected ct,v 
  
  Static Dim lut(102) 
  If lut(48) <> 1
    lut(48)=1 
    lut(49)=2 
    lut(50)=3
    lut(51)=4 
    lut(52)=5 
    lut(53)=6
    lut(54)=7 
    lut(55)=8 
    lut(56)=9
    lut(57)=10 
    lut(65)=11 
    lut(66)=12 
    lut(67)=13
    lut(68)=14
    lut(69)=15
    lut(70)=16 
    lut(97)=11
    lut(98)=12 
    lut(99)=13
    lut(100)=14
    lut(101)=15
    lut(102)=16 
  EndIf  
  
  *p=*hex  
  While ct < Len  
    If (*p\u >= 48 And *p\u <= 102) 
      If lut (*p\u) <> 0 
        v = lut (*p\u)-1
        If (ct & 1) 
          *buff\a | v << 4  
          *buff + 1     
        Else 
          *buff\a | v
        EndIf   
        ct+1 
      Else 
        len+1
      EndIf 
    Else 
      len+1
    EndIf
    *p-2
    If *p\u = 0 
      Break 
    EndIf   
  Wend 
  *out = ReAllocateMemory(*out,ct) 
  ProcedureReturn *out      
  
EndProcedure   

CompilerIf #PB_Compiler_IsMainFile 
  
  Global m128.s ="$1$2B0D868D117D7C8379DE50FA97A7b x ;GA0;"  ;ignores non hex chars 
  Global *m128.m128 = HEXTODEC(m128)  
  Global m256.s = "12B0D868D117D7C8379DE50FA97A7BA012B0D868D117D7C8379DE50FA97A7BAB"
  Global *m256.m256 =  HEXTODEC(m256)  
  
  Debug Hex(*m128\q[1]) + Hex(*m128\q[0])  
  Debug Hex(*m256\q[3]) + Hex(*m256\q[2]) + Hex(*m256\q[1]) + Hex(*m256\q[0]) 
  
  FreeMemory(*m128)
  FreeMemory(*m256)
    
  Debug Hex(PeekQ(HexToDec("F")))		
  Debug Hex(PeekQ(HexToDec("DEF")))		
  Debug Hex(PeekQ(HexToDec("01234")))	
  Debug Hex(PeekQ(HexToDec("9abcd")))	
  Debug Hex(PeekQ(HexToDec("1FFFFFFFFFFFFFDC")))
  
CompilerEndIf 

AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: hextodec convert hex to decimal

Post by AZJIO »

I expect to receive the number 8454089960965228136567750901461387292011415048925916606819418534331473886123. Where can I see this?
https://i.imgur.com/8EYv33i.png

Code: Select all

If (*p\u >= 48 And *p\u <= 102)
	If lut (*p\u) <> 0
		
		
If (*p\u <= 102 And lut (*p\u) <> 0)
more

Code: Select all

  If lut('0') <> 1
    lut('0')=1 ; 48
    lut('1')=2 
    lut('2')=3
    lut('3')=4 
    lut('4')=5 
    lut('5')=6
    lut('6')=7 
    lut('7')=8 
    lut('8')=9
    lut('9')=10 ; 57
    lut('A')=11 ; 65
    lut('B')=12 
    lut('C')=13
    lut('D')=14
    lut('E')=15
    lut('F')=16 ; 70
    lut('a')=11 ; 97
    lut('b')=12 
    lut('c')=13
    lut('d')=14
    lut('e')=15
    lut('f')=16 ; 102
  EndIf  
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: hextodec convert hex to decimal

Post by idle »

AZJIO wrote: Sat Jan 20, 2024 10:29 am I expect to receive the number 8454089960965228136567750901461387292011415048925916606819418534331473886123. Where can I see this?
maybe I should rename it, it's to load hex strings into memory like.

Code: Select all

Global *key = HEXTODEC("1b1a1918131211100b0a090803020100")  
Freememory(*key)
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: hextodec convert hex to decimal

Post by AZJIO »

You have the same code in two posts without changes. Was this how it was intended?

HexToBinary()
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: hextodec convert hex to decimal

Post by idle »

AZJIO wrote: Sat Jan 20, 2024 10:50 am You have the same code in two posts without changes. Was this how it was intended?
Umm yes, I edited the 1st post with the fixes made after Rashad's bug report. what's the issue?
Yes hextobinary might be better or maybe Loadhex
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: hextodec convert hex to decimal

Post by AZJIO »

Code: Select all

Debug PeekS(HexToDec("414243444546616263646566"), -1, #PB_Ascii) ; fedcbaFEDCBA
Reverse order
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: hextodec convert hex to decimal

Post by idle »

AZJIO wrote: Sat Jan 20, 2024 11:42 am

Code: Select all

Debug PeekS(HexToDec("414243444546616263646566"), -1, #PB_Ascii) ; fedcbaFEDCBA
Reverse order
Yes that's the point 👉
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: hextodec convert hex to decimal

Post by ChrisR »

Nice, thanks
Post Reply