472,960 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 software developers and data experts.

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]
Nov 20 '05 #1
25 7146
* "Charles Law" <bl***@nowhere.com> 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/>
Nov 20 '05 #2
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]" <hi***************@gmx.at> wrote in message
news:Oc*************@TK2MSFTNGP11.phx.gbl...
* "Charles Law" <bl***@nowhere.com> 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/>

Nov 20 '05 #3
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" <bl***@nowhere.com> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
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]

Nov 20 '05 #4
"Charles Law" <bl***@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

Nov 20 '05 #5
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*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Charles Law" <bl***@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

Nov 20 '05 #6
* "Charles Law" <bl***@nowhere.com> 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/>
Nov 20 '05 #7
"Charles Law" <bl***@nowhere.com> 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

Nov 20 '05 #8
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]" <hi***************@gmx.at> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...
* "Charles Law" <bl***@nowhere.com> 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/>

Nov 20 '05 #9
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*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Charles Law" <bl***@nowhere.com> 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

Nov 20 '05 #10
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" <bl***@nowhere.com> wrote in message
news:uQ**************@TK2MSFTNGP10.phx.gbl...
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*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Charles Law" <bl***@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


Nov 20 '05 #11
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:uQ**************@TK2MSFTNGP10.phx.gbl...
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*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Charles Law" <bl***@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



Nov 20 '05 #12
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" <bl***@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:uQ**************@TK2MSFTNGP10.phx.gbl...
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*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
> "Charles Law" <bl***@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
>



Nov 20 '05 #13
"Charles Law" <bl***@nowhere.com> schrieb

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)


No. Expected result still &H40.

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:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #14
* "Charles Law" <bl***@nowhere.com> scripsit:
Thousands? I only need one ;-)


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #15
Armin,
How do you get &H40 from &H34 and &H30 ??

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

I was thinking zoned decimal which is the AS/400 mainframe (read EBCDIC)
equivalent...

Hopefully Charles has enough ideas to find one that fits what he needs.

Jay
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de... "Charles Law" <bl***@nowhere.com> schrieb
>
> 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)


No. Expected result still &H40.

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:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #16
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:ej**************@TK2MSFTNGP10.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:uQ**************@TK2MSFTNGP10.phx.gbl...
> 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*******@freenet.de> wrote in message
> news:40***********************@news.freenet.de...
> > "Charles Law" <bl***@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
> >
>
>



Nov 20 '05 #17
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.

Charles
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Charles Law" <bl***@nowhere.com> schrieb
>
> 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)


No. Expected result still &H40.

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:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #18
It's actually a programmable power supply, that returns its status as a 2
byte hex string, and "40" (bit 6 set) means that the output has been
switched off from the front panel.

I realise that probably doesn't help, but I thought you might be interested
to know ;-)

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Armin,
How do you get &H40 from &H34 and &H30 ??

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


Aaah! indeed...

I was thinking zoned decimal which is the AS/400 mainframe (read EBCDIC)
equivalent...

Hopefully Charles has enough ideas to find one that fits what he needs.

Jay
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Charles Law" <bl***@nowhere.com> schrieb
> >
> > 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)

No. Expected result still &H40.

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:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


Nov 20 '05 #19
Charles,
If "bit 6" has meaning I would convert the 2 byte hex string, to an Enum.
Then define an Enum to be what the different bit positions are:

<Flags()> _
Public Enum Status
Off = &h40
End Enum

I would include FlagsAttribute if you can get more then one status at a
time, if you can only have a single status at a time, I would not include
the FlagsAttribute.

I would define the enum such that BitConverter converted the bits
correctly...

Hope this helps
Jay

"Charles Law" <bl***@nowhere.com> wrote in message
news:eK*************@TK2MSFTNGP11.phx.gbl...
It's actually a programmable power supply, that returns its status as a 2
byte hex string, and "40" (bit 6 set) means that the output has been
switched off from the front panel.

I realise that probably doesn't help, but I thought you might be interested to know ;-)

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Armin,
How do you get &H40 from &H34 and &H30 ??

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


Aaah! indeed...

I was thinking zoned decimal which is the AS/400 mainframe (read EBCDIC)
equivalent...

Hopefully Charles has enough ideas to find one that fits what he needs.

Jay
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Charles Law" <bl***@nowhere.com> schrieb
> > >
> > > 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)
>
> No. Expected result still &H40.
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:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html



