如何实现UTF8解码与编码的问题??

时间:2023-01-05 09:37:59
来自己文本文件(ANSI)中的一行:N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=AE=B6=E4=BA=BA 

下面简要说明QUOTED-PRINTABLE编码方式,更为详细的资料请参考相关文档: 
ASCII可显示字符基本保持不变。Unicode字符或者UTF8编码字符使用等号加其对应16进制代码表示。例如上述CHARSET为UTF8的字符=E5=AE=B6=E4=BA=BA对应的UTF8编码0xE5,0xAE,0xB6代表中文“家”,其他的代表“人”。另外如果其中有可显示ASCII码,保持原样输出。

例如ENCODING=QUOTED-PRINTABLE:Home=E5=AE=B6People=E4=BA=BA解码后为“Home家People人”。 
Q:如何实现上述编码的转换(包括解码与编码的方法)?

谢谢~~

41 个解决方案

#1


encoding

#2


Encoding.Convert

#3


将字节数组内某个范围的字节从一种编码转换为另一种编码。

public static byte[] Convert(
   Encoding srcEncoding,
   Encoding dstEncoding,
   byte[] bytes,
   int index,
   int count
);

#4


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);
      }
   }
}

#5


这样行不行
解码
将=E5=AE=B6=E4=BA=BA中的E5 AE 等提取出来后用byte=Convert.ToByte(string,16)转换为byte
然后char[]=Encoding.UTF8.GetChars(byte[])所得char[]就是
编码
byte[]=Encoding.UTF8.GetBytes(string)
char=Convert.ToChar(byte)
string

#6


string a = "Home家People人", c = "";
string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);
int j = 0;
for (int i = 0; i < b.Length; i++)
{        
if (b[i] == '%')
{
  c += "=";
  j = i;
}
if (i - j < 3 && i - j > 0 && j !=0)
{
  c += b[i].ToString().ToUpper();
}
else
{
  if( b[i] !='%')
  c += b[i];
}

}

Response.Write(c);

int jj = 0;
string d = "";
d = c.Replace("=", "%");
Response.Write("<hr>");     
d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
Response.Write(d);

#7


参考
http://www.programfan.com/article/showarticle.asp?id=2329

#8


一时找不到怎么转成16进制的字符串了。。。
string s="";
foreach(byte b in bytes)
s+=("="+string.Format("{0:X}",b));

#9


关注

#10


发现直接替换=为%确实简单。。。

#11


using System;
class EncodeDecode
{
    static void Main(string[] args)
    {       
string a = "Home家People人", c = "";
string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);
int j = 0;
for (int i = 0; i < b.Length; i++)
{        
if (b[i] == '%')
{
  c += "=";
  j = i;
}
if (i - j < 3 && i - j > 0 && j !=0)
{
  c += b[i].ToString().ToUpper();
}
else
{
  if( b[i] !='%')
  c += b[i];
}

}

Console.WriteLine(c);

string d = "";
d = c.Replace("=", "%");
   
d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
Console.WriteLine(d);

    }
}

#12


Converting a string to Quoted Printable

http://nopaste.dotnet-expert.de/View.aspx?id=110

#13


关注钻石

#14


mark

#15


~~mark

#16


"=3F=CA=C7=B0=A2=C9=B3=3F=CE=E4=D2=BA=3F=A4=B5=A3=EA=A3=E8=A4=C5=A4=B5=A4=B8=A3==F3=A4=B8=A4=A6=A4=C9=A4=A7=B9G=A5=B9=A5=A7=81=84sw"
用钻石大哥的写法,结果是:"?&#496;&#627;?&#1210;?&#356;%&#937508;&#612;Gsw"
而我输入的是:"你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw"
不正确啊...

#17


我试了下,你的那个字符串用Encoding.GetEncoding("GB2312")的话可以翻出来一部份但无法全部正确转换,我用下面的可以,但是转换的结果跟你的不一样。。。
// byte[] bytes=Encoding.UTF8.GetBytes("你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw");
// string s="";
// foreach(byte b in bytes)
// s+=("="+string.Format("{0:X}",b));
// Console.WriteLine(s);
string ns="=E4=BD=A0=E6=98=AF=E9=98=BF=E6=B2=99=E5=8D=A1=E6=AD=A6=E6=B6=B2=E5=8E=8B=E3=81=95=EF=BD=8A=EF=BD=88=E3=81=A5=E3=81=95=E3=81=98=EF=BD=93=E3=81=98=E3=81=86=E3=81=A9=E3=81=87=E7=AC=B9=E3=82=B9=E3=82=A7=E4=BA=9C=73=77"; //就是上面的结果
Console.WriteLine(HttpUtility.UrlDecode(ns.Replace("=","%"),Encoding.UTF8));

#18


刚才是我用GB2312了,改成了UTF8,还是有几个不一样
结果是:"?是阿沙?武液?さj%づさじsじうどぇ%スェ亜sw"

#19


我要得结果是::"你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw"

#20


using System;
class EncodeDecode
{
    static void Main(string[] args)
    {       
string a = "你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw", c = "";
string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);
int j = 0;
for (int i = 0; i < b.Length; i++)
{        
if (b[i] == '%')
{
  c += "=";
  j = i;
}
if (i - j < 3 && i - j > 0 && j !=0)
{
  c += b[i].ToString().ToUpper();
}
else
{
  if( b[i] !='%')
  c += b[i];
}

}

Console.WriteLine(c);

string d = "";
d = c.Replace("=", "%");
   
d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
Console.WriteLine(d);

    }
}

#21


里面有些字是日文的

#22


C的
http://dev.csdn.net/develop/article/19/19205.shtm

#23


using System;
class EncodeDecode
{
    static void Main(string[] args)
    {       
string a = "你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw", c = "";
string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);

int j = -1;
for (int i = 0; i < b.Length; i++)
{        
if (b[i] == '%')
{
  c += "=";
  j = i;
}
if (i - j < 3 && i - j > 0 && j !=-1)
{
  c += b[i].ToString().ToUpper();
}
else
{
  if( b[i] !='%')
  c += b[i];
}

}

Console.WriteLine("压缩结果");
Console.WriteLine(c);

string d = "";
d = c.Replace("=", "%");
   
d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
Console.WriteLine("解压结果");
Console.WriteLine(d);

    }
}

#24


如果在页面里使用,要注意页面的编码!!!!

protected void Page_Load(object sender, EventArgs e)
{
  Response.ContentEncoding = System.Text.Encoding.UTF8;
  string a = "你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw", c = "";
  string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);

  int j = -1;
  for (int i = 0; i < b.Length; i++)
  {
    if (b[i] == '%')
    {
      c += "=";
      j = i;
    }
    if (i - j < 3 && i - j > 0 && j != -1)
    {
      c += b[i].ToString().ToUpper();
    }
    else
    {
      if (b[i] != '%')
        c += b[i];
    }

  }

  Response.Write("压缩结果:");
  Response.Write(c);

  string d = "";
  d = c.Replace("=", "%");

  d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
  Response.Write("<hr>解压结果:");
  Response.Write(d);

}

#25


我的问题是这样的:我用jmail发邮件的时候会把正文里的内容编码,我现在就是在用jmail收邮件的时候怎么解码,结果总是不太一样

#26


原来孟子在...

#27


编码解码都是你自己做的么?
jmail本身已经处理这些了,你怎么还自己写?

#28


我用jmail发邮件发出的正文就是QP乱码啊

#29


等待孟子大哥...

#30


难道会是我用日文系统的原因?

#31


你的编码和解码格式要一致,
System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);

#32


晕,刚才改了发信的,忘了改收信的了,不过还是不对啊
结果:"你是阿沙卡武液压%jhづさじsじう%ぇ笹スェ亜sw"
应该为:你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw

#33


"=E4=BD=A0=E6=98=AF=E9=98=BF=E6=B2=99=E5=8D=A1=E6=AD=A6=E6=B6=B2=E5=8E=8B=E3=81==95=EF=BD=8A=EF=BD=88=E3=81=A5=E3=81=95=E3=81=98=EF=BD=93=E3=81=98=E3=81=86=E3==81=A9=E3=81=87=E7=AC=B9=E3=82=B9=E3=82=A7=E4=BA=9Csw"
这是收到的QP码
这时用你的方法得到"你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw"的QP:
"=e4=BD=A0=E6=98=AF=E9=98=BF=E6=B2=99=E5=8D=A1=E6=AD=A6=E6=B6=B2=E5=8E=8B=E3=81=95=EF=BD=8A=EF=BD=88=E3=81=A5=E3=81=95=E3=81=98=EF=BD=93=E3=81=98=E3=81=86=E3=81=A9=E3=81=87=E7=AC=B9=E3=82=B9=E3=82=A7=E4=BA=9Csw"

#34


第二行81后面的=号不同

#35


好像两个==是换行了

#36


