各位高人:
我碰到这样一个问题:我有一个Pchar类型的值,如〔‘^’,#252,‘Q’,‘M’,#24,#228,。,#224,#0〕
现在我想把这组数据转化为byte类型的array数组,该如何做?
有人说,可以先转化成PBYTEARRAY,但是pchar如何转成Pbytearray呢?
先谢谢各位!
我碰到这样一个问题:我有一个Pchar类型的值,如〔‘^’,#252,‘Q’,‘M’,#24,#228,。,#224,#0〕
现在我想把这组数据转化为byte类型的array数组,该如何做?
有人说,可以先转化成PBYTEARRAY,但是pchar如何转成Pbytearray呢?
先谢谢各位!
我一向是用String存储byteArray的.
使用时只要byte(PChar[Index])就可以了
var
byteArray: array of Byte;
pCh: PChar;
i: Integer;
s: string;
begin
pCh := PChar('ABCD');
SetLength(byteArray, StrLen(pCh));
for i := 0 to StrLen(pCh) do
begin
byteArray[i] := Byte((pch + i)^);
s := s + Char(byteArray[i]);
end;
ShowMessage(s);
end;如果使用string:
s := StrPas(pCh); // 看,多简单!
如果要单个字符,则只要以数组形式看呆string类型就可以了:
s[1] 为 'A'; s[2] 为 'B'; s[3] 为 'C'; s[4] 为 'D';
var
byteArray: array of Byte;
pCh: PChar;
i: Integer;
s: string;
begin
pCh := PChar('ABCD');
SetLength(byteArray, StrLen(pCh));
for i := 0 to StrLen(pCh) do
begin
byteArray[i] := Byte((pch + i)^);
s := s + Char(byteArray[i]);
end;
ShowMessage(s);
end;如果使用string:
s := StrPas(pCh); // 看,多简单!
如果要单个字符,则只要以数组形式看呆string类型就可以了:
s[1] 为 'A'; s[2] 为 'B'; s[3] 为 'C'; s[4] 为 'D';
C : Pchar;
B : Array [0..7] of Byte;
iLen : Integer;
begin
iLen := SizeOf(B); // Get the lenth for allocat, clear, copy and compare.
GetMem(C,iLen); // Set Pchar Length.
ZeroMemory(C,iLen); // Zero Pchar Memory.
ZeroMemory(@b,iLen); // Zero Byte array Memory.
if CompareMem(c,@b,iLen) then showmessage('same'); // Compare.
c := 'Hello#^' + #253; // Set Pchar value.
if CompareMem(c,@b,iLen) then showmessage('same'); // Compare.
CopyMemory(@B,C,iLen); // Copy pchar to byte.
if CompareMem(c,@b,iLen) then showmessage('same'); // Compare.
C : Pchar;
B : Array [0..7] of Byte;
iLen : Integer;
begin
iLen := SizeOf(B); // Get the lenth for allocat, clear, copy and compare.
GetMem(C,iLen); // Set Pchar Length.
ZeroMemory(C,iLen); // Zero Pchar Memory.
ZeroMemory(@b,iLen); // Zero Byte array Memory.
if CompareMem(c,@b,iLen) then showmessage('same'); // Compare.
c := 'Hello#^' + #253; // Set Pchar value.
if CompareMem(c,@b,iLen) then showmessage('same'); // Compare.
CopyMemory(@B,C,iLen); // Copy pchar to byte.
if CompareMem(c,@b,iLen) then showmessage('same'); // Compare.
C : Pchar;
B : Array [0..7] of Byte;
iLen : Integer;
begin
iLen := SizeOf(B); // Get the lenth for allocat, clear, copy and compare.
GetMem(C,iLen); // Set Pchar Length.
ZeroMemory(C,iLen); // Zero Pchar Memory.
ZeroMemory(@b,iLen); // Zero Byte array Memory.
if CompareMem(c,@b,iLen) then showmessage('same'); // Compare.
c := 'Hello#^' + #253; // Set Pchar value.
if CompareMem(c,@b,iLen) then showmessage('same'); // Compare.
CopyMemory(@B,C,iLen); // Copy pchar to byte.
if CompareMem(c,@b,iLen) then showmessage('same'); // Compare.
end;