• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting java primitive types to and from binary format.

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call me a slimy salamander. I sure did miss it.

Thanks,

Landon
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic