由于Lazarus从1.2版开始默认字符集就是UTF8,如果要转到系统正常显示或文本保存,就必须对字符集进行转换。Lazarus提供了很多函数。如题。

那么这里面有什么关系呢?

UTF8ToSys 需要 启用编译参数 –dDisableUTF8RTL,否则还是UTF8,如果变码还是Utf8ToAnsi,但做了基本的处理。

对应代码

function UTF8ToSys(const s: string): string;
begin
{$IFDEF UTF8_RTL}
Result:=s;
{$ELSE}
if NeedRTLAnsi and (not IsASCII(s)) then
Result:=UTF8ToAnsi(s)
else
Result:=s;
{$ENDIF}
end;

Utf8ToAnsi是freePascal 自带的,变码道Ansi ,在ustringh.inc文件里面

function Utf8ToAnsi(const s : RawByteString) : RawByteString;{$ifdef SYSTEMINLINE}inline;{$endif}
begin
Result:=RawByteString(Utf8Decode(s));
end;
{$ifdef FPC_HAS_CPSTRING}
RawByteString = type AnsiString(CP_NONE);
{$else FPC_HAS_CPSTRING}
RawByteString = ansistring;
{$endif FPC_HAS_CPSTRING}

UTF8ToWinCP 是Lazarus 针对Windows 功能封包,不变化代码页,仅仅变化字符编码

{$ifdef WinCe}
function UTF8ToWinCP(const s: string): string; inline;
begin
Result := Utf8ToSys(s);
end;
{$else}
function UTF8ToWinCP(const s: string): string;
// result has codepage CP_ACP
var
src: UnicodeString;
len: LongInt;
begin
Result:=s;
if IsASCII(Result) then begin
{$ifdef FPC_HAS_CPSTRING}
// prevent codepage conversion magic
SetCodePage(RawByteString(Result), CP_ACP, False);
{$endif}
exit;
end;
src:=UTF8Decode(s);
if src='' then
exit;
len:=WideCharToMultiByte(CP_ACP,0,PUnicodeChar(src),length(src),nil,0,nil,nil);
SetLength(Result,len);
if len>0 then begin
WideCharToMultiByte(CP_ACP,0,PUnicodeChar(src),length(src),@Result[1],length(Result),nil,nil);
{$ifdef FPC_HAS_CPSTRING}
// prevent codepage conversion magic
SetCodePage(RawByteString(Result), CP_ACP, False);
{$endif}
end;
end;
{$endif not wince}

UTF8ToConsole 是Lazarus 针对Windows 功能封包 ,变码后,同时会配置成默认代码页文本。

{$ifdef WinCe}
function UTF8ToConsole(const s: string): string; // converts UTF8 to console string (used by Write, WriteLn)
begin
Result := UTF8ToSys(s);
end;
{$else}
function UTF8ToConsole(const s: string): string; // converts UTF8 to console string (used by Write, WriteLn)
var
Dst: PChar;
begin
{$ifndef NO_CP_RTL}
Result := UTF8ToWinCP(s);
{$else NO_CP_RTL}
Result := UTF8ToSys(s); // Kept for compatibility
{$endif NO_CP_RTL}
Dst := AllocMem((Length(Result) + 1) * SizeOf(Char));
if CharToOEM(PChar(Result), Dst) then
Result := StrPas(Dst);
FreeMem(Dst);
{$ifndef NO_CP_RTL}
SetCodePage(RawByteString(Result), CP_OEMCP, False);
{$endif NO_CP_RTL}
end;
{$endif not WinCE}

