求助:delphi7下 按UTF-8编码将字符串转换成byte数组
在发送消息时需将字符串指令按UTF-8编码将字符串转换成byte数组,在该byte数组前附加两个字节(big endian)描述后续字节数。
我和别的公司做TCP对接 我这边是客户端
他文档中有上面一句话
我的理解是 先将 string 转换成 UTF8String 在将 UTF8String 赋值给 byte数组
但是信息是错误的 是不是我理解错误了
我的代码如下
var LoginInfo: string; temp1: UTF8String; retuInfo, count: Integer; pass: MD5Digest; info: array of Byte; readarr: array[0..254] of Byte;begin//DlSocketProxy.connectServer; try pass := MD5String(serverPWD); temp1 := MD5Print(pass); //要发送的字符串 LoginInfo := '1,' + serverID + ',' + FormatDateTime('yyyymmddhhmmss', Now) + ',' + temp1; count := Length(LoginInfo); //转化成 UTF8String temp1 := UTF8Encode(LoginInfo); SetLength(info, count + 2); //转化成byte数组 Move(temp1[1], info[2], count); retuInfo := Length(info); //添加描述字符 第一个字节为0 第二个字节为 数据原始长度 info[0] := 0; info[1] := retuInfo - 2; except end;end;