Jim said:
I'm pretty sure it's UTF-8 encoding. At least, when I write an integer to
the PrintWriter I can recover it from the C program, after using atoi().
_That_ works fine. It's trying to write/read/print a float that my trouble
comes. So I'm thinking the character encoding is ok at least?
I don't think you understand the basic problem.
If you use java.io.PrintWriter.print() to print a float, you literally
get a textual representation of that numeric value on the output stream.
You do not get the binary representation of that numeric value, IEEE754
or otherwise. You get a series of characters, and not only that, it is
up to the *runtime system* whether that series of characters consists of
the nice one character-per-8-bit-byte that you are expecting.
But your problem is even worse. You are now passing these bytes, which
are the character representations of your number: "3.14" to printf()'s
%f formatter.
So for the example of "3.14" you'd be getting a float that's like:
33 2E 31 34
Which, if evaluated as an IEEE754 single precision 32-bit value,
works out to be 4.0557282e-8
Note that if you feed that to printf("%f", ...) you can generally expect
to get 0.0000000
In fact, anything that begins with an ASCII digit is going to fall well
outside the range of %f's default precision, since even "9999" leads to
an exponent of -13 so you're *always* going to see zeros...
If you insist on taking this approach for data interchange, you will
need to use an output mechanism that actually puts binary
representations of the data in the output. But I would actually try to
convince you that some kind of markup format for the data would be
preferable. Although if you cannot change the sender of the data,
then sscanf(), atoi(), strtod(), and friends may help you.
I notice you're getting a 127 byte buffer. Do you have some kind of
delimiter that tells you where to stop parsing?
If this were a java forum I would have some specific advice about object
serialization.