当前位置:   article > 正文

C# 字符【包含汉字】常用转码_c#中文转码

c#中文转码

GBK 十六进制转字符(包含汉字)

string code = "D6 B1 B2 E5 CA BD 32 30 31 39 31 32 32 37 30 30 30 32 31 0D";
string[] codeArr = code.Split(' ');
int codeLen = codeArr.Length;
byte[] codeByteArr = new byte[codeArr.Length];
if (codeLen > 0)
{
    for (int i = 0; i < codeLen; i++)
    {
        codeByteArr[i] = (byte)Convert.ToByte(codeArr[i], 16);
    }
}
             
string res = Encoding.GetEncoding("GBK").GetString(codeByteArr);
//结果:直插式2019122700021\r  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

字符(汉字)转 十六进制GBK

string chinese = "汉字哦110"; 
byte[] gbk = Encoding.GetEncoding("GBK").GetBytes(chinese);
string gbkStr = ""; string byteStr = "";
foreach (byte b in gbk)
{
    //s1 += Convert.ToString(b, 16)+" ";
    gbkStr += string.Format("{0:X2}", b) + " ";
    byteStr += b + " "; 
} 
//结果: 
gbkStr :BA BA D7 D6 C5 B6 31 31 30 
byteStr :186 186 215 214 197 182 49 49 48 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Unicode十六进制转字符(包含汉字)

string code = "D6 B1 B2 E5 CA BD 32 30 31 39 31 32 32 37 30 30 30 32 31 0D";
string[] codeArr = code.Split(' ');
int codeLen = codeArr.Length;
byte[] codeByteArr = new byte[codeArr.Length];
if (codeLen > 0)
{
    for (int i = 0; i < codeLen; i++)
    {
        codeByteArr[i] = (byte)Convert.ToByte(codeArr[i], 16);
    }
}
             
string res = Encoding.GetEncoding("Unicode").GetString(codeByteArr);
//结果:直插式2019122700021\r  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

字符(汉字)转 十六进制UniCode

string chinese = "汉字哦110";
byte[] uniCode = Encoding.Unicode.GetBytes(chinese);
string uniStr = ""; string uniByte = "";
foreach (byte b in uniCode)
{
    //s2 += Convert.ToString(b, 16) + " ";
    uniStr += string.Format("{0:X2}", b) + " ";
    uniByte += b + " "; 
} 
//结果: 
49 6C 57 5B E6 54 31 00 31 00 30 00 
73 108 87 91 230 84 49 0 49 0 48 0 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

UTF-8十六进制转字符(包含汉字)

string code = "D6 B1 B2 E5 CA BD 32 30 31 39 31 32 32 37 30 30 30 32 31 0D";
string[] codeArr = code.Split(' ');
int codeLen = codeArr.Length;
byte[] codeByteArr = new byte[codeArr.Length];
if (codeLen > 0)
{
    for (int i = 0; i < codeLen; i++)
    {
        codeByteArr[i] = (byte)Convert.ToByte(codeArr[i], 16);
    }
}
             
string res = Encoding.GetEncoding("UTF-8").GetString(codeByteArr);
//结果:直插式2019122700021\r  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

字符(汉字)转 十六进制UTF-8

string chinese = "汉字哦110";
byte[] uniCode = Encoding.UTF8.GetBytes(chinese);
string uniStr = ""; string uniByte = "";
foreach (byte b in uniCode)
{
    //s2 += Convert.ToString(b, 16) + " ";
    uniStr += string.Format("{0:X2}", b) + " ";
    uniByte += b + " "; 
} 
//结果: 
49 6C 57 5B E6 54 31 00 31 00 30 00 
73 108 87 91 230 84 49 0 49 0 48 0 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/253432
推荐阅读
相关标签
  

闽ICP备14008679号