Results 1 to 3 of 3

Thread: how to get a char's byte equivalent?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    how to get a char's byte equivalent?

    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");}
    }
    }

  2. #2
    Why not type cast it:

    Code:
    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.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width