How to Convert Byte Array to Int32 (Big-endian)

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

    How to Convert Byte Array to Int32 (Big-endian)

    I thought this had come up before, but I now cannot find it.

    I have a byte array, such as

    Dim a() As Byte = {1, 2, 3, 4}

    I want to convert this to an Int32 = 01020304 (hex).

    If I use BitConverter, I get 04030201.

    Is there a built-in way to do this, or am I stuck with extracting and
    shifting each byte manually?

    TIA

    Charles


  • Cor Ligthert

    #2
    Re: How to Convert Byte Array to Int32 (Big-endian)

    Charles,

    We have a long time not seen you, so welcome back,

    Maybe a crazy idea for you, however multiplying and adding?

    Cor


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: How to Convert Byte Array to Int32 (Big-endian)

      "Charles Law" <blank@nowhere. com> schrieb:[color=blue]
      >I thought this had come up before, but I now cannot find it.
      >
      > I have a byte array, such as
      >
      > Dim a() As Byte = {1, 2, 3, 4}
      >
      > I want to convert this to an Int32 = 01020304 (hex).
      >
      > If I use BitConverter, I get 04030201.
      >
      > Is there a built-in way to do this, or am I stuck with extracting and
      > shifting each byte manually?[/color]

      You may want to reverse the byte array before using 'BitConverter'.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Charles Law

        #4
        Re: How to Convert Byte Array to Int32 (Big-endian)

        Hi Cor

        I haven't really been away, just a bit quiet. I did respond to someone the
        other day about a serial comms issue crashing their prog, but no word yet
        whether it fixed the problem.
        [color=blue]
        > Maybe a crazy idea for you, however multiplying and adding?[/color]

        Yes, that is currently what I do, or rather what the VB6 code that I am
        converting does. I was just looking for a native .NET way to do it. It seems
        to me that there should be a switch, or extra parameter to pass that allows
        one to specify big or little-endianness, but I haven't spotted it.

        Charles


        "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
        news:%23c85Pk7$ EHA.1524@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > Charles,
        >
        > We have a long time not seen you, so welcome back,
        >
        > Maybe a crazy idea for you, however multiplying and adding?
        >
        > Cor
        >[/color]


        Comment

        • Charles Law

          #5
          Re: How to Convert Byte Array to Int32 (Big-endian)

          Hi Herfried

          As I have just replied to Cor, I kind of expected a switch somewhere to
          allow me to specify the endianness of the operation, but it seems to be
          fixed.

          I had toyed with reversing the bytes, but then I might just as well stick to
          carving the array up by hand. Besides, it makes it a two-stage process, so
          to avoid duplication I would need a function wrapper.

          Perhaps I am just being picky, but I like to use built-in stuff wherever
          possible; or maybe it's laziness ;-)

          Charles


          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
          news:uU6iyn7$EH A.1300@TK2MSFTN GP14.phx.gbl...[color=blue]
          > "Charles Law" <blank@nowhere. com> schrieb:[color=green]
          >>I thought this had come up before, but I now cannot find it.
          >>
          >> I have a byte array, such as
          >>
          >> Dim a() As Byte = {1, 2, 3, 4}
          >>
          >> I want to convert this to an Int32 = 01020304 (hex).
          >>
          >> If I use BitConverter, I get 04030201.
          >>
          >> Is there a built-in way to do this, or am I stuck with extracting and
          >> shifting each byte manually?[/color]
          >
          > You may want to reverse the byte array before using 'BitConverter'.
          >
          > --
          > M S Herfried K. Wagner
          > M V P <URL:http://dotnet.mvps.org/>
          > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: How to Convert Byte Array to Int32 (Big-endian)

            Charles,
            Jon Skeet has a EndianBitConver ter at
            http://www.yoda.arachsys.com/csharp/miscutil/ that works similar to
            BitConverter but either Little-Endian or Big-Endian depending on which you
            pick...

            Its in C#, however you should be able to use directly as a class assembly,
            or easily converted to VB.NET as the source is available...

            Hope this helps
            Jay

            "Charles Law" <blank@nowhere. com> wrote in message
            news:ODJHwi6$EH A.3988@TK2MSFTN GP11.phx.gbl...[color=blue]
            >I thought this had come up before, but I now cannot find it.
            >
            > I have a byte array, such as
            >
            > Dim a() As Byte = {1, 2, 3, 4}
            >
            > I want to convert this to an Int32 = 01020304 (hex).
            >
            > If I use BitConverter, I get 04030201.
            >
            > Is there a built-in way to do this, or am I stuck with extracting and
            > shifting each byte manually?
            >
            > TIA
            >
            > Charles
            >
            >[/color]


            Comment

            • Dennis

              #7
              RE: How to Convert Byte Array to Int32 (Big-endian)

              Here's a couple of functions that I use...probably a better way to do it but
              it works:

              Friend Function ConvIntegertoBy teArray(ByVal n As Long, ByVal lg As
              Integer) As Byte()
              'converts an integer to a byte array of length lg
              Dim m() As Byte = New Byte(lg - 1) {}
              Dim i, k As Integer
              Dim h As String
              h = Hex(n).PadLeft( 16, "0"c)
              k = 16
              For i = lg - 1 To 0 Step -1
              k = k - 2
              m(i) = CByte("&H" & h.Substring(k, 2))
              Next
              Return m
              End Function

              Public Function ConvByteArrayto Integer(ByVal b As Byte(), Optional ByVal
              ln As Integer = 0, Optional ByVal sidx As Integer = 0) As Long
              Dim i As Integer
              Dim j, k As Long
              If ln = 0 Then ln = UBound(b) + 1
              ln = sidx + ln - 1
              k = 1
              j = CInt(b(ln))
              For i = ln - 1 To sidx Step -1
              k = 256 * k
              j = j + k * b(i)
              Next
              Return j
              End Function

              "Charles Law" wrote:
              [color=blue]
              > I thought this had come up before, but I now cannot find it.
              >
              > I have a byte array, such as
              >
              > Dim a() As Byte = {1, 2, 3, 4}
              >
              > I want to convert this to an Int32 = 01020304 (hex).
              >
              > If I use BitConverter, I get 04030201.
              >
              > Is there a built-in way to do this, or am I stuck with extracting and
              > shifting each byte manually?
              >
              > TIA
              >
              > Charles
              >
              >
              >[/color]

              Comment

              • Charles Law

                #8
                Re: How to Convert Byte Array to Int32 (Big-endian)

                Hi Dennis

                Thanks for the reply. As I said in a couple of other replies, it is not so
                much the ability to manipulate the data by hand that eludes me, but the
                native .NET way, if any. I suppose I am jut trying to avoid re-inventing the
                wheel if, indeed, such a wheel exists.

                Charles


                "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
                news:AA34156E-F28D-4F92-9C17-38DA9EFDC5E9@mi crosoft.com...[color=blue]
                > Here's a couple of functions that I use...probably a better way to do it
                > but
                > it works:
                >
                > Friend Function ConvIntegertoBy teArray(ByVal n As Long, ByVal lg As
                > Integer) As Byte()
                > 'converts an integer to a byte array of length lg
                > Dim m() As Byte = New Byte(lg - 1) {}
                > Dim i, k As Integer
                > Dim h As String
                > h = Hex(n).PadLeft( 16, "0"c)
                > k = 16
                > For i = lg - 1 To 0 Step -1
                > k = k - 2
                > m(i) = CByte("&H" & h.Substring(k, 2))
                > Next
                > Return m
                > End Function
                >
                > Public Function ConvByteArrayto Integer(ByVal b As Byte(), Optional
                > ByVal
                > ln As Integer = 0, Optional ByVal sidx As Integer = 0) As Long
                > Dim i As Integer
                > Dim j, k As Long
                > If ln = 0 Then ln = UBound(b) + 1
                > ln = sidx + ln - 1
                > k = 1
                > j = CInt(b(ln))
                > For i = ln - 1 To sidx Step -1
                > k = 256 * k
                > j = j + k * b(i)
                > Next
                > Return j
                > End Function
                >
                > "Charles Law" wrote:
                >[color=green]
                >> I thought this had come up before, but I now cannot find it.
                >>
                >> I have a byte array, such as
                >>
                >> Dim a() As Byte = {1, 2, 3, 4}
                >>
                >> I want to convert this to an Int32 = 01020304 (hex).
                >>
                >> If I use BitConverter, I get 04030201.
                >>
                >> Is there a built-in way to do this, or am I stuck with extracting and
                >> shifting each byte manually?
                >>
                >> TIA
                >>
                >> Charles
                >>
                >>
                >>[/color][/color]


                Comment

                • Charles Law

                  #9
                  Re: How to Convert Byte Array to Int32 (Big-endian)

                  Hi Jay

                  Thanks for the link. I have had a look and I see that Jon uses, in essence,
                  the same approach that I have at present. I guess that is also how the .NET
                  little endian BitConverter method is coded under the covers, so it seems
                  that my best bet is just to wrap the functions I have a bit better and leave
                  it at that.

                  Cheers.

                  Charles


                  "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                  news:O38Z1$9$EH A.2012@TK2MSFTN GP15.phx.gbl...[color=blue]
                  > Charles,
                  > Jon Skeet has a EndianBitConver ter at
                  > http://www.yoda.arachsys.com/csharp/miscutil/ that works similar to
                  > BitConverter but either Little-Endian or Big-Endian depending on which you
                  > pick...
                  >
                  > Its in C#, however you should be able to use directly as a class assembly,
                  > or easily converted to VB.NET as the source is available...
                  >
                  > Hope this helps
                  > Jay
                  >
                  > "Charles Law" <blank@nowhere. com> wrote in message
                  > news:ODJHwi6$EH A.3988@TK2MSFTN GP11.phx.gbl...[color=green]
                  >>I thought this had come up before, but I now cannot find it.
                  >>
                  >> I have a byte array, such as
                  >>
                  >> Dim a() As Byte = {1, 2, 3, 4}
                  >>
                  >> I want to convert this to an Int32 = 01020304 (hex).
                  >>
                  >> If I use BitConverter, I get 04030201.
                  >>
                  >> Is there a built-in way to do this, or am I stuck with extracting and
                  >> shifting each byte manually?
                  >>
                  >> TIA
                  >>
                  >> Charles
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: How to Convert Byte Array to Int32 (Big-endian)

                    Charles,
                    I would expect yours & Jon's approach to be very similar.

                    My point is that Jon has done all the "leg work" & created "complete"
                    versions of the classes. In other words Why reinvent the wheel?

                    Hope this helps
                    Jay

                    "Charles Law" <blank@nowhere. com> wrote in message
                    news:%23ml44gCA FHA.3852@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                    > Hi Jay
                    >
                    > Thanks for the link. I have had a look and I see that Jon uses, in
                    > essence, the same approach that I have at present. I guess that is also
                    > how the .NET little endian BitConverter method is coded under the covers,
                    > so it seems that my best bet is just to wrap the functions I have a bit
                    > better and leave it at that.
                    >
                    > Cheers.
                    >
                    > Charles
                    >
                    >
                    > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                    > news:O38Z1$9$EH A.2012@TK2MSFTN GP15.phx.gbl...[color=green]
                    >> Charles,
                    >> Jon Skeet has a EndianBitConver ter at
                    >> http://www.yoda.arachsys.com/csharp/miscutil/ that works similar to
                    >> BitConverter but either Little-Endian or Big-Endian depending on which
                    >> you pick...
                    >>
                    >> Its in C#, however you should be able to use directly as a class
                    >> assembly, or easily converted to VB.NET as the source is available...
                    >>
                    >> Hope this helps
                    >> Jay
                    >>
                    >> "Charles Law" <blank@nowhere. com> wrote in message
                    >> news:ODJHwi6$EH A.3988@TK2MSFTN GP11.phx.gbl...[color=darkred]
                    >>>I thought this had come up before, but I now cannot find it.
                    >>>
                    >>> I have a byte array, such as
                    >>>
                    >>> Dim a() As Byte = {1, 2, 3, 4}
                    >>>
                    >>> I want to convert this to an Int32 = 01020304 (hex).
                    >>>
                    >>> If I use BitConverter, I get 04030201.
                    >>>
                    >>> Is there a built-in way to do this, or am I stuck with extracting and
                    >>> shifting each byte manually?
                    >>>
                    >>> TIA
                    >>>
                    >>> Charles
                    >>>
                    >>>[/color]
                    >>
                    >>[/color]
                    >
                    >[/color]


                    Comment

                    • Charles Law

                      #11
                      Re: How to Convert Byte Array to Int32 (Big-endian)

                      Indeed. Fair point.

                      Thanks.

                      Charles


                      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                      news:uzZE7BWAFH A.3436@TK2MSFTN GP09.phx.gbl...[color=blue]
                      > Charles,
                      > I would expect yours & Jon's approach to be very similar.
                      >
                      > My point is that Jon has done all the "leg work" & created "complete"
                      > versions of the classes. In other words Why reinvent the wheel?
                      >
                      > Hope this helps
                      > Jay
                      >
                      > "Charles Law" <blank@nowhere. com> wrote in message
                      > news:%23ml44gCA FHA.3852@TK2MSF TNGP10.phx.gbl. ..[color=green]
                      >> Hi Jay
                      >>
                      >> Thanks for the link. I have had a look and I see that Jon uses, in
                      >> essence, the same approach that I have at present. I guess that is also
                      >> how the .NET little endian BitConverter method is coded under the covers,
                      >> so it seems that my best bet is just to wrap the functions I have a bit
                      >> better and leave it at that.
                      >>
                      >> Cheers.
                      >>
                      >> Charles
                      >>
                      >>
                      >> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                      >> news:O38Z1$9$EH A.2012@TK2MSFTN GP15.phx.gbl...[color=darkred]
                      >>> Charles,
                      >>> Jon Skeet has a EndianBitConver ter at
                      >>> http://www.yoda.arachsys.com/csharp/miscutil/ that works similar to
                      >>> BitConverter but either Little-Endian or Big-Endian depending on which
                      >>> you pick...
                      >>>
                      >>> Its in C#, however you should be able to use directly as a class
                      >>> assembly, or easily converted to VB.NET as the source is available...
                      >>>
                      >>> Hope this helps
                      >>> Jay
                      >>>
                      >>> "Charles Law" <blank@nowhere. com> wrote in message
                      >>> news:ODJHwi6$EH A.3988@TK2MSFTN GP11.phx.gbl...
                      >>>>I thought this had come up before, but I now cannot find it.
                      >>>>
                      >>>> I have a byte array, such as
                      >>>>
                      >>>> Dim a() As Byte = {1, 2, 3, 4}
                      >>>>
                      >>>> I want to convert this to an Int32 = 01020304 (hex).
                      >>>>
                      >>>> If I use BitConverter, I get 04030201.
                      >>>>
                      >>>> Is there a built-in way to do this, or am I stuck with extracting and
                      >>>> shifting each byte manually?
                      >>>>
                      >>>> TIA
                      >>>>
                      >>>> Charles
                      >>>>
                      >>>>
                      >>>
                      >>>[/color]
                      >>
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      • Gerald Hernandez

                        #12
                        Re: How to Convert Byte Array to Int32 (Big-endian)


                        "Charles Law" <blank@nowhere. com> wrote in message
                        news:ODJHwi6$EH A.3988@TK2MSFTN GP11.phx.gbl...[color=blue]
                        > I thought this had come up before, but I now cannot find it.
                        >
                        > I have a byte array, such as
                        >
                        > Dim a() As Byte = {1, 2, 3, 4}
                        >
                        > I want to convert this to an Int32 = 01020304 (hex).
                        >
                        > If I use BitConverter, I get 04030201.
                        >
                        > Is there a built-in way to do this, or am I stuck with extracting and
                        > shifting each byte manually?
                        >
                        > TIA
                        >
                        > Charles
                        >[/color]

                        You could try something like this:

                        Private Function Int32FromByteAr ray(ByVal BA() As Byte, ByVal
                        SwitchEndian As Boolean) As Integer
                        Dim outVal As Integer

                        outVal = BitConverter.To Int32(BA, 0)
                        If SwitchEndian = True Then
                        outVal = System.Net.IPAd dress.NetworkTo HostOrder(outVa l)
                        End If
                        Return outVal
                        End Function

                        NetworkToHostOr der - Big To Little Endian
                        HostToNetworkOr der - Little To Big Endian
                        Although in this case it makes no difference which one you choose as the end
                        result is the same.

                        Gerald


                        Comment

                        • Jay B. Harlow [MVP - Outlook]

                          #13
                          Re: How to Convert Byte Array to Int32 (Big-endian)

                          Gerald,
                          Thanks, I was looking for NetworkToHostOr der & HostToNetworkOr der, when
                          Charles first asked, I just didn't remember which System.Net class they were
                          on. I was thinking Socket or something else...

                          Jay

                          "Gerald Hernandez" <Cablewizard@sp am_remove@Yahoo .com> wrote in message
                          news:%23NjhRqjA FHA.2792@TK2MSF TNGP15.phx.gbl. ..[color=blue]
                          >
                          > "Charles Law" <blank@nowhere. com> wrote in message
                          > news:ODJHwi6$EH A.3988@TK2MSFTN GP11.phx.gbl...[color=green]
                          >> I thought this had come up before, but I now cannot find it.
                          >>
                          >> I have a byte array, such as
                          >>
                          >> Dim a() As Byte = {1, 2, 3, 4}
                          >>
                          >> I want to convert this to an Int32 = 01020304 (hex).
                          >>
                          >> If I use BitConverter, I get 04030201.
                          >>
                          >> Is there a built-in way to do this, or am I stuck with extracting and
                          >> shifting each byte manually?
                          >>
                          >> TIA
                          >>
                          >> Charles
                          >>[/color]
                          >
                          > You could try something like this:
                          >
                          > Private Function Int32FromByteAr ray(ByVal BA() As Byte, ByVal
                          > SwitchEndian As Boolean) As Integer
                          > Dim outVal As Integer
                          >
                          > outVal = BitConverter.To Int32(BA, 0)
                          > If SwitchEndian = True Then
                          > outVal = System.Net.IPAd dress.NetworkTo HostOrder(outVa l)
                          > End If
                          > Return outVal
                          > End Function
                          >
                          > NetworkToHostOr der - Big To Little Endian
                          > HostToNetworkOr der - Little To Big Endian
                          > Although in this case it makes no difference which one you choose as the
                          > end
                          > result is the same.
                          >
                          > Gerald
                          >
                          >[/color]


                          Comment

                          • Gerald Hernandez

                            #14
                            Re: How to Convert Byte Array to Int32 (Big-endian)

                            Yeah, it is buried pretty deep. It also seems they have some sort of
                            aversion to using the word Endian, so it makes it very difficult to find.

                            Gerald

                            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                            news:e$q48rkAFH A.3836@tk2msftn gp13.phx.gbl...[color=blue]
                            > Gerald,
                            > Thanks, I was looking for NetworkToHostOr der & HostToNetworkOr der, when
                            > Charles first asked, I just didn't remember which System.Net class they[/color]
                            were[color=blue]
                            > on. I was thinking Socket or something else...
                            >
                            > Jay
                            >
                            > "Gerald Hernandez" <Cablewizard@sp am_remove@Yahoo .com> wrote in message
                            > news:%23NjhRqjA FHA.2792@TK2MSF TNGP15.phx.gbl. ..[color=green]
                            > >
                            > > "Charles Law" <blank@nowhere. com> wrote in message
                            > > news:ODJHwi6$EH A.3988@TK2MSFTN GP11.phx.gbl...[color=darkred]
                            > >> I thought this had come up before, but I now cannot find it.
                            > >>
                            > >> I have a byte array, such as
                            > >>
                            > >> Dim a() As Byte = {1, 2, 3, 4}
                            > >>
                            > >> I want to convert this to an Int32 = 01020304 (hex).
                            > >>
                            > >> If I use BitConverter, I get 04030201.
                            > >>
                            > >> Is there a built-in way to do this, or am I stuck with extracting and
                            > >> shifting each byte manually?
                            > >>
                            > >> TIA
                            > >>
                            > >> Charles
                            > >>[/color]
                            > >
                            > > You could try something like this:
                            > >
                            > > Private Function Int32FromByteAr ray(ByVal BA() As Byte, ByVal
                            > > SwitchEndian As Boolean) As Integer
                            > > Dim outVal As Integer
                            > >
                            > > outVal = BitConverter.To Int32(BA, 0)
                            > > If SwitchEndian = True Then
                            > > outVal = System.Net.IPAd dress.NetworkTo HostOrder(outVa l)
                            > > End If
                            > > Return outVal
                            > > End Function
                            > >
                            > > NetworkToHostOr der - Big To Little Endian
                            > > HostToNetworkOr der - Little To Big Endian
                            > > Although in this case it makes no difference which one you choose as the
                            > > end
                            > > result is the same.
                            > >
                            > > Gerald
                            > >
                            > >[/color]
                            >
                            >[/color]


                            Comment

                            • Charles Law

                              #15
                              Re: How to Convert Byte Array to Int32 (Big-endian)

                              Hi Gerald

                              That's just the ticket. Thanks.

                              Charles


                              "Gerald Hernandez" <Cablewizard@sp am_remove@Yahoo .com> wrote in message
                              news:%23NjhRqjA FHA.2792@TK2MSF TNGP15.phx.gbl. ..[color=blue]
                              >
                              > "Charles Law" <blank@nowhere. com> wrote in message
                              > news:ODJHwi6$EH A.3988@TK2MSFTN GP11.phx.gbl...[color=green]
                              >> I thought this had come up before, but I now cannot find it.
                              >>
                              >> I have a byte array, such as
                              >>
                              >> Dim a() As Byte = {1, 2, 3, 4}
                              >>
                              >> I want to convert this to an Int32 = 01020304 (hex).
                              >>
                              >> If I use BitConverter, I get 04030201.
                              >>
                              >> Is there a built-in way to do this, or am I stuck with extracting and
                              >> shifting each byte manually?
                              >>
                              >> TIA
                              >>
                              >> Charles
                              >>[/color]
                              >
                              > You could try something like this:
                              >
                              > Private Function Int32FromByteAr ray(ByVal BA() As Byte, ByVal
                              > SwitchEndian As Boolean) As Integer
                              > Dim outVal As Integer
                              >
                              > outVal = BitConverter.To Int32(BA, 0)
                              > If SwitchEndian = True Then
                              > outVal = System.Net.IPAd dress.NetworkTo HostOrder(outVa l)
                              > End If
                              > Return outVal
                              > End Function
                              >
                              > NetworkToHostOr der - Big To Little Endian
                              > HostToNetworkOr der - Little To Big Endian
                              > Although in this case it makes no difference which one you choose as the
                              > end
                              > result is the same.
                              >
                              > Gerald
                              >
                              >[/color]


                              Comment

                              Working...