使用adodb.stream进行编码转换部分字符有问题
我想要将一个UTF-8编码的文本转换成GBK格式,使用下面的两个函数进行转换
content = binToStr(strToBin(str,"UTF-8") ,"GBK")
Function strToBin(strInput, strCharset)
Set stream = CreateObject("ADODB.Stream")
stream.Type = 2 'adTypeText
stream.Charset = strCharset
stream.Open
stream.WriteText strInput
stream.Position = 0
stream.Type = 1 'adTypeBinary
strToBin = stream.Read()
stream.Close
End Function
Function binToStr(strInput, strCharset)
Set stream = CreateObject("ADODB.Stream")
stream.Type = 1
stream.Open
stream.Write strInput
stream.Position = 0
stream.Type = 2
stream.Charset = strCharset
binToStr = stream.ReadText
stream.Close
Set stream = Nothing
End Function
结果转换出来的字符中部分中文正常,部分中文后如果跟着.的话就会变成?号不识别
例如
检.rar --> 检.rar
查.rar --> ??rar
测.rar --> ??rar
请问这是什么原因呢
[解决办法]
http://blog.tjz3.com/2011/01/24/406.html
作为基础小知识,网上很多就直接复制了!
/// <summary>
/// 编码的代码
/// </summary>
/// <param name="value">需要转换的字符串</param>
/// <returns>返回转换后的字符串</returns>
public string EnCodeCovert(string value)
{
System.Text.Encoding srcEncode = System.Text.Encoding.GetEncoding("utf-8");
System.Text.Encoding convToEncode = System.Text.Encoding.GetEncoding("GBK");
byte[] bytes = srcEncode.GetBytes(value);
System.Text.Encoding.Convert(srcEncode, convToEncode, bytes, 0, bytes.Length);
return convToEncode.GetString(bytes);
}
[解决办法]