Delphi:从Windows-1251转换为Shift-JIS [英] Delphi: Convert from windows-1251 to Shift-JIS

查看:67
本文介绍了Delphi:从Windows-1251转换为Shift-JIS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串’MIROKU’。我想将此字符串转换为’%82l%82h%82q%82n%82j%82t’。以下是我当前用于转换的函数:

I have a string 'MIROKU'. I want to convert this string to '%82l%82h%82q%82n%82j%82t'. Below is my current function which I have used for converting:

function MyEncode(const S: string; const CodePage: Integer): string;
var
  Encoding: TEncoding;
  Bytes: TBytes;
  b: Byte;
  sb: TStringBuilder;
begin
  Encoding := TEncoding.GetEncoding(CodePage);
  try
    Bytes := Encoding.GetBytes(S);
  finally
    Encoding.Free;
  end;

  sb := TStringBuilder.Create;
  try
    for b in Bytes do begin
      sb.Append('%');
      sb.Append(IntToHex(b, 2));
    end;
    Result := sb.ToString;
  finally
    sb.Free;
  end;
end;

MyEncode('MIROKU',932)返回'%82%6C%82%68%82%71%82%6E%82%6A%82%74'。我没想到会有这个结果。我期望’%82l%82h%82q%82n%82j%82t’。有任何函数可以正确转换它?

MyEncode('MIROKU', 932) returns '%82%6C%82%68%82%71%82%6E%82%6A%82%74'. I don't expect this result. I expect '%82l%82h%82q%82n%82j%82t'. Are there any functions to convert it correctly?

推荐答案

您所看到的是正确的,而不是您所期望的。例如,%6C l 的ascii表示。因此,您可以尝试这样的事情:

What you are seeing is correct, just not what you are expecting. %6C, for example, is the ascii representation for l. So you could try something like this instead:

function MyEncode(const S: string; const CodePage: Integer): string;
var
  Encoding: TEncoding;
  Bytes: TBytes;
  b: Byte;
  sb: TStringBuilder;
begin
  Encoding := TEncoding.GetEncoding(CodePage);
  try
    Bytes := Encoding.GetBytes(S);
  finally
    Encoding.Free;
  end;

  sb := TStringBuilder.Create;
  try
    for b in Bytes do begin
      if (b in [65..90]) or (b in [97..122]) then
      begin
        sb.Append( char(b)); // normal ascii
      end  
      else
      begin
        sb.Append('%');
        sb.Append(IntToHex(b, 2));
      end;
    end;
    Result := sb.ToString;
  finally
    sb.Free;
  end;
end;

或者您可以保留它!

这篇关于Delphi:从Windows-1251转换为Shift-JIS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