我的一些实用程序(您可以从我的网站下载完整的代码)...
//------------------------------------------------------------------------------
// CsiStrToBytes
//
// Convert pInStr to an array of bytes using the string encoding
// pStringEncoding (one of automatic, Ansi, UTF-16, or UTF-8) and optionally
// include the byte order mark according to the pIncludeBom flag
//------------------------------------------------------------------------------
function CsiStrToBytes(const pInStr: string;
pStringEncoding: TECsiStringEncoding;
pIncludeBom: Boolean): TByteDynArray;
var
{$IFDEF UNICODE}
lStringEncoding: TECsiStringEncoding;
lStringStream: TStringStream;
lPreambleBytes: TBytes;
lStringBytes: TBytes;
lPreambleLen: Integer;
lStringLen: Integer;
{$ENDIF}
lLen: Integer;
{$IFDEF UNICODE}
lIndex: Integer;
{$ENDIF}
begin
if pInStr <> '' then begin
{$IFDEF UNICODE}
if pStringEncoding = seAuto then
lStringEncoding := CsiGetPreferredEncoding(pInStr)
else
lStringEncoding := pStringEncoding;
// UTF-8 and UTF-16 encoding can be handled by the TStringStream class
if (lStringEncoding = seUtf8) or (lStringEncoding = seUtf16) then begin
if lStringEncoding = seUtf8 then
lStringStream := TStringStream.Create(pInStr, TEncoding.Utf8)
else
lStringStream := TStringStream.Create(pInStr, TEncoding.Unicode);
try
// add the UTF-8 or UTF-16 byte order mark to the start of the array of
// bytes if required
if pIncludeBom then
lPreambleBytes := lStringStream.Encoding.GetPreamble
else
SetLength(lPreambleBytes, 0);
lStringBytes := lStringStream.Bytes;
lPreambleLen := Length(lPreambleBytes);
lStringLen := Length(lStringBytes);
SetLength(Result, lPreambleLen + lStringLen);
if lPreambleLen > 0 then
Move(lPreambleBytes[0], Result[0], lPreambleLen);
if lStringLen > 0 then
Move(lStringBytes[0], Result[lPreambleLen], lStringLen);
finally
lStringStream.Free;
end;
end else begin
{$ENDIF}
// Ansi encoding must be handled manually
lLen := Length(pInStr);
SetLength(Result, lLen);
{$IFDEF UNICODE}
for lIndex := 1 to lLen do
Result[lIndex - 1] := Ord(pInStr[lIndex]) and $00ff;
{$ELSE}
Move(pInStr[1], Result[0], lLen);
{$ENDIF}
{$IFDEF UNICODE}
end;
{$ENDIF}
end else
SetLength(Result, 0);
end;
//------------------------------------------------------------------------------
// CsiSaveToFile
//
// Saves pData, an array of bytes, to pFileName
//------------------------------------------------------------------------------
procedure CsiSaveToFile(const pData: TByteDynArray; const pFileName: string);
var
lFileStream: TFileStream;
lLen: Integer;
begin
lFileStream := TFileStream.Create(pFileName, fmCreate);
try
lLen := Length(pData);
if lLen > 0 then
lFileStream.WriteBuffer(pData[0], lLen);
finally
lFileStream.Free;
end;
end;
//------------------------------------------------------------------------------
// CsiSaveToFile
//
// Saves pText to pFileName using the string encoding pStringEncoding, which is
// one of automatic, Ansi, UTF-16, or UTF-8
//------------------------------------------------------------------------------
procedure CsiSaveToFile(const pText: string; const pFileName: string;
pStringEncoding: TECsiStringEncoding);
begin
CsiSaveToFile(CsiStrToBytes(pText, pStringEncoding), pFileName);
end;