AffineTransform to String to AffineTransform

Well the title says it all really…
Ive converted an AffineTransoform to a String, and if i print that String it looks like this:

“AffineTransform[[1.0, 0.0, 25.0], [0.0, 1.0, 10.0]]”

So now i need to convert that String back into an AffineTransform. Can somebody please help me?
Thanks.

wtf? why would anyone want to do that?? o_O

anyways, break up the string using indexof and then use stringtokenizer and integer.valueof to get the values back…

I agree on both of Valodim’s points

well im transfering an AffineTransform over a network (UDP) so it goes into a byte array before its sent. I need to convert it back from the byte array, into an AffineTransform at the other end.
Is there a better way to do this?

[quote]well im transfering an AffineTransform over a network (UDP) so it goes into a byte array before its sent. I need to convert it back from the byte array, into an AffineTransform at the other end.
Is there a better way to do this?
[/quote]
Take a look at java.io.ObjectInputStream and java.io.ObjectOutputStream, with them you can write/read an object to/from a stream.
You may want to attach it to a ByteArrayOutputStream to write it to a byte array. (and read with ByteArrayInputStream)

The problem is i have 6 different values to send. They are all primative data type accept the 2 AffineTransforms so thought it would be quicker to send them in a String, then send an object.
Ill try send the values that the AffineTransforms are made up of, and construct them from the values at the other end.
If anyone gets a better idea, let me know. Thanks.

[quote]The problem is i have 6 different values to send. They are all primative data type accept the 2 AffineTransforms so thought it would be quicker to send them in a String, then send an object.
Ill try send the values that the AffineTransforms are made up of, and construct them from the values at the other end.
If anyone gets a better idea, let me know. Thanks.
[/quote]
Fortunately the ObjectOutputStream can write primitive data types too. (with writeByte,writeFloat etc…)

Example:


//Writing
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(baos);
oos.writeFloat(0.4f);
oos.writeObject("Hello");
byte[] b=baos.toByteArray(); // Here you have the byte array with the data, send it anywhere

//...

//Reading from a byte array "b"
ObjectInputStream ois=new ObjectInputStream(new ByteArrayInputStream(b));
float f=ois.readFloat();
String s=(String)ois.readObject();

Although if somebody has a better solution, I would like to see.

[quote]public class AffineTransform
extends Object
implements Cloneable, Serializable
[/quote]
how about serializing and sending the affinetransform object, then sending the other primitive types? ::slight_smile:

thanks for all your suggestions btw. Ive sent all the data over like u seggested Daniel_F. I have a question though.
If i do soemthing like this…

writeObject()
writeObject()
writeInt()
writeInt()

then at the other end i do this…

readObject()
readObject()
readInt()
readInt()

will it keep them in order? I mean, will the 1st readObject() read in what was written using the first writeObject()…etc?

I think yes, otherwise it would be pretty useless. :wink:

most probable, but not 100% guaranteed with udp :slight_smile:

[quote]most probable, but not 100% guaranteed with udp :slight_smile:
[/quote]
?

Udp don’t have guaranteed delivery but the content sent is guaranteed to be the same as recieved.

[quote]Udp don’t have guaranteed delivery but the content sent is guaranteed to be the same as recieved.
[/quote]
umm… how can the content sent be guaranteed to be the same as received if some packages may just get lost on the way?? o_O

If a datagram/packet is recieved, then you can be almost assured that it will be identical to what was sent.

Its upto you to determine if there are any missing packets.

How did this get onto the ins and outs UDP?
The question was about transfering Objects across a network - for which you would almost certainly want to use TCP anyway =/

that’s what I was trying to point out - use tcp for this kind of transfer :stuck_out_tongue: