c语言获取汉字gb2312编码,C# 汉字转GB2312 编码

///

/// 获取汉字的GB2312编码

///

///

///

public static string GetGB2312EncodingString(string str)

{

StringBuilder sb = new StringBuilder();

try

{

String unicodeString = str;

// Encode the string.

Byte[] encodedBytes = new byte[unicodeString.Length * 2];

int iCount = Encoding.GetEncoding("gb2312").GetBytes(unicodeString, 0, unicodeString.Length, encodedBytes, 0);

foreach (Byte b in encodedBytes)

{

string s = Convert.ToString(b, 16);

if (s == "0") continue;

sb.Append(string.Format("%{0}", s));  //   sb.Append(string.Format("0x{0}", s));

}

}

catch (Exception ex)

{

throw new Exception(ex.Message);

}

return sb.ToString();

}

你可能感兴趣的:(c语言获取汉字gb2312编码)