Working with bytes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cassiano
    New Member
    • Jun 2007
    • 2

    #1

    Working with bytes

    I need to convert 11011101 to a byte but when I try:
    Code:
    Byte.valueOf("11011101", 2);

    Error:
    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: Value out of range. Value:"11011101" Radix:2

    What I have to do to decide this problem?
    How I decide this problem with the band of representation of a byte?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Cassiano
    I need to convert 11011101 to a byte but when I try:
    Code:
    Byte.valueOf("11011101", 2);

    Error:
    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: Value out of range. Value:"11011101" Radix:2

    What I have to do to decide this problem?
    How I decide this problem with the band of representation of a byte?
    But it's telling you that the value is out of range. It's too big to be stored as a byte.

    Comment

    • praveen2gupta
      New Member
      • May 2007
      • 200

      #3
      Hi

      Byte data type can store maximum 127 decimal number value. If you try to store more than that it wil show errors at compiler.

      If you numbr is in binary and you wants to convert it in decinal then . I think you should read a begainners level book.

      Comment

      • Cassiano
        New Member
        • Jun 2007
        • 2

        #4
        I know that, but i'd like to know if have a possibility to represent a decimal > 127 in just 1 byte, how I can do that? This is possible ?

        Theoretically: 1 byte have 8 bits, so can be 11011101 for example.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Cassiano
          I know that, but i'd like to know if have a possibility to represent a decimal > 127 in just 1 byte, how I can do that? This is possible ?

          Theoretically: 1 byte have 8 bits, so can be 11011101 for example.
          I hope you are not confusing changing to binary and changing to byte.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            @OP: note that you can cast an int back to a byte in which case the lowest
            eight bits are stored in the byte. (hint, hint ;-)

            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              @OP: note that you can cast an int back to a byte in which case the lowest
              eight bits are stored in the byte. (hint, hint ;-)

              kind regards,

              Jos
              About time you showed up.
              You have no idea what mess I've been getting myself into.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by r035198x
                About time you showed up.
                You have no idea what mess I've been getting myself into.
                What's the mess then? I saw a very slow thread going on about Exceptions, but
                that was about it. This thread's problem is almost solved thanks/due to my all
                spoiling hint ;-)

                kind regards,

                Jos (still got work to do but I'll take a peek here now and then)

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JosAH
                  What's the mess then? I saw a very slow thread going on about Exceptions, but
                  that was about it. This thread's problem is almost solved thanks/due to my all
                  spoiling hint ;-)

                  kind regards,

                  Jos (still got work to do but I'll take a peek here now and then)
                  You haven't seen the private class declaration thread then.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    You haven't seen the private class declaration thread then.
                    Nope, I'm afraid not; what's the thread title? I'll take a look and see if I can create
                    an even greater mess out of it then ;-)

                    kind regards,

                    Jos

                    Comment

                    Working...