Delphi 中String、ANSIString、TBytes之间的转换
<p>这个问题要<font face="Verdana" size="2">从最近一个项目谈起,服务器端要求UTF8编码,Delphi内部是UTF16编码,为了避免风险我将代码中数据都定义为AnsiString,但实际还是除了些问题。</font></p><p>delphi7下</p><p>buffer是tbytes型,temp是string型</p><p></p><div class="cnblogs_code" style="padding: 5px; border: 1px solid rgba(204, 204, 204, 1); border-image: none; background-color: rgba(245, 245, 245, 1)"><pre>temp := <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">(pointer(buffer));setlength(temp,length(buffer));
CopyMemory(Pointer(temp), @buffer[</span><span style="color: rgba(128, 0, 128, 1)">0</span>], Length(buffer));</pre></div>到了XE后,也许经过dot Net洗礼后,下面的代码更好理解,当然这是站在开发者的角度。<div class="cnblogs_code" style="padding: 5px; border: 1px solid rgba(204, 204, 204, 1); border-image: none; background-color: rgba(245, 245, 245, 1)"><pre><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
Buf : TBytes;
S : String;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">AnsiChar内存数组->UnicodeString</span>
SetLength(Buf , <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">);
Buf[</span><span style="color: rgba(128, 0, 128, 1)">0</span>] := <span style="color: rgba(128, 0, 128, 1)">65</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">A的Ansii码</span>
Buf[<span style="color: rgba(128, 0, 128, 1)">1</span>] := <span style="color: rgba(128, 0, 128, 1)">66</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">B~~</span>
S := TEncoding.ANSI.GetString(Buf);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">AnsiChar内存数组->UnicodeString</span>
<span style="color: rgba(0, 0, 0, 1)">ShowMessage(S);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">UnicodeString -> AnsiChar内存数组</span>
SetLength(Buf , <span style="color: rgba(128, 0, 128, 1)">0</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这句是测试时使用,实际使用时,不需要先清除</span>
Buf := TEncoding.Unicode.GetBytes(S); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">UnicodeString -> AnsiChar内存数组</span>
ShowMessage(IntToStr(Buf[<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">]));
</span><span style="color: rgba(0, 0, 255, 1)">end</span>;</pre></div><p>所谓聪明的人用Pascal,Delphi 站在使用者角度可以这么写</p><p>uses SysUtils;<br>
</p><p>一、string转为ansistring<br>
1、直接赋值 (有警告)<br>
2、ansistring()类型强制转换。(无警告)</p>
<p>二、ansistring 转为string</p>
<p>1、直接赋值 (有警告)<br>
2、string()类型强制转换。(无警告)</p>
<p>三、string 转为Tbytes</p>
<p>1、bytes:= bytesof(str) 已转为ansi编码<br>
2、bytes:= widebytesof(str) UNICODE 编码</p>
<p>四、ansistring 转为Tbytes</p>
<p>1、bytes:= bytesof(str) ansi编码<br>
2、bytes:= widebytesof(string(str)) UNICODE 编码</p>
<p>五、Tbytes 转为string</p>
<p>1、 str:=stringof(bytes) Tbytes 为ansi编码<br>
2、 str:=widestringof(bytes) Tbytes 为unicode编码</p>
<p>六、PChar转String</p>
<p>用StrPas函数,StrPas(PChar):AnsiString;</p><p><br></p><p>当然这内部大部分还是还是如下之类!</p><p> Result := TEncoding.UTF8.GetString(TEncoding.UTF8.GetBytes(sTemp));</p><p>这里不得不说说,lazarus/FPC,UTF8作为默认编码,而且强制几次就OK方便很多。</p><p>
————————————————
<br></p>
<p>部分内容引用<br>
原文链接:https://blog.csdn.net/u011706768/article/details/119566174「暗夜魔尊」<br>原文链接:https://blog.csdn.net/zxm8513/article/details/104728514 「zxm8513」</p><br><br>
来源:https://www.cnblogs.com/hieroly/p/15553542.html
頁:
[1]