如何把GBK内码转换成汉字。
比如‘好’的GBK内码是‘BAC3’,用JAVA如何使控制台输出的是‘好'。有一个字符串‘=BA=C3',使控制台输出的是‘好’。...
比如‘好’的GBK内码是‘BAC3’,用JAVA如何使控制台输出的是‘好'。
有一个字符串‘=BA=C3',使控制台输出的是‘好’。 展开
有一个字符串‘=BA=C3',使控制台输出的是‘好’。 展开
3个回答
展开全部
代码如下:
private void btnconvert_Click(object sender, EventArgs e)
{
string sSou = txtyuanwenjian.Text;
Encoding CnEnconding = Encoding.GetEncoding("GB2312");
byte[] bSou = CnEnconding.GetBytes(sSou); //转成数组
for (int i = 0; i < bSou.Length; i++)
{
string strvalue = convertting(bSou[i].ToString(), 10, 2);
if (strvalue.Length <= 8)
{
int cha = 8 - strvalue.Length ;
for (int j = 0; j < cha; j++)
{
strvalue = strvalue.Insert(0, "0");
}
}
txtzhuanhuan.Text = txtzhuanhuan.Text + strvalue; //依次取得
} public string convertting(string value, int baseform, int tobase)
{
long intvalue = Convert.ToInt64(value, baseform);
return Convert.ToString(intvalue, tobase);
}
private void button1_Click(object sender, EventArgs e)
{
string str2 = txtzhuanhuan.Text.Trim();
for (int x = 0; x < str2.Length; x++)
{
if (str2.Substring(0, 1) == "0")
{
string str0 = str2.Substring(x, 8);
string shijinzhi = convertting(str0, 2, 10);
txtchar.Text = txtchar.Text + Convert.ToChar(Convert.ToInt32(shijinzhi)).ToString();
x = x + 7;
}
else
{
string str0 = str2.Substring(x, 16);
string shijinzhi = convertting(str0, 2, 10);
txtchar.Text = txtchar.Text + Convert.ToChar(Convert.ToInt32(shijinzhi));
x = x + 15;
}
}
private void btnconvert_Click(object sender, EventArgs e)
{
string sSou = txtyuanwenjian.Text;
Encoding CnEnconding = Encoding.GetEncoding("GB2312");
byte[] bSou = CnEnconding.GetBytes(sSou); //转成数组
for (int i = 0; i < bSou.Length; i++)
{
string strvalue = convertting(bSou[i].ToString(), 10, 2);
if (strvalue.Length <= 8)
{
int cha = 8 - strvalue.Length ;
for (int j = 0; j < cha; j++)
{
strvalue = strvalue.Insert(0, "0");
}
}
txtzhuanhuan.Text = txtzhuanhuan.Text + strvalue; //依次取得
} public string convertting(string value, int baseform, int tobase)
{
long intvalue = Convert.ToInt64(value, baseform);
return Convert.ToString(intvalue, tobase);
}
private void button1_Click(object sender, EventArgs e)
{
string str2 = txtzhuanhuan.Text.Trim();
for (int x = 0; x < str2.Length; x++)
{
if (str2.Substring(0, 1) == "0")
{
string str0 = str2.Substring(x, 8);
string shijinzhi = convertting(str0, 2, 10);
txtchar.Text = txtchar.Text + Convert.ToChar(Convert.ToInt32(shijinzhi)).ToString();
x = x + 7;
}
else
{
string str0 = str2.Substring(x, 16);
string shijinzhi = convertting(str0, 2, 10);
txtchar.Text = txtchar.Text + Convert.ToChar(Convert.ToInt32(shijinzhi));
x = x + 15;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String s = "BAC3";
byte[] buf = new byte[2];
buf[0] = (byte)Integer.parseInt( s.substring(0,2), 16 );
buf[1] = (byte)Integer.parseInt( s.substring(2,4), 16 );
System.out.println( new String(buf,"GBK") );
byte[] buf = new byte[2];
buf[0] = (byte)Integer.parseInt( s.substring(0,2), 16 );
buf[1] = (byte)Integer.parseInt( s.substring(2,4), 16 );
System.out.println( new String(buf,"GBK") );
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询