-2

I want to know if there is any Java API available to convert a POJO object to a hex string and vice versa.

Hooli
  • 1,135
  • 3
  • 19
  • 46
  • `Serializable` and `ObjectOutputStream` – xiaofeng.li Aug 25 '16 at 00:48
  • This is possible but your question does not provide enough details to know what you mean by _"convert a POJO object to a hex string"_. This could be interpreted several different ways, and without specifying WHY you want to do this and what you intend to to with the hex string it is impossible to give a meaningful answer. – Jim Garrison Aug 25 '16 at 01:47

2 Answers2

1

There is no simple built in way of doing this, but the general approach to take is to first serialize your object to a byte array, then convert the byte array to hex.

Community
  • 1
  • 1
Magnus
  • 7,952
  • 2
  • 26
  • 52
-1

You should use serialization and deserialization.

like this

ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream ous = new ObjectOutputStream(os);
ous.writeObject(new Message());
ous.flush();
ous.close();
byte[] data = os.toByteArray();
os.close();

and

ByteArrayInputStream is = new ByteArrayInputStream(data);
ObjectInputStream ins = new ObjectInputStream(is);
Message object= (Message) ins.readObject();
ins.close();
is.close();