GNiessen
asked on
Array of Byte to PChar
I am converting a DLL to XE from D7 and need to convert an array of Byte that is passed in to a PChar that can be passed to a StrMove.
I could write my own ByteMove, but am needing a similar conversion in other spots that are more complicated to explain.
With D7 this worked. But with the new PWideChar support in D2009+ it will not.
I could write my own ByteMove, but am needing a similar conversion in other spots that are more complicated to explain.
procedure Transpose(var Data, OrderData: array of Byte);
var
TmpData: array[0..63] of Byte;
i : Byte;
begin
StrMove(@TmpData, @Data, SizeOf(Data));
for i := 0 to High(OrderData) do
Data[i] := TmpData[OrderData[i]];
end;
With D7 this worked. But with the new PWideChar support in D2009+ it will not.
in <=D2007 PChar = PAnsiChar, Char = AnsiChar
in D2009+ PChar = PWideChar, Char = WideChar
SizeOf(PAnsiChar) = 1Byte
SizeOf(PWideChar) = 2Byte
try to run this code in different versions of delphi
in D2009+ PChar = PWideChar, Char = WideChar
SizeOf(PAnsiChar) = 1Byte
SizeOf(PWideChar) = 2Byte
try to run this code in different versions of delphi
ShowMessage(Format('Size of "AnsiChar" %d, Size of "WideChar" %d, Size of "Char" %d', [SizeOf(AnsiChar), SizeOf(WideChar), SizeOf(Char)]));
I wrote wrong
SizeOf(AnsiChar) = 1Byte
SizeOf(WideChar) = 2Byte
SizeOf(PAnsiChar) = 1Bytemust be
SizeOf(PWideChar) = 2Byte
SizeOf(AnsiChar) = 1Byte
SizeOf(WideChar) = 2Byte
ASKER
In this case I have a fixed Byte Array of 64 characters getting passed in from another system. There is some encryption being done that make accessing them as bytes more efficient then as an array of characters, This is legacy code, so it is all AnsiChar. I am not looking to make it Unicode enabled at this time.
I guess you only need to get rid of the string based approach, and handle data simply as bytes instead.
It may worth thinking it over to pass PByte pointers to a function that expected PAnsiChar parameters, since it may lead to maintainability problems. I mean, finding a function with another parameter type is a good trail, even to another possible solution using the CopyMemory API as well.
procedure Transpose(var Data, OrderData: array of Byte);
var
TmpData: array[0..63] of Byte;
i : Byte;
begin
Move(Data, TmpData, SizeOf(Data));
for i := 0 to High(OrderData) do
Data[i] := TmpData[OrderData[i]];
end;
It may worth thinking it over to pass PByte pointers to a function that expected PAnsiChar parameters, since it may lead to maintainability problems. I mean, finding a function with another parameter type is a good trail, even to another possible solution using the CopyMemory API as well.
Hello,
I guess you only need to get rid of the otherwise suspicious parameter passing - I mean, the StrMove expects PAnsiChars (in D7), so it should not deal with PByte pointers. You could move the data in a loop yourself byte by byte, or use one of the handful of functions like CopyMemory, MoveMemory, Move, etc..
As for me, I would prefer using Move as it seems to be the native solution:
I guess you only need to get rid of the otherwise suspicious parameter passing - I mean, the StrMove expects PAnsiChars (in D7), so it should not deal with PByte pointers. You could move the data in a loop yourself byte by byte, or use one of the handful of functions like CopyMemory, MoveMemory, Move, etc..
As for me, I would prefer using Move as it seems to be the native solution:
procedure Transpose(var Data, OrderData: array of Byte);
var
TmpData: array[0..63] of Byte;
i : Byte;
begin
Move(Data, TmpData, SizeOf(Data));
for i := 0 to High(OrderData) do
Data[i] := TmpData[OrderData[i]];
end;
" I mean, the StrMove expects PAnsiChars (in D7), so it should not deal with PByte pointers. "
Well, they are technically both pointers, hence why it works fine.
Well, they are technically both pointers, hence why it works fine.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
While I agree with your comments on the ByteArray, sometimes there are reasons to not change things any more then required to make it work again. So I will go with the StrMove and PAnsiChar. Which was what I did and tested before, but was not sure it was the best or definitive solution. And this might not have been the best example of what I have in other areas, but seems to have worked.
Thanks
Thanks
and what are you trying to accomplish ?
the context of what you are doing is odd for unicode strings
have you checked the functions StringOf and BytesOf ?