Byte to 0 and 1 String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Winterrage
    New Member
    • Sep 2007
    • 8

    #1

    Byte to 0 and 1 String

    Greetings,

    Now that i've solver my problem writting a string made of 0 and 1 to a byte. I need to do the reverse process.

    I use:
    Integer.toBinar yString((int)re adedBytes.get(i ))

    It returns some odd data like:
    111111111111111 111111111110110 11

    It should be returning 8 character ? maybe I am wrong..

    Anyway, is there any known way to convert a Byte into a string containing a bunch 0 and 1.

    Thanks again!
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by Winterrage
    Greetings,

    Now that i've solver my problem writting a string made of 0 and 1 to a byte. I need to do the reverse process.

    I use:
    Integer.toBinar yString((int)re adedBytes.get(i ))

    It returns some odd data like:
    111111111111111 111111111110110 11

    It should be returning 8 character ? maybe I am wrong..

    Anyway, is there any known way to convert a Byte into a string containing a bunch 0 and 1.

    Thanks again!
    Sure there are known ways - one (probably the wanted way) would be to do manually. You know, where the 1s and 0s come from? Say you have the Byte 9. How do you get the Binary representation (if you want 8 characters: 00001001) from that?

    It's no different from what you would do manually - you just have tell your computer how to do it.

    Greetings,
    Nepomuk

    PS.: System.out.prin tln(Integer.toB inaryString(9)) ; will only give you 1001 as 0s at the front are ignored.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Winterrage
      Greetings,

      Now that i've solver my problem writting a string made of 0 and 1 to a byte. I need to do the reverse process.

      I use:
      Integer.toBinar yString((int)re adedBytes.get(i ))

      It returns some odd data like:
      111111111111111 111111111110110 11

      It should be returning 8 character ? maybe I am wrong..

      Anyway, is there any known way to convert a Byte into a string containing a bunch 0 and 1.

      Thanks again!
      Bytes, shorts, ints and longs are all signed; e.g. -1 is represented as a byte as
      11111111; as 111111111111111 1 as a short etc. This is the 'sign extension'
      mechanism. Simply take the last eight characters from the String representation
      and you're in business. Or alternatively chop off all those 'generated' bits by using
      this:

      [code=java]
      byte b= -1;
      String rep= Integer.toBinar yString(b&0xff) ;
      [/code]

      kind regards,

      Jos

      Comment

      • Winterrage
        New Member
        • Sep 2007
        • 8

        #4
        Thanks everyone for the reply :)

        Comment

        Working...