Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Binary converting byte to sbyte

Binary converting byte to sbyte

Scheduled Pinned Locked Moved C#
visual-studiodata-structureshelpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marco Stinger
    wrote on last edited by
    #1

    Hi coders ! I need to binary convert a byte (unsigned 8 bit, 0 to 255) to an sbyte (signed 8 bit, -128 to 127). I have no problem converting an array of bytes to any other type using BitConverter.To[type] functions, but there's no BitConverter.ToSByte .... VS 2003 - compact framework SP3 any ideas ? Ciao Marco -- modified at 9:54 Thursday 26th January, 2006

    G 1 Reply Last reply
    0
    • M Marco Stinger

      Hi coders ! I need to binary convert a byte (unsigned 8 bit, 0 to 255) to an sbyte (signed 8 bit, -128 to 127). I have no problem converting an array of bytes to any other type using BitConverter.To[type] functions, but there's no BitConverter.ToSByte .... VS 2003 - compact framework SP3 any ideas ? Ciao Marco -- modified at 9:54 Thursday 26th January, 2006

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can just convert it, ignoring overflow, as there is no data loss. byte source = -42; sbyte dest; unsafe { dest = (sbyte)source; } --- b { font-weight: normal; }

      M S 2 Replies Last reply
      0
      • G Guffa

        You can just convert it, ignoring overflow, as there is no data loss. byte source = -42; sbyte dest; unsafe { dest = (sbyte)source; } --- b { font-weight: normal; }

        M Offline
        M Offline
        Marco Stinger
        wrote on last edited by
        #3

        Are you sure ? First off byte source = -42; is indeed wrong since byte is UNSIGNED (0 to 255). Second a simple cast doesn't convert BINARY the values, meaning a 10010011 is (let's say) 215 for a byte and -81 for a sbyte. still looknig for help Ciao Marco ;)

        L G 2 Replies Last reply
        0
        • M Marco Stinger

          Are you sure ? First off byte source = -42; is indeed wrong since byte is UNSIGNED (0 to 255). Second a simple cast doesn't convert BINARY the values, meaning a 10010011 is (let's say) 215 for a byte and -81 for a sbyte. still looknig for help Ciao Marco ;)

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          tried unckecked ?

          xacc.ide-0.1.1.11 - now with Settings & AutoComplete(kinda)

          1 Reply Last reply
          0
          • G Guffa

            You can just convert it, ignoring overflow, as there is no data loss. byte source = -42; sbyte dest; unsafe { dest = (sbyte)source; } --- b { font-weight: normal; }

            S Offline
            S Offline
            Saravanan_article
            wrote on last edited by
            #5

            Try to use Convert.ToSByte(source); May be this will work. byte source =42; sbyte dest; dest = convert.ToSByte(source);

            1 Reply Last reply
            0
            • M Marco Stinger

              Are you sure ? First off byte source = -42; is indeed wrong since byte is UNSIGNED (0 to 255). Second a simple cast doesn't convert BINARY the values, meaning a 10010011 is (let's say) 215 for a byte and -81 for a sbyte. still looknig for help Ciao Marco ;)

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Just a typo. Of course a byte can't be -42. It was unchecked (as leppie suggested) that I was going for, not unsafe. However, some testing reveals that you can just cast the value from byte to sbyte. You don't need unchecked mode for casting between signed and unsigned data types. byte source = 214; sbyte dest; dest = (sbyte)source; Now dest will contain the value -42.

              Marco [Stinger] wrote:

              Second a simple cast doesn't convert BINARY the values

              What do you mean by convert BINARY, then? --- b { font-weight: normal; }

              M 1 Reply Last reply
              0
              • G Guffa

                Just a typo. Of course a byte can't be -42. It was unchecked (as leppie suggested) that I was going for, not unsafe. However, some testing reveals that you can just cast the value from byte to sbyte. You don't need unchecked mode for casting between signed and unsigned data types. byte source = 214; sbyte dest; dest = (sbyte)source; Now dest will contain the value -42.

                Marco [Stinger] wrote:

                Second a simple cast doesn't convert BINARY the values

                What do you mean by convert BINARY, then? --- b { font-weight: normal; }

                M Offline
                M Offline
                Marco Stinger
                wrote on last edited by
                #7

                It works perfectly, I did the same test and got the same result. By binary I meant exaclty that : having a brunch of bits read from the sbyte point of view (so to speak) Thanks a LOT everyone !!! Ciao Marco ps: I WANT to install vs 2005 .... it's GREAT, but I have only 1 PC and some soon to be released products in VS 2003. .... I MUST WAIT ..... .... I MUST WAIT ..... .... I MUST WAIT ..... .... I MUST WAIT ..... ;)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups