delphi2009 字符串转codepage编码

delphi2009中使用自带的TEncoding类进行解码时,部分utf-8字符无法正确解码,阿拉伯字符全部都为问号。

所以使用api函数进行转换。

type

    BArr=array of byte;

Function Decode2Bytes(strInput:widestring;CodepageIdentifier:cardinal):BArr;

var
    TLen,lngBufferSize,lngResult,i:integer;
    bytResult:BArr;
begin
    TLen := Length(strInput);
    If TLen = 0 Then
        Exit;

    lngBufferSize := TLen * 3 + 1;
    setlength(bytResult,(lngBufferSize - 1));


    lngResult := WideCharToMultiByte(CodepageIdentifier, 0, @strInput[1], TLen, @bytResult[0], lngBufferSize, nil, 0);


    setlength(result,lngresult);
    for i := 0 to lngResult - 1 do
        result[i]:=bytResult[i];

End;


PS:

1.BArr为自定义byte数组类型。

2.CodepageIdentifier源自微软https://msdn.microsoft.com/en-us/library/dd317756(VS.85).aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unit CodeSoft; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DdeMan, ComObj, stdctrls, IniFiles; type TDMCodeSoft = class(TDataModule) CSDdeClientConv: TDdeClientConv; procedure DataModuleCreate(Sender: TObject); procedure DataModuleDestroy(Sender: TObject); private M_sOldFileName : String; M_slCSParam : TStringList; function csFileName(psFileName: String): String; procedure csSet(psFieldName,psFieldValue: String); procedure listVar; procedure closeLink; procedure startLink; public procedure CSFullScreen; procedure CSLockScreen; function printToCodeSoft(psLabelFile:string; piLabelQty:integer; plsListBoxItem,plsListBoxData:TObject):Boolean; end; var DMCodeSoft: TDMCodeSoft; implementation {$R *.DFM} function TDMCodeSoft.printToCodeSoft(psLabelFile:string; piLabelQty:integer; plsListBoxItem, plsListBoxData:TObject):Boolean; var i : integer; begin if not FileExists(psLabelFile) then begin showmessage('error!-The Label file ' + psLabelFile+' can''t be found.'); Result := False; exit; end; Result := True; try if M_sOldFileName <> psLabelFile then begin if M_sOldFileName <> '' then begin while True do begin if not CSDdeClientConv.WaitStat then begin CSDdeClientConv.ExecuteMacro(PChar('[CloseLab('+csFileName(M_sOldFileName)+')]'),False); Break; end; end; end; while True do begin if not CSDdeClientConv.WaitStat then begin CSDdeClientConv.ExecuteMacro(PChar('[OpenLab('+csFileName(psLabelFile)+')]'),False); M_sOldFileName := psLabelFile; Break; end; end; end; listVar; for i := 1 to M_slCSParam.Count do begin if (plsListBoxItem as TListBox).Items.IndexOf(M_slCSParam[i-1]) <> -1 then begin csSet((plsListBoxItem as TListBox).Items[i-1],'"'+ (plsListBoxData as TListBox).Items[(plsListBoxItem as TListBox).Items.IndexOf(M_slCSParam[i-1])]+'"'); end; end; while True do begin if not CSDdeClientConv.WaitStat then begin CSDdeClientConv.ExecuteMacro(PChar('[PrintLabel('+IntToStr(piLabelQty)+')]'),False); Break; end; end; while True do begin if not CSDdeClientConv.WaitStat then begin CSDdeClientConv.ExecuteMacro(PChar('[FormFeed()]'),False); Break; end; end; except end; end; procedure TDMCodeSoft.CSFullScreen; begin while True do begin if not CSDdeClientConv.WaitStat then begin CSDdeClientConv.ExecuteMacro(PChar('[Display(F)]'),False); Break; end; Sleep(500); end; end; procedure TDMCodeSoft.CSLockScreen; begin while True do begin if not CSDdeClientConv.WaitStat then begin CSDdeClientConv.ExecuteMacro(PChar('[Display(K)]'),False); Break; end; Sleep(500); end; end; procedure TDMCodeSoft.closeLink; begin CSDdeClientConv.PokeData('Close','1'); CSDdeClientConv.CloseLink; end; function TDMCodeSoft.csFileName(psFileName: String): String; var sFileNameTemp: String; iCount: Integer; begin for iCount := 1 to Length(psFileName) do begin case psFileName[iCount] of '\': sFileNameTemp := sFileNameTemp + '\\'; '[': sFileNameTemp := sFileNameTemp + '\['; ']': sFileNameTemp := sFileNameTemp + '\]'; '"': sFileNameTemp := sFileNameTemp + '\"'; ',': sFileNameTemp := sFileNameTemp + '\,'; '(': sFileNameTemp := sFileNameTemp + '\('; ')': sFileNameTemp := sFileNameTemp + '\)'; else sFileNameTemp := sFileNameTemp + psFileName[iCount]; end; end; Result := sFileNameTemp; end; procedure TDMCodeSoft.csSet(psFieldName,psFieldValue: String); begin while True do if not CSDdeClientConv.WaitStat then begin CSDdeClientConv.ExecuteMacro(PChar('[Set('+psFieldName+','+psFieldValue+')]'),False); Break; end; end; procedure TDMCodeSoft.listVar; var sTextTemp, sText: String; iCount: Integer; begin sTextTemp := ''; sText := ''; M_slCSParam.Clear; while True do begin if not CSDdeClientConv.WaitStat then begin sTextTemp := StrPas(CSDdeClientConv.RequestData('VARLIST')); Break; end; end; for iCount := 1 to Length(sTextTemp) do begin if sTextTemp[iCount] = ';' then begin M_slCSParam.Add(sText); sText := ''; end else sText := sText + sTextTemp[iCount]; end; if sText <> '' then M_slCSParam.Add(sText); end; procedure TDMCodeSoft.startLink; var WinINI, CSINI: TIniFile; CSPath, CSEXE: String; begin WinINI := TIniFile.Create('Win.INI'); try CSPath := WinINI.ReadString('CS','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('CS4DMX','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('ELTPLUS','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('LSPRO','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('CSRUN','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('DMXRUN','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('ELTRUN','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('LSRUN','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('LWISE','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('LWRUN','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('IPAL','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('IPALRUN','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('IMPULS','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('IMPRUN','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('VITA','Path',''); if CSPath = '' then CSPath := WinINI.ReadString('VITARUN','Path',''); finally WinINI.Free; end; if CSPath = '' then begin MessageDLG('Please install Code soft!!',mtError,[mbOK],0); Application.Terminate; end; CSINI := TIniFile.Create(CSPath+'\CS.INI'); try CSEXE := CSINI.ReadString('General','ExeName',''); finally CSINI.Free; end; CSDdeClientConv.ServiceApplication := CSPath+'\'+CSEXE; CSDdeClientConv.SetLink('CS','CS'); while True do begin if CSDdeClientConv.OpenLink then Break; CSDdeClientConv.ServiceApplication := ''; sleep(500); end; end; procedure TDMCodeSoft.DataModuleCreate(Sender: TObject); begin startLink; CSLockScreen; M_slCSParam := TStringList.Create; end; procedure TDMCodeSoft.DataModuleDestroy(Sender: TObject); begin M_slCSParam.Free; closeLink; end; end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值