Seite 1 von 1

Hexadecimal string in zahl umwandlen

Verfasst: 29.06.2012 14:19
von _JON_
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?

Re: Hexadecimal string in zahl umwandlen

Verfasst: 29.06.2012 14:25
von Sirius-2337
So vielleicht?

Code: Alles auswählen

HexZahl$ = "0x1000"
Debug Val("$" + Mid(HexZahl$, 3))

Re: Hexadecimal string in zahl umwandlen

Verfasst: 29.06.2012 14:32
von _JON_
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()

Re: Hexadecimal string in zahl umwandlen

Verfasst: 29.06.2012 15:36
von Sirius-2337

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

Verfasst: 29.06.2012 15:47
von _JON_
Wow, das ist genial und löst auch gleich noch ein anderes Problem, das ich hatte.

Vielen Dank :allright:

Re: Hexadecimal string in zahl umwandlen

Verfasst: 29.06.2012 17:20
von NicTheQuick
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