A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
OCA Java SE 8 Programmer I Study Guide
this week in the
Other JSE/JEE APIs
Wanted to pack values in java like struct.pack() in python
Muthu Kumaran Murugesan
Joined: Jul 08, 2010
Jul 09, 2010 00:20:34
I wanted to pack some values and later i have to unpack it ..
in python struct.pack() method is used to pack and unpack the values in the same way is there any way to pack values in
If any suggestions kindly advice me
Thanks & Regards
Joined: Oct 27, 2005
Jul 09, 2010 01:18:58
If I read it correctly, pack() turns the tuple into bytes and unpack() turns such bytes back into a tuple. Is this true?
If so, check out how serialization works. You can then write entire objects into any output stream using an
. To convert an object into bytes and back you can use the following:
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(object); oos.flush(); oos.close(); byte bytes = baos.toByteArray(); // converting back ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); MyObject object2 = (MyObject)ois.readObject();
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
I agree. Here's the link:
subject: Wanted to pack values in java like struct.pack() in python
Is there a way in netbeans to include external libraries in project JAR file(s)?
How to open .do file?
How to package your solution for upload
how to get this same output format in java
Python's struct.pack in Java
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015