Re: encoding a float
On Mar 28, 8:39 am, "andrew_n...@yahoo.com" <andrew_n...@yahoo.com>
wrote:
Hi all, I am trying to convert a float value to a octet stream
I have a similar problem. However, I am working within the same
machine, passing float/double from a JVM to native code. Currently, I
am assuming that for JNI implementations of the JVM, sizeof(jint) ==
sizeof(jfloat) and sizeof(jlong) == sizeof(jdouble) but this might not
be safe. I can only assume that a JVM can successfully encode a Java
float as a C++ jfloat and a Java double as a C++ jdouble.
JNI is a different situation, and you should check the JNI
documentation to be sure, but I believe that at the JNI
interface, you'll get native format, regardless of what the JVM
uses internally.
The first question is the one above: what is the desired format.
The above will only work if the internal format of a float
corresponds exactly to the desired format. And of course, it
also depends on int being the same size as float; I've definitly
worked on machines where it is smaller.
I tried memcpy to treat the vector as if
an array it didn't
work out well( although, It should work right?)
To memcpy, you'd have to first increase the size of the vector,
then pass the address of the target element. And of course,
memcpy is even less portable than the above; it can fail between
two different machines even if both use IEEE floating point (and
I've seen at least one case where it failed between two
different versions of the same compiler, on the same platform).
What if I reinterpret the pointer to jdouble and jfloat as an unsigned
char* and then encode as a hexstring. Won't this work as long as my
JVM that produces the doubles and floats is the same machine and
process that consumes them within the native code?
It depends on who's reading the stream, and why. I don't really
understand what you are trying to do; you can pass float and
double directly over the JNI interface, and they should be in
native format, ready for immediate use. If you're streaming
data (e.g. into a ByteArray), then you have to follow the same
rules as everyone else: decide on a format, and ensure that
everyone codes to it. (Converting to a hex string seems off
hand to combine the worst aspects of both binary and text. I
wouldn't do it unless it was required by some external format.)
--
James Kanze (GABI Software) mailto:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]