0

So. I'm taking ASCII values (numbers) and turning them into ASCII characters (letters and symbols). I've managed to come up with something but when using For Each...Next, it's separating each individual digit. For instance, if I enter 82 (which corresponds to R), I would get whatever corresponds to 8 and whatever corresponds to 2. Is there anyone to indicate that if there are no spaces in between the digits, then it's one single number? Or just overall a way for it to read the numbers, and not the individual digits.

Here is a bit of the code. Should I not be using the Char data type? As I'm assuming that's what's making it convert 1 digit at a time. Though when I tried any other data types, I get some error regarding it not being a collection type.

 Dim rtb As String rtb = Rich.Text1 For Each num As Char In rtb 'Conversion stuff goes here Next num 

4 Answers 4

1

Lets analyse what is happening here:

For Each num As Char In rtb 

Since rtb is a String, and String implements IEnumerable<Char>, the above is essentially you saying - for each character in the string - which is not what you want.

You need to first split the string into groups that each represents a number - for example:

Dim numbers As String() = rtb.Split(" ") 

Then, you could iterate over the list and convert each of the strings (these are still strings) to a actual number, which you will need to convert to a character:

For Each num As String In numbers ' num can be "82", for instance Next num 
Sign up to request clarification or add additional context in comments.

Comments

0

Do something like this:

Dim rtb As String = Nothing rtb = "82 40 30" For Each num As var In rtb.Split(" "C) Next 

Of course if you would like to split numbers using other char just replace " "C with what you need

Comments

0

You’re not iterating over numbers, you’re iterating over individual characters. You didn’t tell us how the numbers are separated in the input but for the sake of discussion let’s assume that you are using spaces. We can use String.Split to get individual numbers from the stream:

Dim rtb As String = "82 23 47 123" Dim Ascii = System.Text.Encoding.ASCII For each numToken As String In rtb.Split(" "c) Dim num As Byte If Not Byte.TryParse(numToken, num) ' Signal error Exit For End If Dim buffer As Char() = Ascii.GetChars(New Byte() { num }) Dim result As Char = buffer(0) ' Do something with `result` Next 

One important bit: I’ve used the System.Text.Encoding class to do the mapping between ASCII codes and characters. This may seem unnecessarily complex but do want to do this. Encodings unfortunately are complex and it’s counter-productive to pretend otherwise and use homegrown ad-hoc conversions. In particular, using Chr is not appropriate since that doesn’t actually handle ASCII, it handles Unicode code points (with some restrictions) which are similar, but not the same. Using Chr in the above code would be misleading and thus a surefire way to introduce bugs further down the road. It’s important to document your assumptions, and if your assumption is that the input consists of ASCII, then the conversion code should reflect that.

Comments

0

Use three digits for each character. Most lower-case letters are three digits anyway.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.