1、将汉字编码:
var s: widestring; i, j: integer; tmpstr: string; c2: WideChar; begin for j := 0 to Memo2.lines.count - 1 do begin tmpstr := ''; s := Memo2.Lines[j]; for i := 1 to length(s) do begin tmpstr := tmpstr + inttostr(ord(s[i])) + ','; end; end; Memo1.Lines.Add(tmpstr + '#'); end;
2、根据汉字编码生成二维码:
const sFileName = 'QRCode.bmp'; var oBmp: TBitmap; begin CreateQRCode(AnsiToUtf8(memo1.Lines.Text), 0, 1, 3); if FileExists(sFileName) then begin oBmp := TBitmap.Create; oBmp.LoadFromFile(sFileName); image1.Picture.Assign(oBmp); FreeAndNil(oBmp); end; end;
3、将编码转换成汉字(解码)
var tmplist: TStringList; tmpstr: string; j, i: Integer; c2: WideChar; begin try tmplist := TStringList.Create(); tmpstr := Copy(Self.Edit1.Text, 0, Length(Self.Edit1.Text) - 1); tmplist := SplitString(tmpstr, ','); for j := 0 to tmplist.Count - 1 do begin if tmplist[j] <> '' then begin i := StrToInt(tmplist[j]); c2 := widechar(i); Memo1.Lines.Add(c2) end; end; finally FreeAndNil(tmplist); end; end;
4、用到的splitstring函数:
function SplitString(const Source, ch: string): TStringList; var temp: string; i: Integer; begin Result := TStringList.Create; //如果是空自符串则返回空列表 if Source = '' then exit; temp := Source; i := pos(ch, Source); while i <> 0 do begin Result.add(copy(temp, 0, i - 1)); Delete(temp, 1, i); i := pos(ch, temp); end; Result.add(temp); end;