我有一个来自基于C ++的DLL的字符串,该字符串可以包含BOM,并且将以null终止
我最初是通过将其视为char数组并将结果转换为PChar来处理它的。
function TRFCNWParameter.GetAsString: string;
const
DEFAULT_BUFFER_SIZE = 1024;
var
ResultSize: cardinal;
Buffer: TArray<Char>;
lErrorInfo: RRFCNWErrorInfo;
RC: TRfcReturnCode;
begin
SetLength(Buffer, DEFAULT_BUFFER_SIZE);
RC := RFCGetString(OwnerHandle, PChar(ParamName), @Buffer[0], Length(Buffer), ResultSize, @lErrorInfo);
if (RC <> RFC_OK) and (ResultSize >= Length(Buffer)) then
begin
// resize buffer and retry
SetLength(Buffer, ResultSize + 1);
RC := RFCGetString(OwnerHandle, PChar(ParamName), @Buffer[0], Length(Buffer), ResultSize, @lErrorInfo);
end;
CheckForReturnedError(@lErrorInfo, 'Retrieve Parameter %s ' + ParamName);
result := PChar(Buffer);
end;
现在,我对unicode字符串在调试器中的显示方式不太熟悉,但是我注意到在评估结果字符串时,我可以看到BOM
因此,来自Delphi的常规字符串将出现在单引号中:
'test string'
该例程中的字符串可能在调试器中显示为
#FEFF'test string'
它出现在单引号之外,这使我怀疑Delphi是否在本质上处理它。
但是,当我在将输出解析为XML时遇到问题时,我对此表示怀疑。因此,我正在尝试转换为本地UTF-16
function TRFCNWParameter.GetAsString: string;
const
DEFAULT_BUFFER_SIZE = 1024;
var
ResultSize: cardinal;
Buffer: TBytes;
bufferCharLength: cardinal;
lErrorInfo: RRFCNWErrorInfo;
RC: TRfcReturnCode;
char_size: cardinal;
begin
char_size := 2;
bufferCharLength := DEFAULT_BUFFER_SIZE;
SetLength(Buffer, DEFAULT_BUFFER_SIZE * char_size);
RC := RFCGetString(OwnerHandle, PChar(ParamName), @Buffer[0], bufferCharLength, ResultSize, @lErrorInfo);
if (RC <> RFC_OK) and (ResultSize >= Length(Buffer)) then
begin
// resize buffer and retry
bufferCharLength := (ResultSize + 1);
SetLength(Buffer, bufferCharLength * CHAR_SIZE);
RC := RFCGetString(OwnerHandle, PChar(ParamName), @Buffer[0], bufferCharLength, ResultSize, @lErrorInfo);
end;
CheckForReturnedError(@lErrorInfo, 'Retrieve Parameter %s ' + ParamName);
result := TEncoding.Unicode.GetString(buffer); // needed to handle little-endian and BOM
end;
但是,这并不是以零结尾的字符串,最后使我的负载为0。我猜我可以转换为PChar然后再转换为result := PChar(TEncoding.Unicode.GetString(buffer));
,但这看起来有点丑陋,这让我怀疑我是不是在做这个稍微错误的事情(或者我甚至需要在IDE处理时做任何事情) BOM是否正确)?