473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

ASCII to Byte

If I receive a message from the .net sockets class, it's a byte array. The
original message is an ASCII string,like "70,70,70,70,70,0,0,0,0". The
commas here represent individual bytes. The 70's are the ASCII code for
"F", which I need to interpret as HEX F, or 15. Is this possible without a
long, complicated lookup table?

Thanks,

Nik Martin
Nov 20 '05 #1
8 14323
Cor
Hi Nik,
Always nice games.
This is the shortest I came up with
\\\.
Dim s As String = "70,70,70,70,70,0,0,0,0"
Dim a As String() = Split(s, ",")
Dim i As Integer
Dim b As String
For i = 0 To a.Length - 1
b = String.Concat(b, Hex(a(i)))
Next
////
I hope it works for you
Cor
Nov 20 '05 #2
Hi Nik,

I was going to reply to your ASCII Terminal query, but you've reposted.
Such impatience!! ;-)

|| Is this possible without a long, complicated lookup table?

Yes, lol. You use a short, uncomplicated table!

I presume that you're getting hex values, so '0'-'9', 'A'-'F' are the only
possibilities. Rather than me explaining, try the code below. :-)

If 'a' to 'f' are possibilities, add them to the table.

Regards,
Fergus

<code>
Public Module AsciiToBitArrayConversion
Private CharToNibble() As Byte

Private Sub InitAsciiToBitArray
ReDim CharToNibble(256)
'These values are reversed because
'New BitArray (Byte()) reverses the bytes.
CharToNibble (Asc("0")) = &h0
CharToNibble (Asc("1")) = &h8
CharToNibble (Asc("2")) = &h4
CharToNibble (Asc("3")) = &hC
CharToNibble (Asc("4")) = &h2
CharToNibble (Asc("5")) = &hA
CharToNibble (Asc("6")) = &h6
CharToNibble (Asc("7")) = &hE
CharToNibble (Asc("8")) = &h1
CharToNibble (Asc("9")) = &h9
CharToNibble (Asc("A")) = &h5
CharToNibble (Asc("B")) = &hD
CharToNibble (Asc("C")) = &h3
CharToNibble (Asc("D")) = &hB
CharToNibble (Asc("E")) = &h7
CharToNibble (Asc("F")) = &hF
End Sub

Public Sub TestAtoB
Dim S As String = "80FF7BC30123456789ABCDEF"
Dim Bits As BitArray = AsciiToBitArray (S)
Dim I As Integer
Dim sBytes As String = ""
For I = 0 To Bits.Length - 1
If I / 4 = I \ 4 Then sBytes = sBytes & " "
If I / 8 = I \ 8 Then sBytes = sBytes & " "
sBytes = sBytes & DirectCast _
(IIF (Bits(I) = True, "1", "0"), String)
Next
Console.WriteLine (S & vbCrLf & sBytes)
S = S & ", " & SBytes
End Sub

Public Function AsciiToBitArray (S As String) As BitArray
If CharToNibble Is Nothing Then
InitAsciiToBitArray
End If

Dim aBytes (1 + S.Length \ 2) As Byte
Dim I As Integer
For I = 1 To S.Length Step 2
Dim Hi As Integer = CharToNibble (Asc (Mid (S, I + 1, 1)))
Dim Lo As Integer = CharToNibble (Asc (Mid (S, I + 0, 1)))
aBytes (I \ 2) = CByte (Hi * 16 + Lo)
Next

Return New BitArray (aBytes)
End Function
End Module
</code>

Nov 20 '05 #3
Hi Nik,

I was going to reply to your ASCII Terminal query, but you've reposted.
Such impatience!! ;-)

|| Is this possible without a long, complicated lookup table?

Yes, lol. You use a short, uncomplicated table!

I presume that you're getting hex values, so '0'-'9', 'A'-'F' are the only
possibilities. Rather than me explaining, try the code below. :-)

If 'a' to 'f' are possibilities, add them to the table.

Regards,
Fergus

<code>
Public Class AsciiHexString
Private Shared CharToNibble() As Byte

