converting from bit->byte->char->byte

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicholas Parnell

    converting from bit->byte->char->byte

    Hi! I have the problem where I cast a bit string of "10000000" to a
    byte, which i get -128; this is fine so far. However, when I take this byte
    and cast it to a char, I get a question mark('?'). So when i go to cast it
    back to a byte, i now get 63 (the ascii representation of a question mark).
    I see that for any byte from 128 to 256 i'll get a question mark... must be
    java's way of indicating that's it's negative or something.... my
    question to you all is:

    1) Why is it doing this?

    2) What can I do so that I can cast -128 to char and back to a byte to
    get -128 again?

    Thanks!


  • SPG

    #2
    Re: converting from bit->byte->char->byte

    Hi,

    As I understand it, A byte goes from -128 to 128. A char goes from 0 to 255.
    So, to convert from byte to a char correctly, you could do this:

    byte b = -128;
    char c = (char)b;
    System.out.prin tln(c);
    char c2 = (char)(b + 128);
    System.out.prin tln(c2);
    byte b2 = (byte)(c2 -128);

    Clunky, but works.
    Depends on what you want to use it for, you may want to try using a reader
    or a stream..

    Steve

    "Nicholas Parnell" <nicholas.parne [email protected]> wrote in message
    news:1077127489 [email protected] et.dnd.ca...[color=blue]
    > Hi! I have the problem where I cast a bit string of "10000000" to a
    > byte, which i get -128; this is fine so far. However, when I take this[/color]
    byte[color=blue]
    > and cast it to a char, I get a question mark('?'). So when i go to cast[/color]
    it[color=blue]
    > back to a byte, i now get 63 (the ascii representation of a question[/color]
    mark).[color=blue]
    > I see that for any byte from 128 to 256 i'll get a question mark... must[/color]
    be[color=blue]
    > java's way of indicating that's it's negative or something.... my
    > question to you all is:
    >
    > 1) Why is it doing this?
    >
    > 2) What can I do so that I can cast -128 to char and back to a byte to
    > get -128 again?
    >
    > Thanks!
    >
    >[/color]


    Comment

    • SPG

      #3
      Re: converting from bit-&gt;byte-&gt;char-&gt;byte

      A better way to make a char from a byte would be:

      byte b = -128;
      //To Char
      char c = (char)(b & 0xFF);
      //To Byte
      byte b2 = (byte)(c & 0xFF);




      "Nicholas Parnell" <nicholas.parne [email protected]> wrote in message
      news:1077127489 [email protected] et.dnd.ca...[color=blue]
      > Hi! I have the problem where I cast a bit string of "10000000" to a
      > byte, which i get -128; this is fine so far. However, when I take this[/color]
      byte[color=blue]
      > and cast it to a char, I get a question mark('?'). So when i go to cast[/color]
      it[color=blue]
      > back to a byte, i now get 63 (the ascii representation of a question[/color]
      mark).[color=blue]
      > I see that for any byte from 128 to 256 i'll get a question mark... must[/color]
      be[color=blue]
      > java's way of indicating that's it's negative or something.... my
      > question to you all is:
      >
      > 1) Why is it doing this?
      >
      > 2) What can I do so that I can cast -128 to char and back to a byte to
      > get -128 again?
      >
      > Thanks!
      >
      >[/color]


      Comment

      Working...