Nov 20 '05 #20
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:ej**************@TK2MSFTNGP10.phx.gbl...
> 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" <bl***@nowhere.com> wrote in message
> news:uQ**************@TK2MSFTNGP10.phx.gbl...
> > 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*******@freenet.de> wrote in message
> > news:40***********************@news.freenet.de...
> > > "Charles Law" <bl***@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
> > >
> >
> >
>
>



Nov 20 '05 #21
"Charles Law" <bl***@nowhere.com> schrieb
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


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
Nov 20 '05 #22
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eU*************@TK2MSFTNGP11.phx.gbl...
Charles,
If "bit 6" has meaning I would convert the 2 byte hex string, to an Enum.
Then define an Enum to be what the different bit positions are:

<Flags()> _
Public Enum Status
Off = &h40
End Enum

I would include FlagsAttribute if you can get more then one status at a
time, if you can only have a single status at a time, I would not include
the FlagsAttribute.

I would define the enum such that BitConverter converted the bits
correctly...

Hope this helps
Jay

"Charles Law" <bl***@nowhere.com> wrote in message
news:eK*************@TK2MSFTNGP11.phx.gbl...
It's actually a programmable power supply, that returns its status as a 2 byte hex string, and "40" (bit 6 set) means that the output has been
switched off from the front panel.

I realise that probably doesn't help, but I thought you might be

interested
to know ;-)

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Armin,
> How do you get &H40 from &H34 and &H30 ??
>
> Aaah.. now I understand. Both values are character codes. You should

have
> mentioned! :-)

Aaah! indeed...

I was thinking zoned decimal which is the AS/400 mainframe (read EBCDIC) equivalent...

Hopefully Charles has enough ideas to find one that fits what he needs.
Jay
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
> "Charles Law" <bl***@nowhere.com> schrieb
> > > >
> > > > 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)
> >
> > No. Expected result still &H40.
>
>
> 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:
> http://www.plig.net/nnq/nquote.html
> http://www.netmeister.org/news/learn2quote.html
>



Nov 20 '05 #23
In the absence of a built in one-liner, I might do that ;-)

Charles
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40*********************@news.freenet.de...
"Charles Law" <bl***@nowhere.com> schrieb
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


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

Nov 20 '05 #24
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Os**************@TK2MSFTNGP09.phx.gbl... 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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2******************@tk2msftngp13.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> 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
>
>
> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
> news:ej**************@TK2MSFTNGP10.phx.gbl...
> > 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" <bl***@nowhere.com> wrote in message
> > news:uQ**************@TK2MSFTNGP10.phx.gbl...
> > > 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*******@freenet.de> wrote in message
> > > news:40***********************@news.freenet.de...
> > > > "Charles Law" <bl***@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
> > > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #25
Charles,
Yes ToInt32...

Jay

"Charles Law" <bl***@nowhere.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Os**************@TK2MSFTNGP09.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 [MVP - Outlook]" <Ja************@msn.com> wrote in
message news:%2******************@tk2msftngp13.phx.gbl...
> 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" <bl***@nowhere.com> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > 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
> >
> >
> > "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message
> > news:ej**************@TK2MSFTNGP10.phx.gbl...
> > > 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" <bl***@nowhere.com> wrote in message
> > > news:uQ**************@TK2MSFTNGP10.phx.gbl...
> > > > 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*******@freenet.de> wrote in message
> > > > news:40***********************@news.freenet.de...
> > > > > "Charles Law" <bl***@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
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim H | last post by:
I am getting binary data form a database binary(8). I am copying it to a byte array byte. I need to covert this to a number, ulong, so I can write it to a string in hex format. Any idea on how...
1
by: War Eagle | last post by:
I have int SomeNumber (which is less than 256) which I want to convert to 1 byte array so I might send it over TCP. Can someone help me out with writing this line of code? TIA, War Eagle
1
by: Angel Filev | last post by:
Hi everyone, I am trying to store a file as a binary array in an "image" field in SQL Server 2000 database. It works OK except for the ".PDF" files, which I believe get corrupted in the process...
3
by: Peted | last post by:
In the default state ie somemethod(Byte array); is the byte array passed by method or by reference ? or do you have to use the ref keyword to be able to mod the contents of the orig array...
10
by: Chunekit Pong | last post by:
I have a BYTE array - BYTE const* pbBinary I would like to know how many bytes in that byte array but if I do - sizeof(* pbBinary); - then I got 1 but if I do - sizeof( pbBinary); - then I...
18
by: MrVS | last post by:
Hi, I have a C++ CLR class method that takes System::Byte *b as parameter argument. I want the CSharp caller pass a byte * to this function. But in the CSharp prorgram, I only managed to create a...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.