zoukankan      html  css  js  c++  java
  • ansistring和unicode的序列和还原

    ansistring和unicode序列

    delphi2009以前的所有版本,默认字符串都是ansistring,字符串中的一个字符刚好就是一个字节。

    后面的DELPHI版本,默认字符串是unicode,字符串中的一个字符不再是一个字节。

    那么,可以写一个通用的ansistring和unicode序列函数,让它适配所有的delphi版本。

    FValue: TBytes;

    procedure TMsgPack.setAsString(pvValue: string);
    begin
      FDataType := mptString;
      if SizeOf(Char) = 2 then //unicode
      begin
        SetLength(FValue, length(pvValue) shl 1);
        Move(PChar(pvValue)^, FValue[0], Length(FValue));
      end else //ansistring
      begin
        SetLength(FValue, length(pvValue));
        Move(PChar(pvValue)^, FValue[0], Length(FValue));
      end;
    end;
    

      

    function TMsgPack.getAsString: String;
    var
      len:Cardinal;
    begin
        len := Length(FValue);
        if SizeOf(Char) = 2 then //unicode
        begin
          SetLength(Result, len shr 1);
          Move(FValue[0],PChar(Result)^, len);
        end else  //ansistring
        begin
          SetLength(Result, len);
          Move(FValue[0],PChar(Result)^, len);
        end;
    end;
    

      

  • 相关阅读:
    类的定义
    面向对象与面向过程介绍
    跨目录导入模块
    正则表达式re模块
    常用工具包(模块)
    生成器generator
    闭包
    命名空间name space
    函数的递归
    POJ1062
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/13716073.html
Copyright © 2011-2022 走看看