delphi在新版本的字符串默认编码都是unicode编码,但有些场合需要使用utf-8编码后的ansistring,比如常用的aes加密。
在delphi2010下有个函数可以使用
如果字符串大小不超过256个字符的,可以直接使用UTF8EncodeToShortString,返回值为utf-8编码的ansistring
如果更多的字符内容,那么使用Utf8Encode函数,返回值为RawByteString,可直接复制为ansistring
function aes_encrypt(const s: string): string;
var ss: ansistring;
ww: RawByteString;
begin
ww:= Utf8Encode(s); //编码为utf-8
SetString(ss, PAnsiChar(ww), Length(ww)); //复制为ansistring
result:= EncryptString(ss,'0000000000000001'); //自定义aes cbc加密
end;
本文介绍在 Delphi 中如何将 Unicode 字符串转换为 UTF-8 编码的 ANSI 字符串,包括使用 UTF8EncodeToShortString 和 Utf8Encode 函数的方法,并给出 AES 加密的应用实例。

418

被折叠的 条评论
为什么被折叠?