不可以先进行替换
str = str.Replace("==","=")

#37


你可以先进行替换
str = str.Replace("==","=")

#38


恩,解决了啊...
我要拜孟子大哥为师啊,谁都不要拦我

#39


太崇拜你了,收我为徒吧

#40


mark

#41


谢谢 net_lover(【孟子E章】)

#1


encoding

#2


Encoding.Convert

#3


将字节数组内某个范围的字节从一种编码转换为另一种编码。

public static byte[] Convert(
   Encoding srcEncoding,
   Encoding dstEncoding,
   byte[] bytes,
   int index,
   int count
);

#4


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);
      }
   }
}

#5


这样行不行
解码
将=E5=AE=B6=E4=BA=BA中的E5 AE 等提取出来后用byte=Convert.ToByte(string,16)转换为byte
然后char[]=Encoding.UTF8.GetChars(byte[])所得char[]就是
编码
byte[]=Encoding.UTF8.GetBytes(string)
char=Convert.ToChar(byte)
string

#6


string a = "Home家People人", c = "";
string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);
int j = 0;
for (int i = 0; i < b.Length; i++)
{        
if (b[i] == '%')
{
  c += "=";
  j = i;
}
if (i - j < 3 && i - j > 0 && j !=0)
{
  c += b[i].ToString().ToUpper();
}
else
{
  if( b[i] !='%')
  c += b[i];
}

}

Response.Write(c);

int jj = 0;
string d = "";
d = c.Replace("=", "%");
Response.Write("<hr>");     
d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
Response.Write(d);

#7


参考
http://www.programfan.com/article/showarticle.asp?id=2329

#8


一时找不到怎么转成16进制的字符串了。。。
string s="";
foreach(byte b in bytes)
s+=("="+string.Format("{0:X}",b));

#9


关注

#10


发现直接替换=为%确实简单。。。

#11


using System;
class EncodeDecode
{
    static void Main(string[] args)
    {       
string a = "Home家People人", c = "";
string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);
int j = 0;
for (int i = 0; i < b.Length; i++)
{        
if (b[i] == '%')
{
  c += "=";
  j = i;
}
if (i - j < 3 && i - j > 0 && j !=0)
{
  c += b[i].ToString().ToUpper();
}
else
{
  if( b[i] !='%')
  c += b[i];
}

}

Console.WriteLine(c);

string d = "";
d = c.Replace("=", "%");
   
d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
Console.WriteLine(d);

    }
}

#12


Converting a string to Quoted Printable

http://nopaste.dotnet-expert.de/View.aspx?id=110

#13


关注钻石

#14


mark

#15


~~mark

#16


"=3F=CA=C7=B0=A2=C9=B3=3F=CE=E4=D2=BA=3F=A4=B5=A3=EA=A3=E8=A4=C5=A4=B5=A4=B8=A3==F3=A4=B8=A4=A6=A4=C9=A4=A7=B9G=A5=B9=A5=A7=81=84sw"
用钻石大哥的写法,结果是:"?&#496;&#627;?&#1210;?&#356;%&#937508;&#612;Gsw"
而我输入的是:"你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw"
不正确啊...

#17


我试了下,你的那个字符串用Encoding.GetEncoding("GB2312")的话可以翻出来一部份但无法全部正确转换,我用下面的可以,但是转换的结果跟你的不一样。。。
// byte[] bytes=Encoding.UTF8.GetBytes("你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw");
// string s="";
// foreach(byte b in bytes)
// s+=("="+string.Format("{0:X}",b));
// Console.WriteLine(s);
string ns="=E4=BD=A0=E6=98=AF=E9=98=BF=E6=B2=99=E5=8D=A1=E6=AD=A6=E6=B6=B2=E5=8E=8B=E3=81=95=EF=BD=8A=EF=BD=88=E3=81=A5=E3=81=95=E3=81=98=EF=BD=93=E3=81=98=E3=81=86=E3=81=A9=E3=81=87=E7=AC=B9=E3=82=B9=E3=82=A7=E4=BA=9C=73=77"; //就是上面的结果
Console.WriteLine(HttpUtility.UrlDecode(ns.Replace("=","%"),Encoding.UTF8));

#18


刚才是我用GB2312了,改成了UTF8,还是有几个不一样
结果是:"?是阿沙?武液?さj%づさじsじうどぇ%スェ亜sw"

#19


我要得结果是::"你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw"

#20


