how to send a bigdecimal by DataOutputStream

Hi Folks!

Can anybody tell me how i can send a bigDecimal to my client through DataOutputStream object. There are methods in DataOutputStream class for sending long,int,string,double,short and so on BUT there is nothing for bigDecimal.

Here is my code.

Socket cs;

DataInputStream br;

DataOutputStream dos;

publicvoid run(){

try{

BigDecimal bd =new BigDecimal ( 12342342.234234 ) ;

bd = bd.setScale ( 1, BigDecimal.ROUND_DOWN ) ;

System.out.println ( bd ) ;

//here how i cant send my BigDecimal those dos object?

dos.

}catch(Exception e){}

}

Thanks in Advance

[1019 byte] By [asyed01a] at [2007-11-27 8:34:35]
# 1
Convert the BigDecimal to a byte array - check the methods of BigDecimal.
sabre150a at 2007-7-12 20:30:53 > top of Java-index,Java Essentials,New To Java...
# 2

you are correct there is a method bytevalue() in bigDecimal class. which conver my bigDecimal value into bytevalue. WORKS FINE. but on the client side when i receive "BYTEVALUE()" how can i bring it back to the original. Because ofcurse i am gettin my coverted byte value not the original value of bigDecimal..

Here's how i convert Bigdecimal into bytevalue

System.out.println("Original "+bd);

dos.writeByte(bd.byteValue());

System.out.println("BigInt byte values. "+bd.byteValue());

dos.flush();

now on client side thats how i read it,, BUT bytevalue not the original

byte b = br.readByte();

System.out.println("I got byte value of bigint " + b);

Thanks in advance

asyed01a at 2007-7-12 20:30:53 > top of Java-index,Java Essentials,New To Java...
# 3
You need to look at the Javadoc for BigDecimal. In particular the constructors.
sabre150a at 2007-7-12 20:30:53 > top of Java-index,Java Essentials,New To Java...
# 4

Remember that BigDecimal -> http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html is serializable. So, in this case (and for any serializable type) you can do something like:

OutputStream os = socket.getOutputStrem();

ObjectOutputStream oos = new ObjectOutputStream(os);

BigDecimal bd = new BigDecimal("10.25");

oos.writeObject(bd);

oos.close();

You can find more info here:

http://java.sun.com/j2se/1.5.0/docs/api/java/io/ObjectOutputStream.html

http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#getOutputStream()

I hope it can help you!

albolocuraa at 2007-7-12 20:30:53 > top of Java-index,Java Essentials,New To Java...