We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Casting and byte-order question
Page Index Toggle Pages: 1
Casting and byte-order question (Read 378 times)
Casting and byte-order question
Jul 3rd, 2006, 5:05am
 
Hey,
I come from a C/C++ background, and I was wondering how I would go about typecasting without changing the binary data of a variable. For instance, I want to convert a float to an array of four chars.

If anyone is familiar, in C++ this would be:

char *ca;
float f = 1.032f;
ca = (char *)&f;
//now we can safely access indicies 0-3 of ca

Also, in terms of networking, does java use the native byte order of the system? If it does use the native byte order, does processing have access to something that converts data to network byte order?

Thanks!
Re: Casting and byte-order question
Reply #1 - Jul 4th, 2006, 2:13pm
 
There's no pointer arithmetic in Java, though you can do something like this using Java's wrapper classes for primitive types:

Code:
byte[] floatAsByteArray(float f) {
int raw=Float.floatToRawIntBits(f);
byte[] buf=new byte[4];
buf[0]=(byte)(raw>>>24);
buf[1]=(byte)(raw>>16 & 0xff);
buf[2]=(byte)(raw>>8 & 0xff);
buf[3]=(byte)(raw & 0xff);
return buf;
}


Remember, Java char's are always Unicode and so 16bit long. That's why the above code will return the float value as a byte buffer in Big Endian order. Using (Java) Bytes also means that some of the results may have negative numbers since Java Byte are always signed. To avoid that you could just return a char[] or int[] array. It's a bit more wasteful since you'll only be using the lower 8 bits for each index, but might be more convenient to use...

hth!
Re: Casting and byte-order question
Reply #2 - Jul 4th, 2006, 2:39pm
 
Thanks alot!

I was looking at ByteArrayOutputStream & ObjectOutputStream -- is this an easier or faster way to do this?
Re: Casting and byte-order question
Reply #3 - Jul 4th, 2006, 7:21pm
 
Am really not sure what you're trying to do, but couldn't see why using a stream of any form would be faster than the above code snippet...

Also, I just did a quick google for "java network byte order" and it came up with this link, which explains the Java approach and to that features almost the exact same code! Smiley

http://www.jguru.com/faq/view.jsp?EID=25977
Re: Casting and byte-order question
Reply #4 - Jul 5th, 2006, 7:55pm
 
Thanks again!

I just wanted to know how to assemble a networking packet.

As for the streams, I figure that java's interpreter keeps variables in the native byte order until someone wants to mess with them (which takes a bit of effort on its part). I was just thinking that the streams might have some optimizations because Java trusts them enough to mess directly with the memeory. However, Ill just use this method as recommended.
Page Index Toggle Pages: 1