forum.boolean.name

forum.boolean.name (http://forum.boolean.name/index.php)
-   Основной форум (http://forum.boolean.name/forumdisplay.php?f=49)
-   -   Как перевести строку в hex ? (http://forum.boolean.name/showthread.php?t=3195)

om2804 20.04.2007 22:20

Как перевести строку в hex ?
 
Нужно файл файл представить в виде hex (как в hex-редакторе). Возникают проблемы если hex-код символа в файле болше 7F(127). Как правильно перевести символ в hex?

Этот код работает не корректно, если символ hex-код символа в str болше 7F(127)

num:=ord(getChar(str, i));

i-порядковый номер символа
str - строка, которую нужно преобразовать
num- преобразованный символ (в десятичной системе счисления)

Kurdt 21.04.2007 16:41

Можно сделать функцию с банальным сравнением А='lala'...'я'='lala'

вот тибе пример как переводить текст в шеснадцатиричный вид

function celoe(ch,n:integer):integer;
Var i:integer;
Begin
while ch>=n do ch:=ch-n;
celoe:=ch
end;

function integertohex(ii:integer):string;
const
Val = 16;
var
HexValue : string;
Remainder : Integer;
begin
HexValue := '';
while ii > 0 do
begin
Remainder := celoe(ii,val);
If Remainder = 10 then HexValue := 'A' + HexValue else
If Remainder = 11 then HexValue := 'B' + HexValue else
If Remainder = 12 then HexValue := 'B' + HexValue else
If Remainder = 13 then HexValue := 'D' + HexValue else
If Remainder = 14 then HexValue := 'E' + HexValue else
If Remainder = 15 then HexValue := 'F' + HexValue
else
HexValue := IntegerToString( Remainder ) + HexValue;

ii := Trunc(ii/val);
end;
while length(HexValue)< Trunc(Val/4) do HexValue:='0'+HexValue;
integertohex:=HexValue;
end;

function text2hex(s:string):string;
Var i:integer;
res:string;
Begin
for i:=1 to length(s) do res:=res+integertohex(ord(getchar(s,i-1)));
text2hex:=res;
End;

om2804 21.04.2007 21:47

Re: Как перевести строку в hex ?
 
Я так понимаю нужно вызывать function text2hex(s:string):string;
Где s - исходная текстовая строка.А Функция возвращает hex - код.

impersonalis 04.06.2011 18:49

Ответ: Как перевести строку в hex ?
 
Код:

If Remainder = 10 then HexValue := 'A' + HexValue else
 If Remainder = 11 then HexValue := 'B' + HexValue else
 If Remainder = 12 then HexValue := 'B' + HexValue else
 If Remainder = 13 then HexValue := 'D' + HexValue else
 If Remainder = 14 then HexValue := 'E' + HexValue else
 If Remainder = 15 then HexValue := 'F' + HexValue

Этот код ужасен.
Его заменяет конструкция типа
Цитата:

HexValue :=chr(55+Remainder)+HexValue


Часовой пояс GMT +4, время: 15:06.

vBulletin® Version 3.6.5.
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Перевод: zCarot