Question

I have the following problem: I have to send a list of bytes from my android phone to an old server with udp. Because in java the bytes are signed, the server receive always to many bytes. Instead of 36 bytes he has 72 bytes. So, the interpretation of the bytes list is not work on serverside.

A correct interpretation on the server is: 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 354242055139836 2013-19-02 17:16:18 47,41413 9,3971 879m 273,2° 0km/h

My wrong result is: 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 35 44 32 38 32 43 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

Has someone an idea how I have to send this byte list? It should be 36 unsigned bytes?

My android app code is:

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();
        }
    }
}

}

Was it helpful?

Solution

You need to convert your udpMsg hex string to a byte array using something like so:

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

edit: try this method instead, I guess Android doesn't have the XML classes.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top