Private Shared Sub Initialise
ReDim CharToNibble(256)
'These values are reversed because
'New BitArray (Byte()) reverses the bytes.
CharToNibble (Asc("0")) = &h0
CharToNibble (Asc("1")) = &h8
CharToNibble (Asc("2")) = &h4
CharToNibble (Asc("3")) = &hC
CharToNibble (Asc("4")) = &h2
CharToNibble (Asc("5")) = &hA
CharToNibble (Asc("6")) = &h6
CharToNibble (Asc("7")) = &hE
CharToNibble (Asc("8")) = &h1
CharToNibble (Asc("9")) = &h9
CharToNibble (Asc("A")) = &h5
CharToNibble (Asc("B")) = &hD
CharToNibble (Asc("C")) = &h3
CharToNibble (Asc("D")) = &hB
CharToNibble (Asc("E")) = &h7
CharToNibble (Asc("F")) = &hF
End Sub

Public Shared Function ToBitArray (S As String) As BitArray
If CharToNibble Is Nothing Then
Initialise
End If

Dim aBytes (1 + S.Length \ 2) As Byte
Dim I As Integer
For I = 1 To S.Length Step 2
Dim Hi As Integer = CharToNibble (Asc (Mid (S, I + 1, 1)))
Dim Lo As Integer = CharToNibble (Asc (Mid (S, I + 0, 1)))
aBytes (I \ 2) = CByte (Hi * 16 + Lo)
Next

Return New BitArray (aBytes)
End Function
End Class

Public Sub TestAtoB
Dim S As String = "80FF7BC30123456789ABCDEF"
Dim Bits As BitArray = AsciiHexString.ToBitArray (S)
Dim I As Integer
Dim sBytes As String = ""
For I = 0 To Bits.Length - 1
If I / 4 = I \ 4 Then sBytes = sBytes & " "
If I \ 8 = I / 8 Then sBytes = sBytes & " "
sBytes = sBytes & DirectCast _
(IIF (Bits(I), "1", "0"), String)
Next
Console.WriteLine (S & vbCrLf & sBytes)
S = S & ", " & sBytes
End Sub
</code>


Nov 20 '05 #4
Hello,

"Cor" <no*@non.com> schrieb:
Dim s As String = "70,70,70,70,70,0,0,0,0"
Dim a As String() = Split(s, ",")
Dim i As Integer
Dim b As String
For i = 0 To a.Length - 1
b = String.Concat(b, Hex(a(i)))


Notice that it's better to use a 'StringBuilder' to compose the string.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #5
Cor
Hi Herfried,
Notice that it's better to use a 'StringBuilder' to compose the string.

I agree with you
I tested it and the string builder seems really to overcome the problem from
every time allocating memory.
So therefore is beneath a more preferable approach.
\\\\
Dim s As String = "70,70,70,70,70,0,0,0,0"
Dim a As String() = Split(s, ",")
Dim i As Integer
Dim b As New System.Text.StringBuilder
For i = 0 To a.Length - 1
b.Append(Hex(a(i)))
Next
Dim y As String = b.ToString
///
;-)
Cor
Nov 20 '05 #6
I wasn't sure the ASCII terminal post made sense.

The problem is, I'm getting the ASCII values for the Hex character I really
need.

So what the terminal sends is a string like "FFFFF12EF0000333"

But using the socket class, I have to allocate a byte buffer, so what I get
in the Buffer is:
dim b() as byte
stream.read(b,0,16)

B(0)=70
B(1)=70
B(2)=70
B(3)=70
B(4)=70
B(5)=49
B(6)=50
etc.
etc
Which are the decimal values for the strings F, 1, 2, etc.

What I need aout of these strings is the fact that F (str(70)) is hex F (16)


"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl...
Hi Nik,

I was going to reply to your ASCII Terminal query, but you've reposted. Such impatience!! ;-)

|| Is this possible without a long, complicated lookup table?

Yes, lol. You use a short, uncomplicated table!

I presume that you're getting hex values, so '0'-'9', 'A'-'F' are the only possibilities. Rather than me explaining, try the code below. :-)

If 'a' to 'f' are possibilities, add them to the table.

Regards,
Fergus

<code>
Public Class AsciiHexString
Private Shared CharToNibble() As Byte

Private Shared Sub Initialise
ReDim CharToNibble(256)
'These values are reversed because
'New BitArray (Byte()) reverses the bytes.
CharToNibble (Asc("0")) = &h0
CharToNibble (Asc("1")) = &h8
CharToNibble (Asc("2")) = &h4
CharToNibble (Asc("3")) = &hC
CharToNibble (Asc("4")) = &h2
CharToNibble (Asc("5")) = &hA
CharToNibble (Asc("6")) = &h6
CharToNibble (Asc("7")) = &hE
CharToNibble (Asc("8")) = &h1
CharToNibble (Asc("9")) = &h9
CharToNibble (Asc("A")) = &h5
CharToNibble (Asc("B")) = &hD
CharToNibble (Asc("C")) = &h3
CharToNibble (Asc("D")) = &hB
CharToNibble (Asc("E")) = &h7
CharToNibble (Asc("F")) = &hF
End Sub

