不太了解你的意思,你的PChar是怎样的,要转换成16进制的字符串形式(还用转换??)? 还是转换成数字:var str:PChar; a: integer; begin str := 'A24D'; a := StrToInt('$'+str); ShowMessage(IntToStr(a)); end;
procedure TForm1.Button1Click(Sender: TObject); var c: pchar; Len,i: integer; S: string; begin s := ''; c := 'cvdsvdfvfvfvfdassd2144235080'; for i:=1 to Length(c) do begin s:=s+inttohex(ord(c[i]),2); end; Edit1.Text:=s; end;
s:=s+inttohex(ord(c[i]),2)+' ';//这句加个空格,看起来舒服些
procedure TForm1.Button2Click(Sender: TObject); VAR pc:pchar; str:string; x: integer; begin pc:='1234567'; x:=strtoint(pc); str:=inttohex(x,length(pc)); showmessage(str); end;
Pascalprocedure BinToHex(Buffer: PChar; Text: PChar; BufSize: Integer);File ClassesDescription Call BinToHex to convert the binary value in a buffer into a string that is its hexadecimal representation. Buffer is a buffer of bytes that contains the binary value. Text returns a null-terminated string that represents the value of Buffer as a hexadecimal number. BufSize is the size of Buffer. Text needs to point to a sequence of characters that has at least 2*BufSize bytes because each hexadecimal character represents two bytes. setleng(shex, StrLen(const PCharbuffer)*2); bintohex(pcharbuffer, shex, length(shex));
PChar转Hex,首先通过StrLen取得字符串的长度,然后根据该长度的两倍分配Hex的内存空间(一个字节对应两个十六进制字符),然后再一个对个转换。最简单的就是索引表,给出一个256个元素的十六进制串,然后根据Byte的值作为下标直接取。 function PCharToHex(Data:PChar):String; const HEX_CONST:array [0..255] of String = ('00'..'FF');//中间自己填 var iLen: Integer; P_Data,P_Result: PChar; begin Result := ''; Assert(Data<>Nil); iLen := StrLen(Data); if ILen = 0 then Exit; SetLength(Result, iLen * 2); P_Data := Data; P_Result := PChar(Result); while iLen > 0 do begin Move(PChar(HEX_CONST[Ord(P_Data^)])^, P_Result^, 2); Inc(P_Data); Dec(iLen); Inc(P_Result, 2); end; end;
function MemoryToHex(HeadStr: string; const Buffer; Size: Integer): Tstringlist; var tmpP: PByteArray; tmpB: Byte; tmpC: Char; tmpS: string; tmpI: Integer; tmpJ: Integer; begin result := tstringlist.create; tmpP := PByteArray(@Buffer); for tmpI := 0 to (Size + 15) div 16 - 1 do begin tmps := HeadStr; tmpS := tmps + Format(' %0.8X ', [tmpI * $10]); for tmpJ := 0 to $F do //每行以十六进制数显示16个字节 begin if tmpI * $10 + tmpJ < Size then begin tmpB := tmpP^[tmpI * $10 + tmpJ]; if tmpJ = 7 then //第8个字符后显示一个分隔符 tmpS := tmpS + IntToHex(tmpB, 2) + '-' else tmpS := tmpS + IntToHex(tmpB, 2) + ' '; end else begin tmpS := tmpS + ' '; //没有数据时以空格填充 end; end; tmps := tmps + ';'; for tmpJ := 0 to $F do //显示16个字节对应的字符 begin tmpC := ' '; if tmpI * $10 + tmpJ < Size then tmpC := Chr(tmpP^[tmpI * $10 + tmpJ]); if ((tmpC < ' ') or (tmpC > '~')) and (tmpC < char(128)) then tmpC := '.'; //不可见字符以"."显示 tmpS := tmpS + tmpC; end; result.Add(tmpS); end; end; 调用: Memo.lines:= MemoryToHex('Mem:',Buff,Size);
HeadStr: string 这个是什么参数?
procedure TForm1.Button1Click(Sender: TObject); var c: pchar; Len,i: integer; S: string; begin s := ''; c := 'cvdsvdfvfvfvfdassd2144235080'; for i:=1 to Length(c) do begin s:=s+inttohex(ord(c[i]),2); end; Edit1.Text:=s; end;function PCharToHex(Data:PChar):String; const HEX_CONST:array [0..255] of String = ('00'..'FF');//中间自己填 var iLen: Integer; P_Data,P_Result: PChar; begin Result := ''; Assert(Data<>Nil); iLen := StrLen(Data); if ILen = 0 then Exit; SetLength(Result, iLen * 2); P_Data := Data; P_Result := PChar(Result); while iLen > 0 do begin Move(PChar(HEX_CONST[Ord(P_Data^)])^, P_Result^, 2); Inc(P_Data); Dec(iLen); Inc(P_Result, 2); end; end;setleng(shex, StrLen(const PCharbuffer)*2); bintohex(pcharbuffer, shex, length(shex));function PCharToHexStr(var src: PChar):string; var i: Integer; begin Result:='0x'; for i:=0 to strlen(src)-1 do Result:=Result+Format('%.2x', [PBYTE(Integer(src)+i)^])+' '; end;
为什么这三个方法返回的结果都不一样呢?
不知道你是如何使用的 function PCharToHex(Data:PChar):String; const HEX_CONST:array [0..255] of String = ('00','01','02','03','04','05','06','07','08','09','0A','0B','0C','0D','0E','0F', '10','11','12','13','14','15','16','17','18','19','1A','1B','1C','1D','1E','1F', '20','21','22','23','24','25','26','27','28','29','2A','2B','2C','2D','2E','2F', '30','31','32','33','34','35','36','37','38','39','3A','3B','3C','3D','3E','3F', '40','41','42','43','44','45','46','47','48','49','4A','4B','4C','4D','4E','4F', '50','51','52','53','54','55','56','57','58','59','5A','5B','5C','5D','5E','5F', '60','61','62','63','64','65','66','67','68','69','6A','6B','6C','6D','6E','6F', '70','71','72','73','74','75','76','77','78','79','7A','7B','7C','7D','7E','7F', '80','81','82','83','84','85','86','87','88','89','8A','8B','8C','8D','8E','8F', '90','91','92','93','94','95','96','97','98','99','9A','9B','9C','9D','9E','9F', 'A0','A1','A2','A3','A4','A5','A6','A7','A8','A9','AA','AB','AC','AD','AE','AF', 'B0','B1','B2','B3','B4','B5','B6','B7','B8','B9','BA','BB','BC','BD','BE','BF', 'C0','C1','C2','C3','C4','C5','C6','C7','C8','C9','CA','CB','CC','CD','CE','CF', 'D0','D1','D2','D3','D4','D5','D6','D7','D8','D9','DA','DB','DC','DD','DE','DF', 'E0','E1','E2','E3','E4','E5','E6','E7','E8','E9','EA','EB','EC','ED','EE','EF', 'F0','F1','F2','F3','F4','F5','F6','F7','F8','F9','FA','FB','FC','FD','FE','FF'); var iLen: Integer; P_Data,P_Result: PChar; begin Result := ''; Assert(Data<>Nil); iLen := StrLen(Data); if ILen = 0 then Exit; SetLength(Result, iLen * 2); P_Data := Data; P_Result := PChar(Result); while iLen > 0 do begin Move(PChar(HEX_CONST[Ord(P_Data^)])^, P_Result^, 2); Inc(P_Data); Dec(iLen); Inc(P_Result, 2); end; end; //测试 procedure TForm1.btnTestClick(Sender: TObject); var c: pchar; Len,i: integer; S: string; begin s := ''; c := 'cvdsvdfvfvfvfdassd2144235080'; S := PCharToHex(PChar(C)); ShowMessage(S); end;
function PCharToHexStr(var src: PChar):string; var i: Integer; begin Result:='0x';//这里多了0x for i:=0 to strlen(src)-1 do Result:=Result+Format('%.2x', [PBYTE(Integer(src)+i)^])+' ';//后面多了一个空格 end;procedure TForm1.Button1Click(Sender: TObject); var c: pchar; Len,i: integer; S: string; begin s := ''; c := 'cvdsvdfvfvfvfdassd2144235080'; for i:=1 to Length(c) do //这里的C是PChar,而不是String,所以i应该从0开始,而不是从1开始 begin s:=s+inttohex(ord(c[i]),2); end; Edit1.Text:=s; end;
procedure TForm1.Button1Click(Sender: TObject); var c: pchar; Len,i: integer; S: string; begin s := ''; c := 'cvdsvdfvfvfvfdassd2144235080'; for i:=1-1 to Length(c)-1 do begin s:=s+inttohex(ord(c[i]),2); end; Edit1.Text:=s; end;
function PCharToHex(const Data: PChar): string; const HexDigits: array[0..15] of char = '0123456789ABCDEF'; var I: Integer; Len: Integer; P: PByteArray; begin if Data = nil then Exit; Len := StrLen(Data); if Len = 0 then Exit; SetLength(Result, Len shl 1); P := PByteArray(Data); for I := 0 to Len - 1 do begin Result[I shl 1 + 1] := HexDigits[P[I] shr 4]; Result[I shl 1 + 2] := HexDigits[P[I] and $F]; end; end;procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(PCharToHex('0123456789ABCDEFabcdef')); end;
谢谢各位了,最后使用了这个方法,相对比较的简约一点。function PCharToHex(var src: PChar):string; var i: Integer; begin for i:=0 to strlen(src)-1 do Result:=Result+Format('%.2x', [PBYTE(Integer(src)+i)^]); end;function HexToStr(var src: string):string; var i: Integer; begin for i:=0 to length(src) do begin if (i mod 2 =1) then Result:=Result+char(strtoint('0x'+Copy(src,i,2))); end; end;
还是转换成数字:var
str:PChar;
a: integer;
begin
str := 'A24D';
a := StrToInt('$'+str);
ShowMessage(IntToStr(a));
end;
var
c: pchar;
Len,i: integer;
S: string;
begin
s := '';
c := 'cvdsvdfvfvfvfdassd2144235080';
for i:=1 to Length(c) do
begin
s:=s+inttohex(ord(c[i]),2);
end;
Edit1.Text:=s;
end;
VAR
pc:pchar;
str:string;
x: integer;
begin
pc:='1234567';
x:=strtoint(pc);
str:=inttohex(x,length(pc));
showmessage(str);
end;
ClassesDescription
Call BinToHex to convert the binary value in a buffer into a string that is its hexadecimal representation. Buffer is a buffer of bytes that contains the binary value. Text returns a null-terminated string that represents the value of Buffer as a hexadecimal number. BufSize is the size of Buffer. Text needs to point to a sequence of characters that has at least 2*BufSize bytes because each hexadecimal character represents two bytes.
setleng(shex, StrLen(const PCharbuffer)*2);
bintohex(pcharbuffer, shex, length(shex));
const
HEX_CONST:array [0..255] of String = ('00'..'FF');//中间自己填
var
iLen: Integer;
P_Data,P_Result: PChar;
begin
Result := '';
Assert(Data<>Nil);
iLen := StrLen(Data);
if ILen = 0 then Exit;
SetLength(Result, iLen * 2);
P_Data := Data;
P_Result := PChar(Result);
while iLen > 0 do begin
Move(PChar(HEX_CONST[Ord(P_Data^)])^, P_Result^, 2);
Inc(P_Data);
Dec(iLen);
Inc(P_Result, 2);
end;
end;
function MemoryToHex(HeadStr: string; const Buffer; Size: Integer): Tstringlist;
var
tmpP: PByteArray;
tmpB: Byte;
tmpC: Char;
tmpS: string;
tmpI: Integer;
tmpJ: Integer;
begin
result := tstringlist.create;
tmpP := PByteArray(@Buffer);
for tmpI := 0 to (Size + 15) div 16 - 1 do
begin
tmps := HeadStr;
tmpS := tmps + Format(' %0.8X ', [tmpI * $10]);
for tmpJ := 0 to $F do //每行以十六进制数显示16个字节
begin
if tmpI * $10 + tmpJ < Size then
begin
tmpB := tmpP^[tmpI * $10 + tmpJ];
if tmpJ = 7 then //第8个字符后显示一个分隔符
tmpS := tmpS + IntToHex(tmpB, 2) + '-'
else
tmpS := tmpS + IntToHex(tmpB, 2) + ' ';
end
else
begin
tmpS := tmpS + ' '; //没有数据时以空格填充
end;
end;
tmps := tmps + ';';
for tmpJ := 0 to $F do //显示16个字节对应的字符
begin
tmpC := ' ';
if tmpI * $10 + tmpJ < Size then tmpC := Chr(tmpP^[tmpI * $10 + tmpJ]);
if ((tmpC < ' ') or (tmpC > '~')) and (tmpC < char(128)) then tmpC := '.'; //不可见字符以"."显示
tmpS := tmpS + tmpC;
end;
result.Add(tmpS);
end;
end;
调用:
Memo.lines:= MemoryToHex('Mem:',Buff,Size);
var
c: pchar;
Len,i: integer;
S: string;
begin
s := '';
c := 'cvdsvdfvfvfvfdassd2144235080';
for i:=1 to Length(c) do
begin
s:=s+inttohex(ord(c[i]),2);
end;
Edit1.Text:=s;
end;function PCharToHex(Data:PChar):String;
const
HEX_CONST:array [0..255] of String = ('00'..'FF');//中间自己填
var
iLen: Integer;
P_Data,P_Result: PChar;
begin
Result := '';
Assert(Data<>Nil);
iLen := StrLen(Data);
if ILen = 0 then Exit;
SetLength(Result, iLen * 2);
P_Data := Data;
P_Result := PChar(Result);
while iLen > 0 do begin
Move(PChar(HEX_CONST[Ord(P_Data^)])^, P_Result^, 2);
Inc(P_Data);
Dec(iLen);
Inc(P_Result, 2);
end;
end;setleng(shex, StrLen(const PCharbuffer)*2);
bintohex(pcharbuffer, shex, length(shex));function PCharToHexStr(var src: PChar):string;
var
i: Integer;
begin
Result:='0x';
for i:=0 to strlen(src)-1 do
Result:=Result+Format('%.2x', [PBYTE(Integer(src)+i)^])+' ';
end;
为什么这三个方法返回的结果都不一样呢?
function PCharToHex(Data:PChar):String;
const
HEX_CONST:array [0..255] of String = ('00','01','02','03','04','05','06','07','08','09','0A','0B','0C','0D','0E','0F',
'10','11','12','13','14','15','16','17','18','19','1A','1B','1C','1D','1E','1F',
'20','21','22','23','24','25','26','27','28','29','2A','2B','2C','2D','2E','2F',
'30','31','32','33','34','35','36','37','38','39','3A','3B','3C','3D','3E','3F',
'40','41','42','43','44','45','46','47','48','49','4A','4B','4C','4D','4E','4F',
'50','51','52','53','54','55','56','57','58','59','5A','5B','5C','5D','5E','5F',
'60','61','62','63','64','65','66','67','68','69','6A','6B','6C','6D','6E','6F',
'70','71','72','73','74','75','76','77','78','79','7A','7B','7C','7D','7E','7F',
'80','81','82','83','84','85','86','87','88','89','8A','8B','8C','8D','8E','8F',
'90','91','92','93','94','95','96','97','98','99','9A','9B','9C','9D','9E','9F',
'A0','A1','A2','A3','A4','A5','A6','A7','A8','A9','AA','AB','AC','AD','AE','AF',
'B0','B1','B2','B3','B4','B5','B6','B7','B8','B9','BA','BB','BC','BD','BE','BF',
'C0','C1','C2','C3','C4','C5','C6','C7','C8','C9','CA','CB','CC','CD','CE','CF',
'D0','D1','D2','D3','D4','D5','D6','D7','D8','D9','DA','DB','DC','DD','DE','DF',
'E0','E1','E2','E3','E4','E5','E6','E7','E8','E9','EA','EB','EC','ED','EE','EF',
'F0','F1','F2','F3','F4','F5','F6','F7','F8','F9','FA','FB','FC','FD','FE','FF');
var
iLen: Integer;
P_Data,P_Result: PChar;
begin
Result := '';
Assert(Data<>Nil);
iLen := StrLen(Data);
if ILen = 0 then Exit;
SetLength(Result, iLen * 2);
P_Data := Data;
P_Result := PChar(Result);
while iLen > 0 do begin
Move(PChar(HEX_CONST[Ord(P_Data^)])^, P_Result^, 2);
Inc(P_Data);
Dec(iLen);
Inc(P_Result, 2);
end;
end;
//测试
procedure TForm1.btnTestClick(Sender: TObject);
var
c: pchar;
Len,i: integer;
S: string;
begin
s := '';
c := 'cvdsvdfvfvfvfdassd2144235080';
S := PCharToHex(PChar(C));
ShowMessage(S);
end;
var
i: Integer;
begin
Result:='0x';//这里多了0x
for i:=0 to strlen(src)-1 do
Result:=Result+Format('%.2x', [PBYTE(Integer(src)+i)^])+' ';//后面多了一个空格
end;procedure TForm1.Button1Click(Sender: TObject);
var
c: pchar;
Len,i: integer;
S: string;
begin
s := '';
c := 'cvdsvdfvfvfvfdassd2144235080';
for i:=1 to Length(c) do //这里的C是PChar,而不是String,所以i应该从0开始,而不是从1开始
begin
s:=s+inttohex(ord(c[i]),2);
end;
Edit1.Text:=s;
end;
var
c: pchar;
Len,i: integer;
S: string;
begin
s := '';
c := 'cvdsvdfvfvfvfdassd2144235080';
for i:=1-1 to Length(c)-1 do
begin
s:=s+inttohex(ord(c[i]),2);
end;
Edit1.Text:=s;
end;
function PCharToHex(const Data: PChar): string;
const
HexDigits: array[0..15] of char = '0123456789ABCDEF';
var
I: Integer;
Len: Integer;
P: PByteArray;
begin
if Data = nil then Exit;
Len := StrLen(Data);
if Len = 0 then Exit;
SetLength(Result, Len shl 1);
P := PByteArray(Data);
for I := 0 to Len - 1 do
begin
Result[I shl 1 + 1] := HexDigits[P[I] shr 4];
Result[I shl 1 + 2] := HexDigits[P[I] and $F];
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(PCharToHex('0123456789ABCDEFabcdef'));
end;
Input,
Output,shex,
Key: Pchar;
str,user,pass:string;
i,j:integer;begin
GetMem(Input,16);
GetMem(Output,16);
GetMem(Key,16);
CopyMemory(Key,PChar('test'),8);
CopyMemory(Input,PChar('test'),16);DES(Output,Input,16,Key,8,false);//bintohex(Output, shex, length(Output)*2);Memo1.Text:=Output;
end;我发现改变 key input 等值的长度结果都会改变,向各位请教了。例如:CopyMemory(Key,PChar('test'),8);
CopyMemory(Input,PChar('test'),16);跟CopyMemory(Key,PChar('test'),8);
CopyMemory(Input,PChar('test'),64);返回的 output 结果是不一样的。
这个是为什么?
GetMem(Output,64);
GetMem(Key,16);
CopyMemory(Key,PChar('test'),16);
CopyMemory(Input,PChar('test'),16);
你的test才4个字节,你CopyMemory用16个字节,剩下那12个字节就是偷来的,自然也是随机的。
1.Getmem之后,清空,比如使用fillchar
2.CopyMemory的时候,只复制实际字节数,或者换用strcopy
var
i: Integer;
begin
for i:=0 to strlen(src)-1 do
Result:=Result+Format('%.2x', [PBYTE(Integer(src)+i)^]);
end;function HexToStr(var src: string):string;
var
i: Integer;
begin
for i:=0 to length(src) do begin
if (i mod 2 =1) then
Result:=Result+char(strtoint('0x'+Copy(src,i,2)));
end;
end;