修改simenshelp的string读写
整理apiclient
This commit is contained in:
1 parent
18dfe4f807
commit
6c7278ed45
2 files changed
+276
-444
No files matched your search
@@ -266,7 +266,18 @@ namespace WindowsFormsApp1.server
|
||||
break;
|
||||
|
||||
case "string":
|
||||
address[i].value = Encoding.ASCII.GetString(read.Content, index, address[i].addresstypelegth);
|
||||
// 读取字符串的最大长度和实际长度
|
||||
byte maxLength = read.Content[index];
|
||||
byte actualLength = read.Content[index + 1];
|
||||
|
||||
// 确保实际长度不超过最大长度和剩余可用空间
|
||||
actualLength = Math.Min(actualLength, (byte)Math.Min(maxLength, address[i].addresstypelegth - 2));
|
||||
|
||||
// 从第3个字节开始读取实际的字符串内容
|
||||
address[i].value = new string(
|
||||
Encoding.ASCII.GetString(read.Content, index + 2, actualLength)
|
||||
.Where(c => c > '\u0003' && !char.IsControl(c))
|
||||
.ToArray());
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -329,17 +340,23 @@ namespace WindowsFormsApp1.server
|
||||
break;
|
||||
|
||||
case "string":
|
||||
byte[] tochar = new byte[address[i].addresstypelegth];
|
||||
|
||||
var byte1 = Convert.ToByte((address[i].addresstypelegth - 2).ToString());
|
||||
var byte2 = Convert.ToByte( (((string)value).Length).ToString() );
|
||||
var byte3 = Encoding.ASCII.GetBytes((string)value);
|
||||
|
||||
tochar[0] = byte1;
|
||||
tochar[1] = byte2;
|
||||
|
||||
Array.Copy(byte3, 0, tochar, 2, byte3.Length);
|
||||
tochar.CopyTo(writebyte, index);
|
||||
string strValue = ((string)value ?? "").TrimEnd();
|
||||
// 计算实际字符串长度,不能超过最大长度(addresstypelegth-2)
|
||||
byte actualLength = (byte)Math.Min(strValue.Length, address[i].addresstypelegth - 2);
|
||||
// 创建包含头部信息的字节数组
|
||||
byte[] stringBytes = new byte[address[i].addresstypelegth];
|
||||
// 设置最大长度(第一个字节)
|
||||
stringBytes[0] = (byte)(address[i].addresstypelegth - 2);
|
||||
// 设置实际长度(第二个字节)
|
||||
stringBytes[1] = actualLength;
|
||||
// 转换字符串内容并复制到字节数组
|
||||
if (actualLength > 0)
|
||||
{
|
||||
byte[] contentBytes = Encoding.ASCII.GetBytes(strValue.Substring(0, actualLength));
|
||||
Array.Copy(contentBytes, 0, stringBytes, 2, actualLength);
|
||||
}
|
||||
// 复制到目标数组
|
||||
stringBytes.CopyTo(writebyte, index);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
Reference in new issue
Block a user