如何将区位码转换成汉字?? 请指教! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 delphi写的,转成java吧function GetHz(qm, wm: Integer): String;beginResult := Chr(160+qm)+Chr(160+wm);end;procedure GetQWCode(HZ: string; var Q, W: Word);beginQ := Byte(HZ[1]) - $A0; 这是区码W := Byte(HZ[2]) - $A0; 这是位码end;function GetHZCode(vHZ: string): string;//vHZ:一个汉字//返回区位码字符串,GB2312中没有的字返回0000varQM, WM: Integer;tStr: string;begintStr := ;QM := Ord(vHZ[1])-160;WM := Ord(vHZ[2])-160;if (QM<0) or (WM<0) thenbeginQM := 0;WM := 0;end;if QM <10 thentStr := 0+IntToStr(QM)elsetStr := IntToStr(QM);if WM < 10 thentStr := tStr+0+IntToStr(WM)elsetStr := tStr+IntToStr(WM);GetHZCode := tStr;end; 这里有C#写的,如何把里面的方法转成java的方法呢:public string CodingToCharacter(string coding) { string characters = ""; if (coding.Length % 4 != 0)//编码为16进制,必须为4的倍数。 { throw new System.Exception("编码格式不正确"); } for (int i = 0; i<coding.Length; i+=4 ) //每四位为一个汉字 { byte[] bytes = new byte[2]; string lowCode = coding.Substring(i, 2); //取出低字节,并以16进制进制转换 bytes[0] = System.Convert.ToByte(lowCode, 16); string highCode = coding.Substring(i + 2, 2); //取出高字节,并以16进制进行转换 bytes[1] = System.Convert.ToByte(highCode, 16); string character = System.Text.Encoding.Unicode.GetString(bytes); characters += character; } return characters; } 已经解决了,供大家参考一下:public String CodeToChinese (String code) { String Chinese = ""; for (int i = 0; i < code.length(); i +=4) { byte[] bytes = new byte[2]; String lowCode = code.substring(i, i+2); int tempLow = Integer.parseInt(lowCode, 16); tempLow += 160; bytes[0] = (byte) tempLow; String highCode = code.substring(i+2, i+4); int tempHigh = Integer.parseInt(highCode, 16); tempHigh += 160; bytes[1] = (byte) tempHigh; String chara = new String(bytes); Chinese += chara; } return Chinese;} to: liuzichang(刘子)为什么tempLow 和tempHigh要加上160? Swing 界面其实不难看,懂SwingC.jar的进 总分120+分 求几个不长有趣的小程序,功能月独特越好,不要大众教材 AbstractCollection的removeAll方法实现的一个小疑问 关于标签的问题。。。为什么这里会死循环。。。。 String 对象的问题 这样的问题能解决吗? 编译都通过了,就是运行不起来。。新人跪求 请大家给个建议! 能否将File转成ASCII并存在XML中,如何实现? 如何计算一个字符串表达式的值? 将\u5f53转换为汉字? 使用getImage方法装载图像,但是不能显示,有时显示,会提示outofmemory的异常。如何解决
function GetHz(qm, wm: Integer): String;
begin
Result := Chr(160+qm)+Chr(160+wm);
end;
procedure GetQWCode(HZ: string; var Q, W: Word);
begin
Q := Byte(HZ[1]) - $A0; 这是区码
W := Byte(HZ[2]) - $A0; 这是位码
end;
function GetHZCode(vHZ: string): string;
//vHZ:一个汉字
//返回区位码字符串,GB2312中没有的字返回0000
var
QM, WM: Integer;
tStr: string;
begin
tStr := ;
QM := Ord(vHZ[1])-160;
WM := Ord(vHZ[2])-160;
if (QM<0) or (WM<0) then
begin
QM := 0;
WM := 0;
end;if QM <10 then
tStr := 0+IntToStr(QM)
else
tStr := IntToStr(QM);
if WM < 10 then
tStr := tStr+0+IntToStr(WM)
else
tStr := tStr+IntToStr(WM);GetHZCode := tStr;
end;
public string CodingToCharacter(string coding)
{
string characters = "";
if (coding.Length % 4 != 0)//编码为16进制,必须为4的倍数。
{
throw new System.Exception("编码格式不正确");
}
for (int i = 0; i<coding.Length; i+=4 ) //每四位为一个汉字
{
byte[] bytes = new byte[2];
string lowCode = coding.Substring(i, 2); //取出低字节,并以16进制进制转换
bytes[0] = System.Convert.ToByte(lowCode, 16);
string highCode = coding.Substring(i + 2, 2); //取出高字节,并以16进制进行转换
bytes[1] = System.Convert.ToByte(highCode, 16);
string character = System.Text.Encoding.Unicode.GetString(bytes);
characters += character;
}
return characters;
}
byte[] bytes = new byte[2];
String lowCode = code.substring(i, i+2);
int tempLow = Integer.parseInt(lowCode, 16);
tempLow += 160;
bytes[0] = (byte) tempLow;
String highCode = code.substring(i+2, i+4);
int tempHigh = Integer.parseInt(highCode, 16);
tempHigh += 160;
bytes[1] = (byte) tempHigh;
String chara = new String(bytes);
Chinese += chara;
}
return Chinese;}
为什么tempLow 和tempHigh要加上160?