向服务器发送Byte串 请高手帮忙
发送内容
Edit1->Text
Button->Click
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789abcdef".indexOf(c);
return b;
}
把Edit1->Text用这个转换一下,然后发送出去
请大虾们帮忙
上面那个转换是JAVA的,关于C++Builder小弟还不是很会用
[解决办法]
Byte toByte(char c)
{
if (c >= '0' && c <= '9')
return Byte(c-'0');
else if (c >= 'A' && c <= 'F')
return Byte(c-'A');
else
return Byte(c-'a');
}
Byte* hexStringToByte(AnsiString hex)
{
int len = (hex.length() / 2);
Byte *result = new Byte[len];
for (int i = 0; i < len; i++)
{
int pos = i * 2 + 1;
result[i] = (Byte)(toByte(hex[pos]) << 4
[解决办法]
toByte(hex[pos + 1]));
}
return result;
}