代码如下:
//大概定义编码的
// The format of BSComm Binary data
//
// | length_json_string | string_data |0| length_bin1_data | bin1_data | length_bin2_data | bin2_data |
//----------------------------------------------------------------------------------------------------------
// | 4 byte | (len_json - 1)|1| 4 byte | len_bin1 | 4 byte | len_bin2 |
//
static public string GetStringFromBSCommBuffer(byte[] abytBSComm)
{
if (abytBSComm.Length < 4)
return "";
try
{
string sRet;
int lenText = BitConverter.ToInt32(abytBSComm, 0);//返回由字节数组中指定位置的四个字节转换来的 32 位有符号整数
if (lenText > abytBSComm.Length - 4)
return "";
if (lenText == 0)
return "";
if (abytBSComm[4 + lenText - 1] == 0) // if last value of string buufer is 0x0
sRet = System.Text.Encoding.UTF8.GetString(abytBSComm, 4, lenText - 1);
else
sRet = System.Text.Encoding.UTF8.GetString(abytBSComm, 4, lenText);
return sRet;
}
catch
{
return "";
}
}