ansi字符串和utf8字符串之間相互轉換


ansi字符串和utf8字符串之間相互轉換

unit AnsiStringTools;

interface

uses
  SysUtils;



type
  {$if CompilerVersion <= 18.5}
     TBytes = array of Byte;
  {$ifend}
  TAnsiStringTools = class(TObject)
  public
    /// <summary>
    /// ansi字符串轉utf8 bytes
    /// </summary>
    /// <param name="v">ansi字符串</param>
    /// <returns>utf8 bytes</returns>
    class function ansiString2Utf8Bytes(v:AnsiString): TBytes;
    /// <summary>
    /// utf8字符串轉ansi字符串
    /// </summary>
    /// <param name="pvData"></param>
    /// <returns>ansi字符串</returns>
    class function Utf8AnsiString2AnsiString(pvData:AnsiString): AnsiString;
    /// <summary>
    /// uft8 bytes轉ansi字符串
    /// </summary>
    /// <param name="pvData">uft8 bytes</param>
    /// <returns>ansi字符串</returns>
    class function Utf8Bytes2AnsiString(pvData: TBytes): AnsiString;
  end;

implementation

class function TAnsiStringTools.ansiString2Utf8Bytes(v:AnsiString): TBytes;
var
  lvTemp:AnsiString;
begin
  lvTemp := AnsiToUtf8(v);
  SetLength(Result, Length(lvTemp));
  Move(lvTemp[1], Result[0],  Length(lvTemp));
end;

class function TAnsiStringTools.Utf8AnsiString2AnsiString(pvData:AnsiString):
    AnsiString;
begin
  Result := Utf8ToAnsi(pvData);
end;

class function TAnsiStringTools.Utf8Bytes2AnsiString(pvData: TBytes):
    AnsiString;
var
  lvTemp:AnsiString;
begin
  SetLength(lvTemp, Length(pvData));
  Move(pvData[0], lvTemp[1],  Length(pvData));
  Result := Utf8ToAnsi(lvTemp);
end;

end.

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM