How to Convert Binary Coded Hex Byte Array to Byte

  • Thread starter Thread starter Charles Law
  • Start date Start date
Charles,
Doh! misread the direction you were converting, my sample is the "easier"
way to do what you showed.

The inverse would be:

Convert.ToInt43("40", 16) gives &H40

Which is from a string to an Integer in base 16.

Hope this helps
Jay

Charles Law said:
Hi Jay

I don't think I do. In my example I was trying to show a byte being
converted to its hex string representation (w/o the &H). So the reverse I
was looking for was a hex string being converted to its byte equivalent. As
I say, I can put &H on the front and use CByte(), but that is cheating
really, and not an exact reversal as it requires some tampering with the
string to achieve the effect.

Charles


Jay B. Harlow said:
Charles,
Thanks. I was trying to avoid byte shifting and masking if possible. I
had
Only via the various methods previously mentioned (BitConverter)
Convert.ToString(&H40, 16) gives "40"
so isn't there something to do the reverse?

You mean?

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

Hope this helps
Jay

Charles Law said:
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.ToString(&H40, 16) gives "40"

so isn't there something to do the reverse?

Charles


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

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


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 said:
Hmm. You're going to think I am hard to please, but it doesn't quite
do it for me yet.

I prefer the latter, but it involves processing each element
individually, which troubles me.

I posted the following in response to Herfried

<code>
Dim enc As New Text.ASCIIEncoding

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

which is more on the lines of what I was hoping for, but I am still
unhappy with the prepending of &H. If I could find a way w/o having
to do that then I think I would say I had taken it as far as I
could.

If you'd find a one-line solution it would be a call to a procedure
containing a loop - I guess. Why don't /you/ write the function, so you're
the author of the one-line-function-call-solution? ;-)


--
Armin

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

In fact I do something similar. Although I have shown an assignment to a
byte, this is actually a writeonly byte property of a structure, that uses a
BitVector32 to access the individual bits.

Charles
 
Aha! That does it.

I have changed it to

Convert.ToByte("40", 16)
Convert.ToInt43("40", 16) gives &H40

but it's the same idea (I presume you meant ToInt32?).

You know, that calling syntax is number 19 of 19 overrides, and I admit to
having got bored after reaching about 15 or 16 in the intellisense.

Anyway, I have my answer. Thanks very much.

And thanks to Armin and Herfried if you're listening.

Cheers.

Charles


Jay B. Harlow said:
Charles,
Doh! misread the direction you were converting, my sample is the "easier"
way to do what you showed.

The inverse would be:

Convert.ToInt43("40", 16) gives &H40

Which is from a string to an Integer in base 16.

Hope this helps
Jay

Charles Law said:
Hi Jay

I don't think I do. In my example I was trying to show a byte being
converted to its hex string representation (w/o the &H). So the reverse I
was looking for was a hex string being converted to its byte equivalent. As
I say, I can put &H on the front and use CByte(), but that is cheating
really, and not an exact reversal as it requires some tampering with the
string to achieve the effect.

Charles


I
had I
had many
ways that
just
from
a
byte:

Convert.ToString(&H40, 16) gives "40"

so isn't there something to do the reverse?

Charles


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

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


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,
Yes ToInt32...

Jay

Charles Law said:
Aha! That does it.

I have changed it to

Convert.ToByte("40", 16)
Convert.ToInt43("40", 16) gives &H40

but it's the same idea (I presume you meant ToInt32?).

You know, that calling syntax is number 19 of 19 overrides, and I admit to
having got bored after reaching about 15 or 16 in the intellisense.

Anyway, I have my answer. Thanks very much.

And thanks to Armin and Herfried if you're listening.

Cheers.

Charles


Jay B. Harlow said:
Charles,
Doh! misread the direction you were converting, my sample is the "easier"
way to do what you showed.

The inverse would be:

Convert.ToInt43("40", 16) gives &H40

Which is from a string to an Integer in base 16.

Hope this helps
Jay
reverse
I equivalent.
As
possible.
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.ToString(&H40, 16) gives "40"

so isn't there something to do the reverse?

Charles


message
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

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


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