# How to Convert Binary Coded Hex Byte Array to Byte

Charles Law
Guest
Posts: n/a

 1st Jun 2004
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]
Guest
Posts: n/a

 1st Jun 2004
* "Charles Law" <(E-Mail Removed)> scripsit:
> 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]

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/>

Charles Law
Guest
Posts: n/a

 1st Jun 2004
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-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * "Charles Law" <(E-Mail Removed)> scripsit:
> > 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]

>
> 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/>

Charles Law
Guest
Posts: n/a

 1st Jun 2004
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" <(E-Mail Removed)> wrote in message
news:uyq\$(E-Mail Removed)...
> 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]
>
>

Armin Zingler
Guest
Posts: n/a

 1st Jun 2004
"Charles Law" <(E-Mail Removed)> 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

Charles Law
Guest
Posts: n/a

 1st Jun 2004
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" <(E-Mail Removed)> wrote in message
news:40bc7455\$0\$24816\$(E-Mail Removed)...
> "Charles Law" <(E-Mail Removed)> 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
>

Herfried K. Wagner [MVP]
Guest
Posts: n/a

 1st Jun 2004
* "Charles Law" <(E-Mail Removed)> scripsit:
> 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 ?

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/>

Armin Zingler
Guest
Posts: n/a

 1st Jun 2004
"Charles Law" <(E-Mail Removed)> schrieb
> > > 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)

>
> 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

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.BitConverter.ToInt16(ba, 0)

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Charles Law
Guest
Posts: n/a

 1st Jun 2004
Thousands? I only need one ;-)

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

<code>
Dim enc As New Text.ASCIIEncoding

Return CByte("&H" & enc.GetString(ba))
</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-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * "Charles Law" <(E-Mail Removed)> scripsit:
> > 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 ?

>
> 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/>

Charles Law
Guest
Posts: n/a

 1st Jun 2004
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" <(E-Mail Removed)> wrote in message
news:40bc7db4\$0\$24800\$(E-Mail Removed)...
> "Charles Law" <(E-Mail Removed)> schrieb
> > > > 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)

> >
> > 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

>
> 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.BitConverter.ToInt16(ba, 0)
>
>
>
> --
> Armin
>
> How to quote and why:
> http://www.plig.net/nnq/nquote.html
> http://www.netmeister.org/news/learn2quote.html
>

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Russell Mangel Microsoft Dot NET Framework 2 2nd Feb 2005 07:01 PM BMaxwell Microsoft Dot NET Framework 7 28th Oct 2004 11:08 PM Jochen Stuempfig Microsoft C# .NET 2 29th Jul 2004 07:04 PM Rich Microsoft Access Queries 1 22nd Sep 2003 10:34 PM Filippo Microsoft C# .NET 2 1st Aug 2003 07:07 PM

Features