/// <summary> /// byte数组转化成string类型 /// </summary> /// <param name="bytes">要转化的数组</param> /// <param name="startIndex"></param> /// <param name="length"></param> /// <param name="sweepInvalidData">是否清除第一个\0后面的数据</param> /// <returns>转化成的字符串</returns> public static string ByteArrayToString(Byte[] bytes, int startIndex, int length, bool sweepInvalidData = true) { if (sweepInvalidData) { UtilityDataProcess.SweepInvalidateByte(ref bytes, startIndex, length); } string outPutString = UtilityEncoding.Gb2312.GetString(bytes, startIndex, length); return(outPutString.TrimEnd('\0')); }
/// <summary> ///从byte数组指定索引处读取指定长度的byte数据,把其转换成字符串,并且把其中第一个'\0'后的数据替换成空格 /// </summary> /// <param name="bytes">byte数组</param> /// <param name="curBytesIndex">指定索引</param> /// <param name="dataLegth">指定长度</param> /// <returns>转换成的字符串</returns> public static string BytesToString(byte[] bytes, ref int curBytesIndex, int dataLegth) { UtilityDataProcess.SweepInvalidateByte(ref bytes, curBytesIndex, dataLegth); string curText = UtilityEncoding.Gb2312.GetString(bytes, curBytesIndex, dataLegth); //替换\0为空格 curText = curText.Replace('\0', ' '); curBytesIndex += dataLegth; return(curText); }