如题,不能用ToString()直接转化,这样得到的是等效的十六进制数,而不是想要的内容,请问各位高手如何实现?
#region 转换指定字节数组为字符串
/// <summary>
/// 转换指定字节数组为字符串
/// </summary>
/// <param name="ByteGet">字节数组Byte[]</param>
/// <param name="myEncoding">编码方式</param>
/// <returns></returns>
private static string getStringFromByteArray(Byte[] ByteGet,Encoding myEncoding)
{
int i,lngCount;
StringBuilder aTemp = new StringBuilder(10000);
lngCount = ByteGet.Length;
for(i = 0;i<lngCount;i+= 10000)
{
aTemp.Append(myEncoding.GetString(ByteGet,i,(lngCount>=i+10000?10000:lngCount - i)));
}
if(i<=lngCount)
{
aTemp.Append(myEncoding.GetString(ByteGet,i,(lngCount - i)));
}
return aTemp.ToString();
}
#endregion
你先试试Encoding,如果不行试试这个
public static byte[] StringToByte(string str)
{
byte[] bytes = new byte[str.Length / 2];
for (int i = 0; i < str.Length / 2; i++)
{
int btvalue = Convert.ToInt32(str.Substring(i * 2, 2), 16);
bytes[i] = (byte)btvalue;
}
return bytes;
}
public static string ByteToString(byte[] bytes)
{
StringBuilder strBuilder = new StringBuilder();
foreach (byte bt in bytes)
{
strBuilder.AppendFormat("{0:X2}", bt);
}
return strBuilder.ToString();
}
byte[] tmp;
string strTemp = Encoding.Default.GetString(tmp, 0, tmp.Length);
你试试使用GB2312编码试试
使用同一个Encoding进行string-〉byte[]-〉string即可