Hallo,
Ich habe eine Textdatei die Zahlen im normalen Dezimal und im Hexadezimal system enthält.
Nun habe ich ein Problem die Daten umzuwandeln.
Der Val() Befehl will den String als "$1000" und ich habe "0x1000".
Das mittels ReplaceString zu ändern funktioniert zwar ist aber viel zu langsam.
Wie geht ihr so ein Problem an?
Hexadecimal string in zahl umwandlen
Hexadecimal string in zahl umwandlen
PureBasic 5.46 LTS (Windows x86/x64) | windows 10 x64 Oktober failure
-
- Beiträge: 71
- Registriert: 29.05.2010 20:55
Re: Hexadecimal string in zahl umwandlen
So vielleicht?
Code: Alles auswählen
HexZahl$ = "0x1000"
Debug Val("$" + Mid(HexZahl$, 3))
Re: Hexadecimal string in zahl umwandlen
Hmm, da auch normale dezimal Zahlen in der liste sind müsste ich zusätzlich noch FindString(HexZahl$, "0x").
Das ist dann aber genauso langsam wie mit ReplaceString()
Das ist dann aber genauso langsam wie mit ReplaceString()
PureBasic 5.46 LTS (Windows x86/x64) | windows 10 x64 Oktober failure
-
- Beiträge: 71
- Registriert: 29.05.2010 20:55
Re: Hexadecimal string in zahl umwandlen
Code: Alles auswählen
ZahlenListe$ = "2143 505 400 0x1f 231 0xa3 0x12 13 0x63 0x16 610 0x76 0xfe 0xaab 0x123 0xa2b1 0123"
ZahlenListe$ = LCase(ZahlenListe$)
Procedure.q PowInt(Base.q, Exponent.q)
Protected Result.q = Base
If Exponent = 0
ProcedureReturn 1
ElseIf Exponent > 0
For x = 2 To Exponent
Result * Base
Next x
ProcedureReturn Result
Else
ProcedureReturn NaN()
EndIf
EndProcedure
Structure CharArray
c.CHARACTER[0]
EndStructure
*Character.CharArray = @ZahlenListe$
Macro Char
*Character\c[x]\c
EndMacro
For x = 0 To Len(ZahlenListe$)
If Char = '0'
x + 1
If Char = 'x'
x + 1
StartPos = x
While (Char >= '0' And Char <= '9') Or (Char >= 'a' And Char <= 'f')
x + 1
Wend
x - 1
EndPos = x
Len = EndPos - StartPos
Number = 0
For y = Len To 0 Step -1
If *Character\c[StartPos + y]\c >= '0' And *Character\c[StartPos + y]\c <= '9'
Number + (*Character\c[StartPos + y]\c - '0') * PowInt(16, Len - y)
Else
Number + (*Character\c[StartPos + y]\c - ('a' - 10)) * PowInt(16, Len - y)
EndIf
Next y
Debug Number
Continue
Else
x - 1
EndIf
EndIf
If Char >= '0' And Char <= '9'
StartPos = x
While Char >= '0' And Char <= '9'
x + 1
Wend
x - 1
EndPos = x
Len = EndPos - StartPos
Number = 0
For y = Len To 0 Step -1
Number + (*Character\c[StartPos + y]\c - '0') * PowInt(10, Len - y)
Next y
Debug Number
EndIf
Next x
Re: Hexadecimal string in zahl umwandlen
Wow, das ist genial und löst auch gleich noch ein anderes Problem, das ich hatte.
Vielen Dank
Vielen Dank

PureBasic 5.46 LTS (Windows x86/x64) | windows 10 x64 Oktober failure
- NicTheQuick
- Ein Admin
- Beiträge: 8809
- Registriert: 29.08.2004 20:20
- Computerausstattung: Ryzen 7 5800X, 64 GB DDR4-3200
Ubuntu 24.04.2 LTS
GeForce RTX 3080 Ti - Wohnort: Saarbrücken
Re: Hexadecimal string in zahl umwandlen
Und warum so kompliziert?
Code: Alles auswählen
EnableExplicit
Define Zahlenliste.s
ZahlenListe = "2143 505 400 0x1f 231 0xa3 0x12 13 0x63 0x16 610 0x76 0xfe 0xaab 0x123 0xa2b1 0123"
ZahlenListe = LCase(ZahlenListe)
Define number.i, base.i
Define *c.Character = @ZahlenListe
While *c\c
number = 0
base = 10
While (*c\c < '0' Or *c\c > '9')
*c + SizeOf(Character)
Wend
If *c\c = '0'
*c + SizeOf(Character)
If *c\c = 'x'
base = 16
*c + SizeOf(Character)
EndIf
EndIf
While (*c\c >= '0' And *c\c <= '9') Or ((*c\c >= 'a' And *c\c <= 'f') And base = 16)
If *c\c >= '0' And *c\c <= '9'
number * base + (*c\c - '0')
Else
number * base + (10 + *c\c - 'a')
EndIf
*c + SizeOf(Character)
Wend
Debug number
Wend