The moose likes Java in General and the fly likes Converting java primitive types to and from binary format. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Converting java primitive types to and from binary format." Watch "Converting java primitive types to and from binary format." New topic
Author

Converting java primitive types to and from binary format.

Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 43
Is there a class in the standard library, or a class in an open source library that allows the client code to easy convert a primitive type like boolean, long, double, or integer to binary form and back again? I don't want to use the DataInputStream and DataOutputStream for this purpose, since I'm not writing to files and this seems like overkill.

I can write my own class to do this work, but why reinvent the wheel?

Thanks for the help.

Landon
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

The wrapper classes have some methods to help you. The precise code you'd use depends on exactly what you want to do.

For instance, Float has the "floatToIntBits()" and "intBitsToFloat()" to get the exact bit representation of a float in and out of an Integer. An int created this way could be read in as a float by a C program (minding byte ordering, of course.) Double has a similar method.

Integer has toBinaryString() to produce "101010101..." from your favorite int.


[Jess in Action][AskingGoodQuestions]
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 43
Thanks for the response. I acutally looked at the wrapper class for Long, but it doesn't appear to have a "toBits" method like Integer and Double. However, it does have a toBinaryString method. However, it doesn't look like Long has a method that creates a Long from a binary string representation. The decode method only works with decimal, hex, and octal. If I'm not missing something I'll have to whip up a utility class that contains the methods to move Long objects back and forth from a binary format.

Thanks for the help.

Landon
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
However, it doesn't look like Long has a method that creates a Long from a binary string representation. The decode method only works with decimal, hex, and octal. If I'm not missing something I'll have to whip up a utility class that contains the methods to move Long objects back and forth from a binary format.


You're missing something. See Long.parseLong(String, int)
[ December 04, 2008: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 43
Call me a slimy salamander. I sure did miss it.

Thanks,

Landon
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
Originally posted by Landon Blake:
Call me a slimy salamander. I sure did miss it.

Thanks,

Landon


You're a slimy salamander.

Everybody does that sort of thing from time to time . . . think nothing of it
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Converting java primitive types to and from binary format.
 
Similar Threads
How to produce multi-sheet excel output with XSLT
Looking for servlet API source code
SOAP Message
Bit Operator
how to apply arthmetic operation on String containing only large number of digits