Wenn ich eine Zahl in Hex String umwandeln möchte, dann benutze ich die hex() Funktion.
Gibt es eine Umkehrfunktion, die wieder aus einem Hexstring z.B. '\xfa' oder '0xff' eine integer Zahl berechnet
Erka
Hex String zum Int Zahl
Hoi,
int() wird diese Arbeit machen, aber Vorsicht:
Das letzte funktioniert. Und warum?
Klar?
Gruss,
Christian
int() wird diese Arbeit machen, aber Vorsicht:
Code: Alles auswählen
>>> x = hex(1)
>>> x #gibt '0x1', ist also ein String
'0x1'
>>> int(x)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 0x1
>>> int(x,0)
1
Code: Alles auswählen
>>> print int.__doc__
int(x[, base]) -> integer
Convert a string or number to an integer, if possible. A floating point
argument will be truncated towards zero (this does not include a string
representation of a floating point number!) When converting a string, use
the optional base. It is an error to supply a base when converting a
non-string. If the argument is outside the integer range a long object
will be returned instead.
Gruss,
Christian
Mit int() funktioniert es, wenn du die base angibst (oder 0, dann sucht er sich die selber).
Edit: ...zu langsam
Code: Alles auswählen
>>> x = hex(27)
>>> x
'0x1b'
>>> y = int(x,0)
>>> y
27
Edit: ...zu langsam