Delphi 字符类转换集《转》

一、string转为ansistring
1、直接赋值 (有警告)
2、ansistring()类型强制转换。(无警告)

二、ansistring 转为string

1、直接赋值 (有警告)
2、string()类型强制转换。(无警告)

三、string 转为Tbytes

1、bytes:= bytesof(str) 已转为ansi编码
2、bytes:= widebytesof(str) UNICODE 编码

四、ansistring 转为Tbytes

1、bytes:= bytesof(str) ansi编码
2、bytes:= widebytesof(string(str)) UNICODE 编码

五、Tbytes 转为string

1、 str:=stringof(bytes) Tbytes 为ansi编码
2、 str:=widestringof(bytes) Tbytes 为unicode编码

六、PChar转String

用StrPas函数,StrPas(PChar):AnsiString;



设有以下三个变量: 

var 
s:string; 
p:pchar; 
a:array[1..20] of char; 
那么三者之间的转换如下: 
1、字符串到PChar 
    p:=PChar(s); 
2、PChar到字符串 
    s:=p; 
3、PChar到字符数组 
    StrCopy(@a,p); 
4、字符数组到PChar 
    PChar(@a); 
5、字符串与字符数组之间的转换就只有通过PChar来中转了。例如下面这个例子: 
procedure TForm1.btn1Click(Sender: TObject); 
var 
str:array[1..10] of char; 
begin 
StrCopy(@str,PChar(mmo1.Text)); 
mmo2.Text:=PChar(@str); 
end;

以下是本人自己编写发送短信的一段代码,实现字符串转UTF8编码

  //SMS
  if IsPhoen(edtTEL.Text) then
    begin
      SysconfigRun;
      if Sysconfig.ReadBool('SMS','AddingSend',False) then
        begin
          if Sysconfig.ReadInteger('SMS','AddingMode',0) = 0 then
            begin
               if MessageBox(Handle, PChar('是否发送短信?                                         '),
               PChar('短信'), MB_ICONINFORMATION or MB_YESNO or MB_DEFBUTTON1) = IDYES then
                 begin
                   goto GoToS;
                 end;
            end
          else
            begin
              GoToS :
              Params := TStringList.Create;
              IDHTTP1 := TIdHTTP.Create(nil);
              reg := TPerlRegEx.Create;
              Params.Add('ac='+trim(Sysconfig.ReadString('SMS','SMSA','')));
              Params.Add('authkey='+trim(Sysconfig.ReadString('SMS','SMSP','')));
              Params.Add('m='+trim(edtTEL.Text));
              Params.Add('action=sendOnce');
              Params.Add('cgid='+trim(Sysconfig.ReadString('SMS','CGID','')));

              Params.Add('csid='+trim(Sysconfig.ReadString('SMS','CSID','')));
              Params.Add('c='+Utf8ToAnsi(UTF8Encode(Matching(edtTID.Text,edtUserName.Text,edt2.Text,trim(Sysconfig.ReadString('SMS','AddingInfo',DateTimeToStr(Now)))))));
              if Sysconfig.ReadString('SMS','AddingInfo','') <> '' then
                begin
                  WriteLog(IDHTTP1.Post('http://smsapi.c123.cn/OpenPlatform/OpenApi', Params));
                end;
              FreeAndNil(IDHTTP1);
              FreeAndNil(Params);
              FreeAndNil(reg);
            end;
        end;
      Sysconfig.Destroy;
    end;

 

转载于:https://www.cnblogs.com/LceMeaning/p/3503237.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值