PDA

Click to See Complete Forum and Search --> : how to get a char's byte equivalent?


Dillinger4
Jul 19th, 2001, 11:19 AM
I just wrote a quick hack to try and get a char's byte equivalent but im not sure if the output is correct.

a comes up as [B@50bd4d
b comes up as [B@3c4459
c comes up as [B@2b6651

i was expecting to see two bytes for each char ie.... 10101110 10101010

there not the character encoding because that is being returned by the getEncoding() method. so what are they?

thanks.....


import java.io.*;

public class getbytes{
public static void main(String[] args){

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(isr); // to get readLine() method

String comline;
System.out.println(" Character encoding is " + isr.getEncoding());

try{
do{
comline = buf.readLine();
buffer = comline.getBytes();
System.out.println(buffer);
}while(comline.length() != 0);

}catch(IOException e){System.out.println("Exception caught");}
}
}

filburt1
Jul 19th, 2001, 12:00 PM
Why not type cast it:


byte converted;
char original;

converted = (byte) original;


A byte is just a number from -127 to 127 (signed) or 0 to 255 (unsigned). It will never print out binary digits.

Dillinger4
Jul 19th, 2001, 05:48 PM
Im sorry, i ment bytes not binary. Im taking the Java 2 Cert exam tommorow so my brain i a little scrambled. Ill give it a try. Thanks