Share your advanced PureBasic knowledge/code with the community.
idle
Always Here
Posts: 5095 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Fri Jan 19, 2024 10:17 am
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
Caronte3D
Addict
Posts: 1055 Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe
Post
by Caronte3D » Fri Jan 19, 2024 12:33 pm
Thanks, idle
RASHAD
PureBasic Expert
Posts: 4663 Joined: Sun Apr 12, 2009 6:27 am
Post
by RASHAD » 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 :
Egypt my love
idle
Always Here
Posts: 5095 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Sat Jan 20, 2024 6:08 am
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 :
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
Posts: 1361 Joined: Sun May 14, 2017 1:48 am
Post
by AZJIO » Sat Jan 20, 2024 10:29 am
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
idle
Always Here
Posts: 5095 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Sat Jan 20, 2024 10:47 am
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
Posts: 1361 Joined: Sun May 14, 2017 1:48 am
Post
by AZJIO » Sat Jan 20, 2024 10:50 am
You have the same code in two posts without changes. Was this how it was intended?
HexToBinary()
idle
Always Here
Posts: 5095 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Sat Jan 20, 2024 11:07 am
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
Posts: 1361 Joined: Sun May 14, 2017 1:48 am
Post
by AZJIO » Sat Jan 20, 2024 11:42 am
Code: Select all
Debug PeekS(HexToDec("414243444546616263646566"), -1, #PB_Ascii) ; fedcbaFEDCBA
Reverse order
idle
Always Here
Posts: 5095 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Sat Jan 20, 2024 12:04 pm
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
ChrisR
Addict
Posts: 1154 Joined: Sun Jan 08, 2017 10:27 pm
Location: France
Post
by ChrisR » Sat Jan 20, 2024 1:29 pm
Nice, thanks