如何将字符串转化成utf8格式 ( 积分: 100 )

  • 主题发起人 主题发起人 lengbing
  • 开始时间 开始时间
L

lengbing

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:<br> &nbsp; &nbsp;谁能提供一个将字符串转化成utf8格式的代码,非常感谢。
 
各位大侠:<br> &nbsp; &nbsp;谁能提供一个将字符串转化成utf8格式的代码,非常感谢。
 
一个 UTF-8 字符转换单元 <br>提交日期:2004-5-28 作者?: itren <br> <br>关键词:UTF-8 &nbsp;<br>{$J+,Z4}<br>unit UTF8;<br>{$IFDEF CONDITIONALEXPRESSIONS} {$WARN SYMBOL_PLATFORM OFF} {$ENDIF}<br><br>{------------------------------------------------------------------------------}<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UTF8 utilities - especially for versions prior to Delphi 6 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; This code is &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Copyright (C) 2003 by Michael in der Wiesche &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>{------------------------------------------------------------------------------}<br><br> Secure memory management prevents passphrases from being swapped to disk<br>// Using secure memory within Delphi causes crashes, so it is disabled when debugging<br>// IsDebuggerPresent isn't available on Windows 95 - but neither is PGP 8.X - so we can use it ...<br><br>interface<br><br>type UTF8String = type String;<br><br>var AnsiToUtf8PChar: procedure(const Ansi, Utf8: PChar; var Len: Cardinal); stdcall;<br>var Utf8ToAnsiPChar: procedure(const Utf8, Ansi: PChar; var Len: Cardinal); stdcall;<br><br>function AnsiToUtf8(const S: String): UTF8String;<br>function Utf8ToAnsi(const S: UTF8String): String;<br>function Utf8OrAnsi(const S: UTF8String): String;<br><br>const MaxUTF8Length = 256*6;<br><br>implementation<br><br>uses Windows, SysUtils, pgpBase;<br><br>var SecureMemoryMgr: procedure; stdcall;<br>var DefaultMemoryMgr: procedure; stdcall;<br>var IsSecureMemoryMgr: function: Longbool; stdcall;<br><br>function AnsiToUtf8(const S: String): UTF8String;<br>var<br> &nbsp;Len: Cardinal;<br>begin<br> &nbsp;Len:=succ(Length(S) shl 2);<br> &nbsp;SetLength(Result, Len);<br> &nbsp;AnsiToUtf8PChar(PChar(S), PChar(Result), Len);<br> &nbsp;SetLength(Result, Len);<br>end;<br><br>function Utf8ToAnsi(const S: UTF8String): String;<br>var<br> &nbsp;Len: Cardinal;<br>begin<br> &nbsp;Len:=succ(Length(S));<br> &nbsp;SetLength(Result, Len);<br> &nbsp;Utf8ToAnsiPChar(PChar(S), PChar(Result), Len);<br> &nbsp;SetLength(Result, Len);<br>end;<br><br>function Utf8OrAnsi(const S: UTF8String): String;<br>begin<br> &nbsp;Result:=Utf8ToAnsi(S);<br> &nbsp;if Result='' then Result:=S;<br>end;<br><br>function Debugging: Longbool;<br>var<br> &nbsp;IsDebuggerPresent: function: Bool; stdcall;<br>begin<br> &nbsp;IsDebuggerPresent:=GetProcAddress(GetModuleHandle(Kernel32), 'IsDebuggerPresent');<br> &nbsp;Result:=IsDebuggerPresent;<br>end;<br><br>function UsingSecMemIsSafe: Longbool;<br>begin<br> &nbsp;Result:=(PGP8X and not Debugging and (pos('delphi32.exe', LowerCase(ParamStr(0)))=0));<br>end;<br><br>initialization<br> &nbsp;if hUTF8Lib&lt;&gt;0 then begin<br> &nbsp; &nbsp;AnsiToUtf8PChar:=GetProcAddress(hUTF8Lib, ptr(1));<br> &nbsp; &nbsp;Utf8ToAnsiPChar:=GetProcAddress(hUTF8Lib, ptr(2));<br> &nbsp; &nbsp;SecureMemoryMgr:=GetProcAddress(hUTF8Lib, ptr(3));<br> &nbsp; &nbsp;DefaultMemoryMgr:=GetProcAddress(hUTF8Lib, ptr(4));<br> &nbsp; &nbsp;IsSecureMemoryMgr:=GetProcAddress(hUTF8Lib, ptr(5));<br> &nbsp; &nbsp;if UsingSecMemIsSafe then SecureMemoryMgr;<br> &nbsp;end;<br><br>finalization<br> &nbsp;if (hUTF8Lib&lt;&gt;0) and IsSecureMemoryMgr then DefaultMemoryMgr;<br><br>end.
 
不是吧?直接用AnsiToUtf8就可以了。
 
Delphi自带了AnsiToUtf8函数
 
多谢各位的帮助,我要的形式是这样如:疯狂短信 转为utf8编码<br>E796AFE78B82E79FADE4BFA1 &nbsp; &nbsp; &nbsp; <br>如何实现,
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3182238
 
lengbing 呵呵,我知道你想做 gsm modem 的wap push 吧,就用他们说的那样,用AnsiToUtf8就可以了,我做过这个的。呵呵。
 
多谢各位的帮忙,我按各位的方法试过了,直接用AnsiToUtf8函数是不行的。要对该函数返回的信息进行处理,处理后才正确。<br>希望以后还能得到各位的帮助。
 
后退
顶部