Lazarus 字符集转换 Utf8ToAnsi,UTF8ToWinCP,UTF8ToSys,UTF8ToConsole的更多相关文章

  1. mysql已有数据字符集转换

    下面模拟把latin1字符集的数据转换为utf8字符集 一.创建测试表和测试数据: 1.修改会话级别的连接字符集 mysql > set names latin1; 查看一下: 2.创建测试表: ...

  2. 字符集转换: Ansi - Unicode

    字符集转换: Ansi - Unicode wstring AnsiToUnicode (const string& strSrc ) { /*!< 分配目标空间 */ ,strSrc. ...

  3. 字符集转换: Unicode - Ansi

    字符集转换: Unicode - Ansi string UnicodeToAnsi ( const wstring& wstrSrc ) { /*!< 分配目标空间, 一个16位Uni ...

  4. 利用boost做string到wstring转换,以及字符集转换 - Error - C++博客

    利用boost做string到wstring转换,以及字符集转换 - Error - C++博客 利用boost做string到wstring转换,以及字符集转换 #include <boost ...

  5. libiconv字符集转换库在C#中的使用

    <libiconv字符集转换库用法>一文中说到了libiconv能够实现不同字符集的转换. 比方GBK转BIG5等.在项目中由于须要,找到这个库.但是这个库在C#中没有非常好的支持.只是, ...

  6. 部署OGG时字符集转换问题--oracle to oracle已验证,其他异构环境应当也适用

    之前在安装OGG总是遇到字符集问题,尤其是多源端对一个目标端时,源端字符集不同,导致出现字符集问题 无法同步数据,查阅了大量的园子资料,都说要设置复制或抽取进程中SETENV (NLS_LANG=AM ...

  7. MySQL字符集转换引发插入乱码问题

    根据http://www.cnblogs.com/cchust/p/4601536.html进行验证测试 问题背景 在mysql上面执行一条普通的insert语句,结果报错: Incorrect st ...

  8. Oracle字符集转换

            这几天在工作中碰到一个字符乱码的问题,发现在cmd窗口的sqlplus中直接update一个中文和使用@调用一个文件作同样更新的时候,存储的结果 竟不一样.一时比较迷惑,对Oracle ...

  9. 彻底理解mysql服务器的字符集转换问题

    主要参考这三个文章: https://www.xiariboke.com/article/4147.html http://blog.sina.com.cn/s/blog_690c46500100k1 ...

随机推荐

  1. appium desktop

    Appium-desktop 下载地址:https://github.com/appium/appium-desktop/releases 一般功能 这些能力跨越多个驱动因素. 仅限Android 这 ...

  2. javascript返回顶端源代码

    <div style="display:none" id="goTopBtn"><img src="http://www.unity ...

  3. 抢占式内核与非抢占式内核中的自旋锁(spinlock)的差别

    一.概括 (1)自旋锁适用于SMP系统,UP系统用spinlock是作死. (2)保护模式下禁止内核抢占的方法:1.运行终端服务例程时2.运行软中断和tasklet时3.设置本地CPU计数器preem ...

  4. 75. Autorelease机制及释放时机

    Autorelease机制是iOS开发人员管理对象内存的好伙伴.MRC中.调用[obj autorelease]来延迟内存的释放是一件简单自然的事:ARC下,我们甚至能够全然不知道Autoreleas ...

  5. restlet 2.3.5 org.restlet包导入eclipse出现的com.sun.net.httpserver类包找不到问题

    准备过一遍restlet 2.3.5 JavaEE的源码. 环境 eclipse3.7.2 和 jdk 7.0 将org.restlet 包增加到eclipse中.出现 com.sun.net.htt ...

  6. 菜鸟的mongoDB学习---(五)MongoDB的limit、skip、sort方法

    limit方法 假设你须要在MongoDB中读取指定数量的数据记录.能够使用MongoDB的Limit方法,limit()方法接受一个数字參数,该參数指定从MongoDB中读取的记录条数. mongo ...

  7. tf.image.resize_bilinear 图像缩放,双线性插值-图像中心对齐

    http://www.cnblogs.com/yssongest/p/5303151.html 双线性插值算法及需要注意事项 input = tf.placeholder(tf.float32, sh ...

  8. BSGS算法及拓展

    https://www.zybuluo.com/ysner/note/1299836 定义 一种用来求解高次同余方程的算法. 一般问题形式:求使得\(y^x\equiv z(mod\ p)\)的最小非 ...

  9. matlab绘制曲线对比图

    >> clear;>> x1=[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8];>> y1=[0,0.55,0.69,0.86,0.93,0. ...

  10. 在Android.mk文件中输出打印消息 (转载)

    转自:http://blog.csdn.net/xiaibiancheng/article/details/8479694 在进行Android NDK的开发当中有时想看看Android.mk文件当中 ...