Delphi , Синтаксис , Преобразования
Под сторокой с HEX-кодами подразумеваю 7B2EB3749AA32C093A6493EE96985D96
function StrToHex(Const Source: String): String;
var
c:Char;
begin
Result := »;
for c in Source do
Result := Result + IntToHex(Integer(c), 2);
end;
function HexToStr(HexStr : String; const Text : boolean = True) : string;
var
Hex : byte;
sTemp : string;
i : integer;
begin
Result := »;
if Length(HexStr) mod 2 <> 0 then
Exit;//(‘Неправельная длина строки’);
for i := 2 to Length(HexStr) do
if i mod 2 = 0 then begin
sTemp := ‘$’ + Copy(HexStr, i — 1, 2);
Hex := Byte(StrToInt(sTemp));
if Hex = $00 then begin
if Text then
Result := Result + ‘ ‘
else
Result := Result + #$00;
Continue;
end;
Result := Result + chr(Hex);
end;
end;
Статья Преобразование строки с HEX-кодами к строке символов и обратно (Функции StrToHex и HexToStr) раздела Синтаксис Преобразования может быть полезна для разработчиков на Delphi и FreePascal.
Комментарии и вопросы
Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.
<**************************************************************************
* NAME: StringToHexStr
* DESC: Konvertiert einen String in eine hexadezimale Darstellung
*************************************************************************>
function StringToHexStr( const value: string ): string ;
begin
SetLength(Result, Length(value)*2); // es wird doppelter Platz benцtigt
if Length(value) > 0 then
BinToHex(PChar(value), PChar(Result), Length(value));
end ;
<**************************************************************************
* NAME: HexStrToString
* DESC: Dekodiert einen hexadezimalen String
*************************************************************************>
function HexStrToString( const value: string ): string ;
begin
SetLength(Result, Length(value) div 2); // es wird halber Platz benцtigt
if Length(value) > 0 then
HexToBin(PChar(value), PChar(Result), Length(value));
end ;
Wдre echt total nett
MFG Lars Wiltfang
Ein Tag ohne Delphi ist ein verlorener Tag!
Homepage zu meinem neuen Programm:
StreamZ

| Larsi |
| Цffentliches Profil ansehen |
| Mehr Beitrдge von Larsi finden |
![]()
Registriert seit: 3. Jan 2007
Ort: Dresden
3.443 Beitrдge
Delphi 7 Enterprise
Re: Delphi: String To Hex/ Hex To Str

| sirius |
| Цffentliches Profil ansehen |
| Mehr Beitrдge von sirius finden |
Registriert seit: 10. Feb 2007
2.262 Beitrдge
Delphi 2007 Professional
Re: Delphi: String To Hex/ Hex To Str
Ein Tag ohne Delphi ist ein verlorener Tag!
Homepage zu meinem neuen Programm:
StreamZ

| Larsi |
| Цffentliches Profil ansehen |
| Mehr Beitrдge von Larsi finden |
Registriert seit: 28. Jan 2006
Ort: Gцrlitz / Sachsen
489 Beitrдge
The function below works perfectly to convert string to hexadecimal:
This result: «737461636B6F766572666C6F77»
The problem is in the function of converting hexadecimal to string:
The result should be «stackoverflow» but nothing happens.
Could someone help me?
1 Answer 1
There are a couple of problems with your code:
You are type-casting your input AnsiString incorrectly to PWideChar , so you are calling the wrong overload of HexToBin() . PWideChar should be PAnsiChar instead.
The BufSize parameter of HexToBin() specifies the number of bytes the output buffer expects to receive, but you are passing it the number of characters in the hex string instead.
Also, since String2Hex() takes an AnsiString and returns a UnicodeString , Hex2String() should take UnicodeString and return an AnsiString to match.