Reputation: 13
I am having difficulty just passing a short value from a C server. What I receive on the other end is 'junk' it looks like, and I can't seem to convert it into something useful.
Factors I think are influencing it
Starting with C code:
// Read 10 bit value into short
unsigned short ret = decideActionCall(buffer);
unsigned char lsb = (unsigned char) ret;
unsigned char msb = (unsigned char) (ret >> 8);
printf("msb,lsb = %02x,%02x\n",msb, lsb);
/**
char retString [4];
retString[0] = msb;
retString[1] = lsb;
retString[2] = '\n';
retString[3] = '\0';
*/
unsigned short retString1[1];
retString1[0] = ret;
printf("msb,lsb = %d,%d\n",retString[0],retString[1]);
printf("String to send: <%s>\n",retString1);
//if (write(newsockfd, retString, 3) < 0) {
// error("Error sending response to the server");
//}
if (write(newsockfd, &retString1, 2) < 0) {
error("Error sending response to the server");
}
if (write(newsockfd, "\r",1) < 0) {
error("Error sending response to the server");
}
And the terminal output is
START-----------------------------
0 0
136 0x88
ADC3 = 136
END-----------------------------
decideActionCall() ret value: 136
msb,lsb = 00,88
msb,lsb = 0,136
String to send: <�>
So the return for 'ret' is a value between 0 and 1023 (0x3FF). These 10 bits need to be transferred from C server to client. I have tried two implementations where I send them as 8 bit chars (retString) or one 16 bit short (retString1). Sizeof(char) == 1
, and sizeof(short) == 2
.
Java Code (with various attempts to parse it):
BufferedReader bufIn = new BufferedReader(new InputStreamReader(in));
String response = bufIn.readLine();
char [] hello = response.toCharArray();
for (char a : hello) {
short shortA = (short) (a & 0x3FF);
System.out.println(a + " = " + (int)a + " = " + shortA);
//System.out.println((int)a);
//System.out.println(Integer.valueOf(a+"",10));
}
byte test[] = response.getBytes("UTF-8");
StringBuilder sb = new StringBuilder();
for (byte b : test) {
sb.append(String.format("%02X ", b));
}
System.out.println(sb.toString());
byte b05 = 0x00;
byte b06 = 0x00; // MSB, positive as < 0x80
int i = 0;
for (byte a : test) {
//System.out.println(short1);
System.out.println("byte: " + a);
//System.out.println(Integer.valueOf(a+"",10));
if (i == 0 ) {
b05 = a;
} else {
b06 = a;
}
i++;
}
System.out.println("i = " + i);
byte[] byteTabDay = new byte[2];
byteTabDay[0] = b05;
byteTabDay[1] = b06;
BigInteger temp = new BigInteger(test);
System.out.println(temp);
System.out.println(temp.intValue());
System.out.println(temp.shortValue() + 65536);
BigInteger temp2 = new BigInteger(byteTabDay);
System.out.println(temp2);
System.out.println(temp2.shortValue() + 65536);
Java sample output (android logcat):
I/System.out﹕ response: ���
I/System.out﹕ here: � 65533
I/System.out﹕ ascii: 65485 65581
I/System.out﹕ � = 65533 = 1021
I/System.out﹕ �� = 0 = 0
I/System.out﹕ EF BF BD 00
I/System.out﹕ byte: -17
I/System.out﹕ byte: -65
I/System.out﹕ byte: -67
I/System.out﹕ byte: 0
I/System.out﹕ i = 4
I/System.out﹕ -272646912
I/System.out﹕ -272646912
I/System.out﹕ 48384
I/System.out﹕ -4352
I/System.out﹕ 61184
Questions:
Thank you,
Upvotes: 0
Views: 454
Reputation: 311052
Reader
at all. You should be using DataInputStream.readShort().
Upvotes: 2