0

我有以下问题:我必须使用 udp 将我的 android 手机中的字节列表发送到旧服务器。因为在java中字节是有符号的,服务器总是接收到很多字节。他有 72 个字节,而不是 36 个字节。因此,字节列表的解释不适用于服务器端。

服务器上的正确解释是:23 80 82 88 F2 F2 8C F7 B8 FC 9B 88 98 91 82 84 89 80 90 E5 AA 82 C0 83 AB BE 87 BC D3 C0 95 80 80 16 0D 0A 3542420551398236 2013-19-0 17:16:18 47,41413 9,3971 879m 273,2° 0km/h

我的错误结果是:32 33 38 30 38 32 38 38 46 32 46 32 38 43 46 37 42 38 46 43 39 42 38 38 39 38 39 31 38 32 38 31 38 30 38 30 39 30 45 325 3 4 30 38 33 41 42 42 42 38 37 41 43 44 33 43 30 39 35 38 30 38 30 37 44 30 44 30 41

有人知道我必须如何发送这个字节列表吗?它应该是 36 个无符号字节?

我的安卓应用程序代码是:

public class UdpClient extends IntentService {

public static final String REQUEST_STRING = "sharedText";

public UdpClient() {
    super("UdpClient");
}

@Override
public void onHandleIntent(Intent intent) {
    String requestString = intent.getStringExtra(REQUEST_STRING);
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/*".equals(type)) {
            runUdpClient(requestString); // Handle text being sent
        }
    }
}

@SuppressLint("DefaultLocale")
public static String getSum(String in) {
    int checksum = 0;
    if (in.startsWith("$")) {
        in = in.substring(1, in.length());
    }

    int end = in.indexOf('*');
    if (end == -1)
        end = in.length();
    for (int i = 0; i < end; i++) {
        checksum = checksum ^ in.charAt(i);
    }
    String hex = Integer.toHexString(checksum);
    if (hex.length() == 1)
        hex = "0" + hex;
    return hex.toUpperCase();
}

private static final int UDP_SERVER_PORT = 11111;

@SuppressLint("DefaultLocale")
private void runUdpClient(String string) {

    String sharedText = string.toUpperCase();

    String checksum = null;

    if (sharedText != null) {
        // Update UI to reflect text being shared
        checksum = getSum(sharedText);
    }

    String udpMsg = "23" + sharedText + checksum + "0D0A";

    DatagramSocket ds = null;

    try {
        ds = new DatagramSocket();
        InetAddress serverAddr = InetAddress.getByName("127.0.0.1");
        DatagramPacket dp;

        byte[] sendData = new byte[36];

        sendData = udpMsg.getBytes("ISO-8859-1");

        dp = new DatagramPacket(sendData, sendData.length, serverAddr,
                UDP_SERVER_PORT);
        ds.send(dp);
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ds != null) {
            ds.close();
        }
    }
}

}

4

1 回答 1

2

您需要使用以下方式将 udpMsg 十六进制字符串转换为字节数组:

import javax.xml.bind.DatatypeConverter;
sendData = DatatypeConverter.parseHexBinary(udpMsg);

编辑:试试这个方法,我猜Android没有XML类。

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
    data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                         + Character.digit(s.charAt(i+1), 16));
}
return data;
}
于 2013-02-20T01:05:08.913 回答