Converting byte[] to signed int

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

    Converting byte[] to signed int

    Need some help in converting a byte[] to a signed int. This is what I
    have attempted to do:

    byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
    StringBuilder hexString = new StringBuilder() ;
    foreach (byte tmpByte in bytes) hexString.Appen d(tmpByte.ToStr ing("X2"));

    int intValue = int.Parse(hexSt ring.ToString() ,
    NumberStyles.Al lowHexSpecific) ;

    I am getting a positive intValue. However, the intValue is supposed to
    be a negative value. Any help would be greatly appreciated.

    JT
  • Dennis Myrén

    #2
    Re: Converting byte[] to signed int

    I cannot see why you are expecting a negative value.
    [BASE16] FFFF9C = [BASE10] 16777116

    --
    Regards,
    Dennis JD Myrén
    Oslo Kodebureau
    "JT" <jt@jt.jt> wrote in message
    news:OFYOqCizEH A.2200@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Need some help in converting a byte[] to a signed int. This is what I
    > have attempted to do:
    >
    > byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
    > StringBuilder hexString = new StringBuilder() ;
    > foreach (byte tmpByte in bytes) hexString.Appen d(tmpByte.ToStr ing("X2"));
    >
    > int intValue = int.Parse(hexSt ring.ToString() ,
    > NumberStyles.Al lowHexSpecific) ;
    >
    > I am getting a positive intValue. However, the intValue is supposed to be
    > a negative value. Any help would be greatly appreciated.
    >
    > JT[/color]


    Comment

    • JT

      #3
      Re: Converting byte[] to signed int

      Sorry, I should explained my situation abit more. I am getting some
      bytes from a device connected to the serial port. The device displays a
      negative value and the bytes I am getting for the value is FFFF9C.
      Also, the API documentation says that the bytes are in Big-Endian. In
      this case, would FFFF9C translate to some negative integer value?

      Dennis Myrén wrote:
      [color=blue]
      > I cannot see why you are expecting a negative value.
      > [BASE16] FFFF9C = [BASE10] 16777116
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Converting byte[] to signed int

        JT <jt@jt.jt> wrote:[color=blue]
        > Need some help in converting a byte[] to a signed int. This is what I
        > have attempted to do:
        >
        > byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
        > StringBuilder hexString = new StringBuilder() ;
        > foreach (byte tmpByte in bytes) hexString.Appen d(tmpByte.ToStr ing("X2"));
        >
        > int intValue = int.Parse(hexSt ring.ToString() ,
        > NumberStyles.Al lowHexSpecific) ;
        >
        > I am getting a positive intValue. However, the intValue is supposed to
        > be a negative value. Any help would be greatly appreciated.[/color]

        A 24-bit signed int is quite rare - did you really mean it to only have
        3 bytes rather than 4?

        You can use BitConverter to do the conversion without using a string
        format. If that's the wrong endianness, you can use my
        EndianBitConver ter from
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Dennis Myrén

          #5
          Re: Converting byte[] to signed int

          Then, try:
          int result = (a [0x2] << 0x8) + (a [0x1] << 0x10) + (a [0x0] << 0x18);

          --
          Regards,
          Dennis JD Myrén
          Oslo Kodebureau
          "JT" <jt@jt.jt> wrote in message
          news:uygZ%23Liz EHA.4028@TK2MSF TNGP15.phx.gbl. ..[color=blue]
          > Sorry, I should explained my situation abit more. I am getting some bytes
          > from a device connected to the serial port. The device displays a
          > negative value and the bytes I am getting for the value is FFFF9C. Also,
          > the API documentation says that the bytes are in Big-Endian. In this
          > case, would FFFF9C translate to some negative integer value?
          >
          > Dennis Myrén wrote:
          >[color=green]
          >> I cannot see why you are expecting a negative value.
          >> [BASE16] FFFF9C = [BASE10] 16777116
          >>[/color][/color]


          Comment

          • Dennis Myrén

            #6
            Re: Converting byte[] to signed int

            (Where 'a' is represents your variable 'bytes')

            --
            Regards,
            Dennis JD Myrén
            Oslo Kodebureau
            "Dennis Myrén" <dennis@oslokb. no> wrote in message
            news:lfknd.1753 7$Km6.208108@ne ws4.e.nsc.no...[color=blue]
            > Then, try:
            > int result = (a [0x2] << 0x8) + (a [0x1] << 0x10) + (a [0x0] << 0x18);
            >
            > --
            > Regards,
            > Dennis JD Myrén
            > Oslo Kodebureau
            > "JT" <jt@jt.jt> wrote in message
            > news:uygZ%23Liz EHA.4028@TK2MSF TNGP15.phx.gbl. ..[color=green]
            >> Sorry, I should explained my situation abit more. I am getting some
            >> bytes from a device connected to the serial port. The device displays a
            >> negative value and the bytes I am getting for the value is FFFF9C. Also,
            >> the API documentation says that the bytes are in Big-Endian. In this
            >> case, would FFFF9C translate to some negative integer value?
            >>
            >> Dennis Myrén wrote:
            >>[color=darkred]
            >>> I cannot see why you are expecting a negative value.
            >>> [BASE16] FFFF9C = [BASE10] 16777116
            >>>[/color][/color]
            >
            >[/color]


            Comment

            • Dennis Myrén

              #7
              Re: Converting byte[] to signed int

              JT wrote:[color=blue]
              > In this case, would FFFF9C translate to some negative integer value?[/color]

              Yes, it would be -25600 .

              --
              Regards,
              Dennis JD Myrén
              Oslo Kodebureau
              "JT" <jt@jt.jt> wrote in message
              news:uygZ%23Liz EHA.4028@TK2MSF TNGP15.phx.gbl. ..[color=blue]
              > Sorry, I should explained my situation abit more. I am getting some bytes
              > from a device connected to the serial port. The device displays a
              > negative value and the bytes I am getting for the value is FFFF9C. Also,
              > the API documentation says that the bytes are in Big-Endian. In this
              > case, would FFFF9C translate to some negative integer value?
              >
              > Dennis Myrén wrote:
              >[color=green]
              >> I cannot see why you are expecting a negative value.
              >> [BASE16] FFFF9C = [BASE10] 16777116
              >>[/color][/color]


              Comment

              • Liam McNamara

                #8
                Re: Converting byte[] to signed int

                The number given is positive for both big and little endian:

                Big: 00FFFF9C
                Little: 9CFFFF00

                Only when the left-most bit is set will it become negative.

                using System.Net;

                byte[] data = new byte[] { 0x0, 0xFF, 0xFF, 0x9C };
                int bigEndian = BitConverter.To Int32(data, 0);
                int littleEndian = IPAddress.Netwo rkToHostOrder(b igEndian);

                --Liam.

                "JT" <jt@jt.jt> wrote in message
                news:uygZ%23Liz EHA.4028@TK2MSF TNGP15.phx.gbl. ..[color=blue]
                > Sorry, I should explained my situation abit more. I am getting some
                > bytes from a device connected to the serial port. The device displays a
                > negative value and the bytes I am getting for the value is FFFF9C.
                > Also, the API documentation says that the bytes are in Big-Endian. In
                > this case, would FFFF9C translate to some negative integer value?
                >
                > Dennis Myrén wrote:
                >[color=green]
                > > I cannot see why you are expecting a negative value.
                > > [BASE16] FFFF9C = [BASE10] 16777116
                > >[/color][/color]


                Comment

                • James Curran

                  #9
                  Re: Converting byte[] to signed int

                  What you need to do is "sign-extend" the upper bit from the third byte into
                  the fourth. (i.e., if the high bit of the highest byte you have is "1",
                  then every bit of the new fourth byte should be "1". Otherwise they should
                  be all "0"s)

                  byte[] data = new byte[] { 0x0, 0xFF, 0xFF, 0x9C };
                  data[0] = (byte) ((data[1] > 0x7f) ? 0xFF : 0x00);
                  int littleEndian = BitConverter.To Int32(data, 0);
                  int bigEndian= IPAddress.Netwo rkToHostOrder(l ittleEndian);
                  Console.WriteLi ne( "{0}, {0:x}",bigEndia n);

                  This correct prints out "-100, FFFFFF9C"

                  Note that Liam had bigEndian & littleEndian reversed in his code.


                  --
                  Truth,
                  James Curran
                  [erstwhile VC++ MVP]
                  Home: www.noveltheory.com Work: www.njtheater.com
                  Blog: www.honestillusion.com Day Job: www.partsearch.com

                  "JT" <jt@jt.jt> wrote in message
                  news:OFYOqCizEH A.2200@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > Need some help in converting a byte[] to a signed int. This is what I
                  > have attempted to do:
                  >
                  > byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
                  > StringBuilder hexString = new StringBuilder() ;
                  > foreach (byte tmpByte in bytes) hexString.Appen d(tmpByte.ToStr ing("X2"));
                  >
                  > int intValue = int.Parse(hexSt ring.ToString() ,
                  > NumberStyles.Al lowHexSpecific) ;
                  >
                  > I am getting a positive intValue. However, the intValue is supposed to
                  > be a negative value. Any help would be greatly appreciated.
                  >
                  > JT[/color]


                  Comment

                  • JT

                    #10
                    Re: Converting byte[] to signed int

                    thanks james. another question. in the event, the byte
                    data is 2 bytes, how would this be handled? thanks.
                    [color=blue]
                    >-----Original Message-----
                    >What you need to do is "sign-extend" the upper bit from[/color]
                    the third byte into[color=blue]
                    >the fourth. (i.e., if the high bit of the highest byte[/color]
                    you have is "1",[color=blue]
                    >then every bit of the new fourth byte should be "1".[/color]
                    Otherwise they should[color=blue]
                    >be all "0"s)
                    >
                    > byte[] data = new byte[] { 0x0, 0xFF, 0xFF, 0x9C };
                    > data[0] = (byte) ((data[1] > 0x7f) ? 0xFF : 0x00);
                    > int littleEndian = BitConverter.To Int32(data, 0);
                    > int bigEndian= IPAddress.Netwo rkToHostOrder[/color]
                    (littleEndian);[color=blue]
                    > Console.WriteLi ne( "{0}, {0:x}",bigEndia n);
                    >
                    >This correct prints out "-100, FFFFFF9C"
                    >
                    >Note that Liam had bigEndian & littleEndian reversed in[/color]
                    his code.[color=blue]
                    >
                    >
                    >--
                    >Truth,
                    >James Curran
                    >[erstwhile VC++ MVP]
                    >Home: www.noveltheory.com Work: www.njtheater.com
                    >Blog: www.honestillusion.com Day Job: www.partsearch.com
                    >
                    >"JT" <jt@jt.jt> wrote in message
                    >news:OFYOqCizE HA.2200@TK2MSFT NGP09.phx.gbl.. .[color=green]
                    >> Need some help in converting a byte[] to a signed int.[/color][/color]
                    This is what I[color=blue][color=green]
                    >> have attempted to do:
                    >>
                    >> byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
                    >> StringBuilder hexString = new StringBuilder() ;
                    >> foreach (byte tmpByte in bytes) hexString.Appen d[/color][/color]
                    (tmpByte.ToStri ng("X2"));[color=blue][color=green]
                    >>
                    >> int intValue = int.Parse(hexSt ring.ToString() ,
                    >> NumberStyles.Al lowHexSpecific) ;
                    >>
                    >> I am getting a positive intValue. However, the[/color][/color]
                    intValue is supposed to[color=blue][color=green]
                    >> be a negative value. Any help would be greatly[/color][/color]
                    appreciated.[color=blue][color=green]
                    >>
                    >> JT[/color]
                    >
                    >
                    >.
                    >[/color]

                    Comment

                    • James Curran

                      #11
                      Re: Converting byte[] to signed int

                      "JT" <anonymous@disc ussions.microso ft.com> wrote in message
                      news:7a6501c4d0 35$0b4aebc0$a40 1280a@phx.gbl.. .[color=blue]
                      > thanks james. another question. in the event, the byte
                      > data is 2 bytes, how would this be handled? thanks.[/color]

                      int littleEndian = BitConverter.To Int16(data, 0);

                      In this case the sign-bit is already in the correct place, so know other
                      modification is needed.

                      --
                      Truth,
                      James Curran
                      [erstwhile VC++ MVP]
                      Home: www.noveltheory.com Work: www.njtheater.com
                      Blog: www.honestillusion.com Day Job: www.partsearch.com
                      [color=blue]
                      >[color=green]
                      > >-----Original Message-----
                      > >What you need to do is "sign-extend" the upper bit from[/color]
                      > the third byte into[color=green]
                      > >the fourth. (i.e., if the high bit of the highest byte[/color]
                      > you have is "1",[color=green]
                      > >then every bit of the new fourth byte should be "1".[/color]
                      > Otherwise they should[color=green]
                      > >be all "0"s)
                      > >
                      > > byte[] data = new byte[] { 0x0, 0xFF, 0xFF, 0x9C };
                      > > data[0] = (byte) ((data[1] > 0x7f) ? 0xFF : 0x00);
                      > > int littleEndian = BitConverter.To Int32(data, 0);
                      > > int bigEndian= IPAddress.Netwo rkToHostOrder[/color]
                      > (littleEndian);[color=green]
                      > > Console.WriteLi ne( "{0}, {0:x}",bigEndia n);
                      > >
                      > >This correct prints out "-100, FFFFFF9C"
                      > >
                      > >Note that Liam had bigEndian & littleEndian reversed in[/color]
                      > his code.[color=green]
                      > >
                      > >
                      > >--
                      > >Truth,
                      > >James Curran
                      > >[erstwhile VC++ MVP]
                      > >Home: www.noveltheory.com Work: www.njtheater.com
                      > >Blog: www.honestillusion.com Day Job: www.partsearch.com
                      > >
                      > >"JT" <jt@jt.jt> wrote in message
                      > >news:OFYOqCizE HA.2200@TK2MSFT NGP09.phx.gbl.. .[color=darkred]
                      > >> Need some help in converting a byte[] to a signed int.[/color][/color]
                      > This is what I[color=green][color=darkred]
                      > >> have attempted to do:
                      > >>
                      > >> byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
                      > >> StringBuilder hexString = new StringBuilder() ;
                      > >> foreach (byte tmpByte in bytes) hexString.Appen d[/color][/color]
                      > (tmpByte.ToStri ng("X2"));[color=green][color=darkred]
                      > >>
                      > >> int intValue = int.Parse(hexSt ring.ToString() ,
                      > >> NumberStyles.Al lowHexSpecific) ;
                      > >>
                      > >> I am getting a positive intValue. However, the[/color][/color]
                      > intValue is supposed to[color=green][color=darkred]
                      > >> be a negative value. Any help would be greatly[/color][/color]
                      > appreciated.[color=green][color=darkred]
                      > >>
                      > >> JT[/color]
                      > >
                      > >
                      > >.
                      > >[/color][/color]


                      Comment

                      Working...