How to Convert Binary Coded Hex Byte Array to Byte

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

    How to Convert Binary Coded Hex Byte Array to Byte

    I thought this was going to be straight forward, given the wealth of
    conversion functions in .NET, but it is proving more convoluted than
    imagined.

    Given the following

    <code>
    Dim ba(1) As Byte
    Dim b As Byte

    ba(0) = &h4
    ba(1) = &h0

    b = foo(ba)
    </code>

    What is foo() such that b contains &h40 ?

    TIA

    Charles
    [I could write an algorithm for this, but there must surely be a succinct
    conversion for it]


  • Herfried K. Wagner [MVP]

    #2
    Re: How to Convert Binary Coded Hex Byte Array to Byte

    * "Charles Law" <blank@nowhere. com> scripsit:[color=blue]
    > I thought this was going to be straight forward, given the wealth of
    > conversion functions in .NET, but it is proving more convoluted than
    > imagined.
    >
    > Given the following
    >
    > <code>
    > Dim ba(1) As Byte
    > Dim b As Byte
    >
    > ba(0) = &h4
    > ba(1) = &h0
    >
    > b = foo(ba)
    > </code>
    >
    > What is foo() such that b contains &h40 ?
    >
    > TIA
    >
    > Charles
    > [I could write an algorithm for this, but there must surely be a succinct
    > conversion for it][/color]

    There can be many functions...

    \\\
    Public Function Foo(ByVal abyt() As Byte) As Byte
    Return &H40
    End Function
    ///

    SCNR

    --
    Herfried K. Wagner [MVP]
    <URL:http://dotnet.mvps.org/>

    Comment

    • Charles Law

      #3
      Re: How to Convert Binary Coded Hex Byte Array to Byte

      Hi Herfried

      As I was making coffee I realised my mistake (in the post). It should read

      <code>
      Dim ba(1) As Byte
      Dim b As Byte

      ba(0) = &h34
      ba(1) = &h30

      b = foo(ba)
      </code>

      Still looking for foo() such that b contains &h40.

      Charles


      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
      news:Ocxca78REH A.624@TK2MSFTNG P11.phx.gbl...[color=blue]
      > * "Charles Law" <blank@nowhere. com> scripsit:[color=green]
      > > I thought this was going to be straight forward, given the wealth of
      > > conversion functions in .NET, but it is proving more convoluted than
      > > imagined.
      > >
      > > Given the following
      > >
      > > <code>
      > > Dim ba(1) As Byte
      > > Dim b As Byte
      > >
      > > ba(0) = &h4
      > > ba(1) = &h0
      > >
      > > b = foo(ba)
      > > </code>
      > >
      > > What is foo() such that b contains &h40 ?
      > >
      > > TIA
      > >
      > > Charles
      > > [I could write an algorithm for this, but there must surely be a[/color][/color]
      succinct[color=blue][color=green]
      > > conversion for it][/color]
      >
      > There can be many functions...
      >
      > \\\
      > Public Function Foo(ByVal abyt() As Byte) As Byte
      > Return &H40
      > End Function
      > ///
      >
      > SCNR
      >
      > --
      > Herfried K. Wagner [MVP]
      > <URL:http://dotnet.mvps.org/>[/color]


      Comment

      • Charles Law

        #4
        Re: How to Convert Binary Coded Hex Byte Array to Byte

        Correction

        Given the following

        <code>
        Dim ba(1) As Byte
        Dim b As Byte

        ba(0) = &h34
        ba(1) = &h30

        b = foo(ba)
        </code>

        What is foo() such that b contains &h40 ?

        TIA

        Charles


        "Charles Law" <blank@nowhere. com> wrote in message
        news:uyq$it8REH A.1644@TK2MSFTN GP09.phx.gbl...[color=blue]
        > I thought this was going to be straight forward, given the wealth of
        > conversion functions in .NET, but it is proving more convoluted than
        > imagined.
        >
        > Given the following
        >
        > <code>
        > Dim ba(1) As Byte
        > Dim b As Byte
        >
        > ba(0) = &h4
        > ba(1) = &h0
        >
        > b = foo(ba)
        > </code>
        >
        > What is foo() such that b contains &h40 ?
        >
        > TIA
        >
        > Charles
        > [I could write an algorithm for this, but there must surely be a succinct
        > conversion for it]
        >
        >[/color]


        Comment

        • Armin Zingler

          #5
          Re: How to Convert Binary Coded Hex Byte Array to Byte

          "Charles Law" <blank@nowhere. com> schrieb[color=blue]
          > I thought this was going to be straight forward, given the wealth
          > of conversion functions in .NET, but it is proving more convoluted
          > than imagined.
          >
          > Given the following
          >
          > <code>
          > Dim ba(1) As Byte
          > Dim b As Byte
          >
          > ba(0) = &h4
          > ba(1) = &h0
          >
          > b = foo(ba)
          > </code>
          >
          > What is foo() such that b contains &h40 ?
          >
          > TIA
          >
          > Charles
          > [I could write an algorithm for this, but there must surely be a
          > succinct conversion for it][/color]


          What if ba(0) or ba(1) > &Hf?

          If both are [0; &HF]:

          b = ba(0) << 4 or ba(1)


          --
          Armin

          How to quote and why:



          Comment

          • Charles Law

            #6
            Re: How to Convert Binary Coded Hex Byte Array to Byte

            Hi Armin

            Sorry for the confusion, but I think my correction is a bit slow coming
            through. I should have written

            ba(0) = &h34
            ba(1) = &h30

            Charles


            "Armin Zingler" <az.nospam@free net.de> wrote in message
            news:40bc7455$0 $24816$9b622d9e @news.freenet.d e...[color=blue]
            > "Charles Law" <blank@nowhere. com> schrieb[color=green]
            > > I thought this was going to be straight forward, given the wealth
            > > of conversion functions in .NET, but it is proving more convoluted
            > > than imagined.
            > >
            > > Given the following
            > >
            > > <code>
            > > Dim ba(1) As Byte
            > > Dim b As Byte
            > >
            > > ba(0) = &h4
            > > ba(1) = &h0
            > >
            > > b = foo(ba)
            > > </code>
            > >
            > > What is foo() such that b contains &h40 ?
            > >
            > > TIA
            > >
            > > Charles
            > > [I could write an algorithm for this, but there must surely be a
            > > succinct conversion for it][/color]
            >
            >
            > What if ba(0) or ba(1) > &Hf?
            >
            > If both are [0; &HF]:
            >
            > b = ba(0) << 4 or ba(1)
            >
            >
            > --
            > Armin
            >
            > How to quote and why:
            > http://www.plig.net/nnq/nquote.html
            > http://www.netmeister.org/news/learn2quote.html
            >[/color]


            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: How to Convert Binary Coded Hex Byte Array to Byte

              * "Charles Law" <blank@nowhere. com> scripsit:[color=blue]
              > Given the following
              >
              > <code>
              > Dim ba(1) As Byte
              > Dim b As Byte
              >
              > ba(0) = &h34
              > ba(1) = &h30
              >
              > b = foo(ba)
              > </code>
              >
              > What is foo() such that b contains &h40 ?[/color]

              There are still thousands of ways to return this result. Do you have
              other (input, outpuut) pairs?

              --
              Herfried K. Wagner [MVP]
              <URL:http://dotnet.mvps.org/>

              Comment

              • Armin Zingler

                #8
                Re: How to Convert Binary Coded Hex Byte Array to Byte

                "Charles Law" <blank@nowhere. com> schrieb[color=blue][color=green][color=darkred]
                > > > Given the following
                > > >
                > > > <code>
                > > > Dim ba(1) As Byte
                > > > Dim b As Byte
                > > >
                > > > ba(0) = &h4
                > > > ba(1) = &h0
                > > >
                > > > b = foo(ba)
                > > > </code>
                > > >
                > > > What is foo() such that b contains &h40 ?[/color]
                > >
                > > What if ba(0) or ba(1) > &Hf?
                > >
                > > If both are [0; &HF]:
                > >
                > > b = ba(0) << 4 or ba(1)[/color]
                >
                > Sorry for the confusion, but I think my correction is a bit slow
                > coming through. I should have written
                >
                > ba(0) = &h34
                > ba(1) = &h30[/color]

                Expected result? &H3430?

                Dim ba(1) As Byte
                Dim s As Short

                ba(0) = &H34
                ba(1) = &H30

                s = CShort(ba(0)) << 8 Or ba(1)


                If you can exchange the byte order, this is also possible:

                s = System.BitConve rter.ToInt16(ba , 0)



                --
                Armin

                How to quote and why:



                Comment

                • Charles Law

                  #9
                  Re: How to Convert Binary Coded Hex Byte Array to Byte

                  Thousands? I only need one ;-)

                  This is currently the only scenario. As a stop-gap, I have

                  <code>
                  Dim enc As New Text.ASCIIEncod ing

                  Return CByte("&H" & enc.GetString(b a))
                  </code>

                  which I imagine you will say is as good as any, but prepending "&H" to the
                  string just seems a bit 'kludgy'.

                  Charles


                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                  news:u6QyDd9REH A.4020@TK2MSFTN GP11.phx.gbl...[color=blue]
                  > * "Charles Law" <blank@nowhere. com> scripsit:[color=green]
                  > > Given the following
                  > >
                  > > <code>
                  > > Dim ba(1) As Byte
                  > > Dim b As Byte
                  > >
                  > > ba(0) = &h34
                  > > ba(1) = &h30
                  > >
                  > > b = foo(ba)
                  > > </code>
                  > >
                  > > What is foo() such that b contains &h40 ?[/color]
                  >
                  > There are still thousands of ways to return this result. Do you have
                  > other (input, outpuut) pairs?
                  >
                  > --
                  > Herfried K. Wagner [MVP]
                  > <URL:http://dotnet.mvps.org/>[/color]


                  Comment

                  • Charles Law

                    #10
                    Re: How to Convert Binary Coded Hex Byte Array to Byte

                    Hi Armin

                    No. Expected result still &H40.

                    Unfortunately, I cannot easily change the byte order as these are sent to me
                    from an external source, as binary coded hex digits.

                    Charles


                    "Armin Zingler" <az.nospam@free net.de> wrote in message
                    news:40bc7db4$0 $24800$9b622d9e @news.freenet.d e...[color=blue]
                    > "Charles Law" <blank@nowhere. com> schrieb[color=green][color=darkred]
                    > > > > Given the following
                    > > > >
                    > > > > <code>
                    > > > > Dim ba(1) As Byte
                    > > > > Dim b As Byte
                    > > > >
                    > > > > ba(0) = &h4
                    > > > > ba(1) = &h0
                    > > > >
                    > > > > b = foo(ba)
                    > > > > </code>
                    > > > >
                    > > > > What is foo() such that b contains &h40 ?
                    > > >
                    > > > What if ba(0) or ba(1) > &Hf?
                    > > >
                    > > > If both are [0; &HF]:
                    > > >
                    > > > b = ba(0) << 4 or ba(1)[/color]
                    > >
                    > > Sorry for the confusion, but I think my correction is a bit slow
                    > > coming through. I should have written
                    > >
                    > > ba(0) = &h34
                    > > ba(1) = &h30[/color]
                    >
                    > Expected result? &H3430?
                    >
                    > Dim ba(1) As Byte
                    > Dim s As Short
                    >
                    > ba(0) = &H34
                    > ba(1) = &H30
                    >
                    > s = CShort(ba(0)) << 8 Or ba(1)
                    >
                    >
                    > If you can exchange the byte order, this is also possible:
                    >
                    > s = System.BitConve rter.ToInt16(ba , 0)
                    >
                    >
                    >
                    > --
                    > Armin
                    >
                    > How to quote and why:
                    > http://www.plig.net/nnq/nquote.html
                    > http://www.netmeister.org/news/learn2quote.html
                    >[/color]


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: How to Convert Binary Coded Hex Byte Array to Byte

                      Charles,
                      Use Armin's code, only anding with &hf first.

                      Public Function Foo(ByVal bytes() As Byte) As Byte
                      Const mask As Byte = &HF
                      Return (bytes(0) And mask) << 4 Or (bytes(1) And mask)
                      End Function

                      Hope this helps
                      Jay

                      "Charles Law" <blank@nowhere. com> wrote in message
                      news:uQ3AvO9REH A.3608@TK2MSFTN GP10.phx.gbl...[color=blue]
                      > Hi Armin
                      >
                      > Sorry for the confusion, but I think my correction is a bit slow coming
                      > through. I should have written
                      >
                      > ba(0) = &h34
                      > ba(1) = &h30
                      >
                      > Charles
                      >
                      >
                      > "Armin Zingler" <az.nospam@free net.de> wrote in message
                      > news:40bc7455$0 $24816$9b622d9e @news.freenet.d e...[color=green]
                      > > "Charles Law" <blank@nowhere. com> schrieb[color=darkred]
                      > > > I thought this was going to be straight forward, given the wealth
                      > > > of conversion functions in .NET, but it is proving more convoluted
                      > > > than imagined.
                      > > >
                      > > > Given the following
                      > > >
                      > > > <code>
                      > > > Dim ba(1) As Byte
                      > > > Dim b As Byte
                      > > >
                      > > > ba(0) = &h4
                      > > > ba(1) = &h0
                      > > >
                      > > > b = foo(ba)
                      > > > </code>
                      > > >
                      > > > What is foo() such that b contains &h40 ?
                      > > >
                      > > > TIA
                      > > >
                      > > > Charles
                      > > > [I could write an algorithm for this, but there must surely be a
                      > > > succinct conversion for it][/color]
                      > >
                      > >
                      > > What if ba(0) or ba(1) > &Hf?
                      > >
                      > > If both are [0; &HF]:
                      > >
                      > > b = ba(0) << 4 or ba(1)
                      > >
                      > >
                      > > --
                      > > Armin
                      > >
                      > > How to quote and why:
                      > > http://www.plig.net/nnq/nquote.html
                      > > http://www.netmeister.org/news/learn2quote.html
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • Charles Law

                        #12
                        Re: How to Convert Binary Coded Hex Byte Array to Byte

                        Hi Jay

                        Thanks. I was trying to avoid byte shifting and masking if possible. I had
                        hoped that there was a higher level solution, using a combination of the
                        Convert class and perhaps Encoding/Decoding. There seem to be so many ways
                        of performing conversions that I thought there must be one to translate
                        binary code hex. For example, what about foo() where

                        Dim s As String
                        Dim b As Byte

                        s = "40"

                        b = foo(s)

                        to give b equal to &H40. Clearly, I can put &H on the beginning of the
                        string and use CByte(), as I put in my response to Herfried, but that just
                        seems low-tech. After all, there is a way to create a hex string from a
                        byte:

                        Convert.ToStrin g(&H40, 16) gives "40"

                        so isn't there something to do the reverse?

                        Charles


                        "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                        news:ejvTUy9REH A.2604@TK2MSFTN GP10.phx.gbl...[color=blue]
                        > Charles,
                        > Use Armin's code, only anding with &hf first.
                        >
                        > Public Function Foo(ByVal bytes() As Byte) As Byte
                        > Const mask As Byte = &HF
                        > Return (bytes(0) And mask) << 4 Or (bytes(1) And mask)
                        > End Function
                        >
                        > Hope this helps
                        > Jay
                        >
                        > "Charles Law" <blank@nowhere. com> wrote in message
                        > news:uQ3AvO9REH A.3608@TK2MSFTN GP10.phx.gbl...[color=green]
                        > > Hi Armin
                        > >
                        > > Sorry for the confusion, but I think my correction is a bit slow coming
                        > > through. I should have written
                        > >
                        > > ba(0) = &h34
                        > > ba(1) = &h30
                        > >
                        > > Charles
                        > >
                        > >
                        > > "Armin Zingler" <az.nospam@free net.de> wrote in message
                        > > news:40bc7455$0 $24816$9b622d9e @news.freenet.d e...[color=darkred]
                        > > > "Charles Law" <blank@nowhere. com> schrieb
                        > > > > I thought this was going to be straight forward, given the wealth
                        > > > > of conversion functions in .NET, but it is proving more convoluted
                        > > > > than imagined.
                        > > > >
                        > > > > Given the following
                        > > > >
                        > > > > <code>
                        > > > > Dim ba(1) As Byte
                        > > > > Dim b As Byte
                        > > > >
                        > > > > ba(0) = &h4
                        > > > > ba(1) = &h0
                        > > > >
                        > > > > b = foo(ba)
                        > > > > </code>
                        > > > >
                        > > > > What is foo() such that b contains &h40 ?
                        > > > >
                        > > > > TIA
                        > > > >
                        > > > > Charles
                        > > > > [I could write an algorithm for this, but there must surely be a
                        > > > > succinct conversion for it]
                        > > >
                        > > >
                        > > > What if ba(0) or ba(1) > &Hf?
                        > > >
                        > > > If both are [0; &HF]:
                        > > >
                        > > > b = ba(0) << 4 or ba(1)
                        > > >
                        > > >
                        > > > --
                        > > > Armin
                        > > >
                        > > > How to quote and why:
                        > > > http://www.plig.net/nnq/nquote.html
                        > > > http://www.netmeister.org/news/learn2quote.html
                        > > >[/color]
                        > >
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        • Jay B. Harlow [MVP - Outlook]

                          #13
                          Re: How to Convert Binary Coded Hex Byte Array to Byte

                          Charles,[color=blue]
                          > Thanks. I was trying to avoid byte shifting and masking if possible. I had[/color]
                          Only via the various methods previously mentioned (BitConverter)
                          [color=blue]
                          > Convert.ToStrin g(&H40, 16) gives "40"
                          > so isn't there something to do the reverse?[/color]

                          You mean?

                          Dim s As String = b.ToString("X")

                          Hope this helps
                          Jay

                          "Charles Law" <blank@nowhere. com> wrote in message
                          news:%23IIGB79R EHA.3988@tk2msf tngp13.phx.gbl. ..[color=blue]
                          > Hi Jay
                          >
                          > Thanks. I was trying to avoid byte shifting and masking if possible. I had
                          > hoped that there was a higher level solution, using a combination of the
                          > Convert class and perhaps Encoding/Decoding. There seem to be so many ways
                          > of performing conversions that I thought there must be one to translate
                          > binary code hex. For example, what about foo() where
                          >
                          > Dim s As String
                          > Dim b As Byte
                          >
                          > s = "40"
                          >
                          > b = foo(s)
                          >
                          > to give b equal to &H40. Clearly, I can put &H on the beginning of the
                          > string and use CByte(), as I put in my response to Herfried, but that just
                          > seems low-tech. After all, there is a way to create a hex string from a
                          > byte:
                          >
                          > Convert.ToStrin g(&H40, 16) gives "40"
                          >
                          > so isn't there something to do the reverse?
                          >
                          > Charles
                          >
                          >
                          > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                          > news:ejvTUy9REH A.2604@TK2MSFTN GP10.phx.gbl...[color=green]
                          > > Charles,
                          > > Use Armin's code, only anding with &hf first.
                          > >
                          > > Public Function Foo(ByVal bytes() As Byte) As Byte
                          > > Const mask As Byte = &HF
                          > > Return (bytes(0) And mask) << 4 Or (bytes(1) And mask)
                          > > End Function
                          > >
                          > > Hope this helps
                          > > Jay
                          > >
                          > > "Charles Law" <blank@nowhere. com> wrote in message
                          > > news:uQ3AvO9REH A.3608@TK2MSFTN GP10.phx.gbl...[color=darkred]
                          > > > Hi Armin
                          > > >
                          > > > Sorry for the confusion, but I think my correction is a bit slow[/color][/color][/color]
                          coming[color=blue][color=green][color=darkred]
                          > > > through. I should have written
                          > > >
                          > > > ba(0) = &h34
                          > > > ba(1) = &h30
                          > > >
                          > > > Charles
                          > > >
                          > > >
                          > > > "Armin Zingler" <az.nospam@free net.de> wrote in message
                          > > > news:40bc7455$0 $24816$9b622d9e @news.freenet.d e...
                          > > > > "Charles Law" <blank@nowhere. com> schrieb
                          > > > > > I thought this was going to be straight forward, given the wealth
                          > > > > > of conversion functions in .NET, but it is proving more convoluted
                          > > > > > than imagined.
                          > > > > >
                          > > > > > Given the following
                          > > > > >
                          > > > > > <code>
                          > > > > > Dim ba(1) As Byte
                          > > > > > Dim b As Byte
                          > > > > >
                          > > > > > ba(0) = &h4
                          > > > > > ba(1) = &h0
                          > > > > >
                          > > > > > b = foo(ba)
                          > > > > > </code>
                          > > > > >
                          > > > > > What is foo() such that b contains &h40 ?
                          > > > > >
                          > > > > > TIA
                          > > > > >
                          > > > > > Charles
                          > > > > > [I could write an algorithm for this, but there must surely be a
                          > > > > > succinct conversion for it]
                          > > > >
                          > > > >
                          > > > > What if ba(0) or ba(1) > &Hf?
                          > > > >
                          > > > > If both are [0; &HF]:
                          > > > >
                          > > > > b = ba(0) << 4 or ba(1)
                          > > > >
                          > > > >
                          > > > > --
                          > > > > Armin
                          > > > >
                          > > > > How to quote and why:
                          > > > > http://www.plig.net/nnq/nquote.html
                          > > > > http://www.netmeister.org/news/learn2quote.html
                          > > > >
                          > > >
                          > > >[/color]
                          > >
                          > >[/color]
                          >
                          >[/color]


                          Comment

                          • Armin Zingler

                            #14
                            Re: How to Convert Binary Coded Hex Byte Array to Byte

                            "Charles Law" <blank@nowhere. com> schrieb[color=blue][color=green][color=darkred]
                            > > >
                            > > > Sorry for the confusion, but I think my correction is a bit
                            > > > slow coming through. I should have written
                            > > >
                            > > > ba(0) = &h34
                            > > > ba(1) = &h30[/color]
                            > >
                            > > Expected result? &H3430?
                            > >
                            > > Dim ba(1) As Byte
                            > > Dim s As Short
                            > >
                            > > ba(0) = &H34
                            > > ba(1) = &H30
                            > >
                            > > s = CShort(ba(0)) << 8 Or ba(1)
                            > >
                            > >
                            > > If you can exchange the byte order, this is also possible:
                            > >
                            > > s = System.BitConve rter.ToInt16(ba , 0)[/color]
                            >
                            > No. Expected result still &H40.[/color]


                            How do you get &H40 from &H34 and &H30 ??


                            Aaah.. now I understand. Both values are character codes. You should have
                            mentioned! :-)

                            dim b as byte

                            b = System.Convert. ToByte(Chr(ba(0 )) & Chr(ba(1)), 16)

                            Or, for performance reasons:

                            Dim c(1) As Char
                            c(0) = Convert.ToChar( ba(0))
                            c(1) = Convert.ToChar( ba(1))
                            b = System.Convert. ToByte(New String(c), 16)

                            You may also consider declaring c as static (static c(1) as char).


                            .....


                            Another (untested!) version:

                            Private Function foo(ByVal b As Byte()) As Byte
                            Static b0, b1 As Byte

                            Select Case b(0)
                            Case Is > 70
                            b0 = b(0) - CByte(87)
                            Case Is > 57
                            b0 = b(0) - CByte(55)
                            Case Else
                            b0 = b(0) - CByte(48)
                            End Select

                            Select Case b(1)
                            Case Is > 70
                            b1 = b(1) - CByte(87)
                            Case Is > 57
                            b1 = b(1) - CByte(55)
                            Case Else
                            b1 = b(1) - CByte(48)
                            End Select


                            Return (b0 << 4) Or b1

                            End Function



                            --
                            Armin

                            How to quote and why:



                            Comment

                            • Herfried K. Wagner [MVP]

                              #15
                              Re: How to Convert Binary Coded Hex Byte Array to Byte

                              * "Charles Law" <blank@nowhere. com> scripsit:[color=blue]
                              > Thousands? I only need one ;-)[/color]

                              Sorry, for some reason, I misread the thread of the topic...

                              --
                              Herfried K. Wagner [MVP]
                              <URL:http://dotnet.mvps.org/>

                              Comment

                              Working...