Public Shared Function ToBitArray (S As String) As BitArray
If CharToNibble Is Nothing Then
Initialise
End If

Dim aBytes (1 + S.Length \ 2) As Byte
Dim I As Integer
For I = 1 To S.Length Step 2
Dim Hi As Integer = CharToNibble (Asc (Mid (S, I + 1, 1)))
Dim Lo As Integer = CharToNibble (Asc (Mid (S, I + 0, 1)))
aBytes (I \ 2) = CByte (Hi * 16 + Lo)
Next

Return New BitArray (aBytes)
End Function
End Class

Public Sub TestAtoB
Dim S As String = "80FF7BC30123456789ABCDEF"
Dim Bits As BitArray = AsciiHexString.ToBitArray (S)
Dim I As Integer
Dim sBytes As String = ""
For I = 0 To Bits.Length - 1
If I / 4 = I \ 4 Then sBytes = sBytes & " "
If I \ 8 = I / 8 Then sBytes = sBytes & " "
sBytes = sBytes & DirectCast _
(IIF (Bits(I), "1", "0"), String)
Next
Console.WriteLine (S & vbCrLf & sBytes)
S = S & ", " & sBytes
End Sub
</code>

Nov 20 '05 #7
Nik Martin wrote:
I wasn't sure the ASCII terminal post made sense.

The problem is, I'm getting the ASCII values for the Hex character I really
need.

So what the terminal sends is a string like "FFFFF12EF0000333"

But using the socket class, I have to allocate a byte buffer, so what I get
in the Buffer is:
dim b() as byte
stream.read(b,0,16)

B(0)=70
B(1)=70
B(2)=70
B(3)=70
B(4)=70
B(5)=49
B(6)=50
etc.
etc
Which are the decimal values for the strings F, 1, 2, etc.

What I need aout of these strings is the fact that F (str(70)) is hex F (16)


System.Text.Encoding.ASCII.GetString(). Then just loop through the
caracters and build your byte array.

In fact, you could then use Byte.Parse with the
NumberStyles.AllowHexSpecifier to convert to the actuall byte...

Something like:

Dim returnBuffer As String = _
System.Text.Encoding.ASCII.GetString(socketBuffer)

Dim bytes(returnBuffer.Length - 1) As Byte

For i As Integer = 0 To bytes.GetUpperBound(0)
bytes(i} = Byte.Parse(returnBuffer.SubString(i, 1), _
Globalization.NumberStyles.AllowHexSpecifier)
Next

Dim bits As New BitArray(bytes)
HTH,
Tom Shelton

Nov 20 '05 #8
Hi Nik,

I gave you a <complete solution> in my post - didn't you try it? :-(

Take the class as given. Drop it into a file and call Sub TestAtoB. You
will see what it does. Easy.

It takes an input string. So what. Modify it for your byte array and of
you go.

Regards,
Fergus
Nov 20 '05 #9

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

Similar topics

6
by: clintp | last post by:
I have a byte array that contains 8-bit ascii characters. I'm not particular about the codepage used to display them, but I have to preserve the position in the string with something and be able...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
1
by: Scott Duckworth | last post by:
Can anyone provide a quick code snippit to open a text file and tell if it's ASCII or Unicode? Thanks
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
7
by: Jeffrey Spoon | last post by:
Hello, I'm a bit stuck trying to convert a text file which contains extended ASCII text and changing the ASCII values so they become readable. I do this by subtracting 127 from the ASCII value....
5
by: kuukelekuu | last post by:
I need to convert ascii chars to hex chars. I searched the internet, found hex to ascii, but nowhere is there a ascii to hex method created by anyone. Can anyone help me with that? Chears
19
by: Thomas W | last post by:
I'm getting really annoyed with python in regards to unicode/ascii-encoding problems. The string below is the encoding of the norwegian word "fødselsdag". I stored the string as "fødselsdag"...
4
by: Oleg Parashchenko | last post by:
Hello, I'm working on an unicode-aware application. I like to use "print" to debug programs, but in this case it was nightmare. The most popular result of "print" was: UnicodeDecodeError:...
9
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.