我在以=分割字串后,判断长度,未二才解码,部分解决了问题
可是却去掉了没有解码的部分
//HexString字串为待解码的串
string[] aHex= HexString.Replace(" ","").Split(new char[1]{'='});
byte[] abyte = new Byte[aHex.Length];
for(int i=0;i<abyte.Length;i++)
{
if(abyte[i].ToString().Length==2)
{
abyte[i] =(byte) int.Parse(aHex[i],System.Globalization.NumberStyles.HexNumber);
}
else
{
abyte[i]=(byte)System.Text.Encoding.Default.GetBytes(aHex[i]);//加上这句,想彻底解决问题,可是却无法编译通过,问题是无法将byte[]转化为byte,怎么办
} }
return encode.GetString(abyte);
可是却去掉了没有解码的部分
//HexString字串为待解码的串
string[] aHex= HexString.Replace(" ","").Split(new char[1]{'='});
byte[] abyte = new Byte[aHex.Length];
for(int i=0;i<abyte.Length;i++)
{
if(abyte[i].ToString().Length==2)
{
abyte[i] =(byte) int.Parse(aHex[i],System.Globalization.NumberStyles.HexNumber);
}
else
{
abyte[i]=(byte)System.Text.Encoding.Default.GetBytes(aHex[i]);//加上这句,想彻底解决问题,可是却无法编译通过,问题是无法将byte[]转化为byte,怎么办
} }
return encode.GetString(abyte);
解决方案 »
- vs2005中 索引超出范围。必须为非负值并小于集合大小 怎么解决
- 未处理 NullReferenceException Object reference not set to an instance of an object.
- 大侠们快来帮忙啊~来的通通给分
- 简单问题
- 未能找到文件,是什么原因,怎么解决?急
- 请问Alert函数的原型是什么?如何使用这个函数弹出对话框?
- 关于C#线程安全
- 同一控件要创建多个委托和方法吗?
- 提供了数据表,请高手帮忙设计个.NET的曲线图(在线等一晚上,急)
- 请教:如何修改ICON?
- 怎么把图片放置在当前文件夹中
- 除了大家喜爱的csdn,还有什么比较好的讨论.net技术的论坛呢?
{
return new byte[0];
}
char ch;
char[] hz;
int chint1,chint2;
String strRet="";
hz=strSrc.ToCharArray();
for (int i=0;i<strSrc.Length;i++)
{
ch = hz[i];
if (ch == '=')
{
if (IsHex(hz[i+1],out chint1)&&IsHex(hz[i+2],out chint2))
{
byte cc;
cc = Convert.ToByte(chint1<<4);
cc |= Convert.ToByte(chint2);
//ch = Convert.ToChar(chint1 + chint2);
ch = Convert.ToChar(cc);
i += 2;
}
}
strRet += ch.ToString();
}
char[] x= strRet.ToCharArray();
byte[] b = new byte[x.Length];
for(int i=0;i<x.Length;i++)
{
b[i] = Convert.ToByte(x[i]);
}
return Encoding.GetEncoding("gb2312").GetString(b);
是判断是否是16进制吧
IsHex就是判断字符是不是十六进制的
private bool IsHex(char ch,out int ret)
{
if ((ch >= '0') && (ch<='9'))
{
ret = ch-'0';
return true;
}
if ((ch>='A') &&(ch <='F'))
{
ret = ch-'A'+10;
return true;
}
ret = 0;
return false;
}