怎么gbk内码转为汉字

ahmatjan20041301410 2009-04-22 06:32:50
我在编一个程序,输入一个字符转换为二进制码,比如输入A,对应的二进制码01000001,但是输入一个汉字,比如输入“阿”,对应的二进制码1011000010100010,现在的问题是对应的二进制转到字符的时候,字能转换到了,但是汉字变成了乱码,汉字的二进制码是他的gbk内码,
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;
}
}

各位高手帮我解决一下吧,汉字的GBK内码怎么转到汉字
...全文
1042 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanbo 2009-04-28
  • 打赏
  • 举报
回复
good good study
jie3614 2009-04-23
  • 打赏
  • 举报
回复
哦哦 Up一下
mzjmicrosoft 2009-04-22
  • 打赏
  • 举报
回复
very good
study
UP
周公 2009-04-22
  • 打赏
  • 举报
回复
下面的代码示例将字符串从一种编码转换为另一种编码。

using System;
using System.Text;

namespace ConvertExample
{
class ConvertExampleClass
{
static void Main()
{
string unicodeString = "This string contains the unicode character Pi(\u03a0)";

// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;

// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);

// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);

// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
}
}
周公 2009-04-22
  • 打赏
  • 举报
回复
public static byte[] Convert(Encoding srcEncoding,Encoding dstEncoding,byte[] bytes)

srcEncoding
类型:System.Text..::.Encoding

bytes 的编码格式。

dstEncoding
类型:System.Text..::.Encoding

目标编码格式。

bytes
类型:array<System..::.Byte>[]()[]

返回值
类型:array<System..::.Byte>[]()[]

Byte 类型的数组包含将 bytes 从 srcEncoding 转换为 dstEncoding 的结果。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