BinToHex ust das Richtige, für mehrere Bytes ... mit IntToHex kann man ja nur Byte-/Word-weise umwandeln...
mathematisch gesehn:
Hex = 16 Werte, das sind ja bekanntlich 4 Bits
1 Byte, das sollte auch bekannt sein besteht aus 8 Bits
das bedeutet 2x Hex = 1 Byte...
Byte and $0F = der 1. Hex-Anteil
Byte and $F0 = der 2. Hex-Anteil
den 2. Anteil solle man entspechend verschieben, damit man damit rechnen kann
> (Ord(Char) and $F0) shr 4 > oder einfach nur Ord(Char) shr 4
Umwandlung geht am einfachsten über 'ne Tabelle, oder einen String
Const HexChars1: Array[0..15] of Char = ('0', '1' ... '9', 'A' ... 'F');
Const HexChars2 = '0123456789ABCDEF';
ein Byte/Char to Hex ist also
HexString := HexChars1[Byte and $0F] + HexChars1[Byte shr 4];
HexString := HexChars2[Byte and $0F + 1] + HexChars2[Byte shr 4 + 1];
in einer Schleife wird dann daraus
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
| Const HexChars: Array[0..15] of Char = '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
Var X: Array of Byte; S: String; i: Interger;
S := ''; For i := 0 to High(X) do S := S + HexChars[Byte and $0F] + HexChars[Byte shr 4];
SetLength(S, Length(X) * 2); For i := 0 to High(X) do Begin S[i * 2] := HexChars[Byte and $0F]; S[i * 2 + 1] := HexChars[Byte shr 4]; End; |
PS: BinToHex mach es so ähnlich wie die kurze Variante (vermute ich mal)