using System;
class EncodeDecode
{
    static void Main(string[] args)
    {       
string a = "你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw", c = "";
string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);
int j = 0;
for (int i = 0; i < b.Length; i++)
{        
if (b[i] == '%')
{
  c += "=";
  j = i;
}
if (i - j < 3 && i - j > 0 && j !=0)
{
  c += b[i].ToString().ToUpper();
}
else
{
  if( b[i] !='%')
  c += b[i];
}

}

Console.WriteLine(c);

string d = "";
d = c.Replace("=", "%");
   
d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
Console.WriteLine(d);

    }
}

#21


里面有些字是日文的

#22


C的
http://dev.csdn.net/develop/article/19/19205.shtm

#23


using System;
class EncodeDecode
{
    static void Main(string[] args)
    {       
string a = "你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw", c = "";
string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);

int j = -1;
for (int i = 0; i < b.Length; i++)
{        
if (b[i] == '%')
{
  c += "=";
  j = i;
}
if (i - j < 3 && i - j > 0 && j !=-1)
{
  c += b[i].ToString().ToUpper();
}
else
{
  if( b[i] !='%')
  c += b[i];
}

}

Console.WriteLine("压缩结果");
Console.WriteLine(c);

string d = "";
d = c.Replace("=", "%");
   
d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
Console.WriteLine("解压结果");
Console.WriteLine(d);

    }
}

#24


如果在页面里使用,要注意页面的编码!!!!

protected void Page_Load(object sender, EventArgs e)
{
  Response.ContentEncoding = System.Text.Encoding.UTF8;
  string a = "你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw", c = "";
  string b = System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);

  int j = -1;
  for (int i = 0; i < b.Length; i++)
  {
    if (b[i] == '%')
    {
      c += "=";
      j = i;
    }
    if (i - j < 3 && i - j > 0 && j != -1)
    {
      c += b[i].ToString().ToUpper();
    }
    else
    {
      if (b[i] != '%')
        c += b[i];
    }

  }

  Response.Write("压缩结果:");
  Response.Write(c);

  string d = "";
  d = c.Replace("=", "%");

  d = System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
  Response.Write("<hr>解压结果:");
  Response.Write(d);

}

#25


我的问题是这样的:我用jmail发邮件的时候会把正文里的内容编码,我现在就是在用jmail收邮件的时候怎么解码,结果总是不太一样

#26


原来孟子在...

#27


编码解码都是你自己做的么?
jmail本身已经处理这些了,你怎么还自己写?

#28


我用jmail发邮件发出的正文就是QP乱码啊

#29


等待孟子大哥...

#30


难道会是我用日文系统的原因?

#31


你的编码和解码格式要一致,
System.Web.HttpUtility.UrlDecode(d, System.Text.Encoding.UTF8);
System.Web.HttpUtility.UrlEncode(a, System.Text.Encoding.UTF8);

#32


晕,刚才改了发信的,忘了改收信的了,不过还是不对啊
结果:"你是阿沙卡武液压%jhづさじsじう%ぇ笹スェ亜sw"
应该为:你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw

#33


"=E4=BD=A0=E6=98=AF=E9=98=BF=E6=B2=99=E5=8D=A1=E6=AD=A6=E6=B6=B2=E5=8E=8B=E3=81==95=EF=BD=8A=EF=BD=88=E3=81=A5=E3=81=95=E3=81=98=EF=BD=93=E3=81=98=E3=81=86=E3==81=A9=E3=81=87=E7=AC=B9=E3=82=B9=E3=82=A7=E4=BA=9Csw"
这是收到的QP码
这时用你的方法得到"你是阿沙卡武液压さjhづさじsじうどぇ笹スェ亜sw"的QP:
"=e4=BD=A0=E6=98=AF=E9=98=BF=E6=B2=99=E5=8D=A1=E6=AD=A6=E6=B6=B2=E5=8E=8B=E3=81=95=EF=BD=8A=EF=BD=88=E3=81=A5=E3=81=95=E3=81=98=EF=BD=93=E3=81=98=E3=81=86=E3=81=A9=E3=81=87=E7=AC=B9=E3=82=B9=E3=82=A7=E4=BA=9Csw"

#34


第二行81后面的=号不同

#35


好像两个==是换行了

#36


不可以先进行替换
str = str.Replace("==","=")

#37


你可以先进行替换
str = str.Replace("==","=")

#38


恩,解决了啊...
我要拜孟子大哥为师啊,谁都不要拦我

#39


太崇拜你了,收我为徒吧

#40


mark

#41


谢谢 net_lover(【孟子E章】)