Home > Mobile >  Is there a way to prevent VB from using character alt codes?
Is there a way to prevent VB from using character alt codes?

Time:10-17

I've been messing around with ASCII in Visual Basic (on .NET) for a school project, and recently discovered something that I don't know how to get around. Converting an integer to ASCII using Asc() and then copying it to another script and converting to an integer using Chr() will return the wrong characters in some cases. For example.

Dim i As String = Chr(29) 
Console.WriteLine(i)         ' Returns ↔

Dim i2 As Integer = Asc("↔") 
Console.WriteLine(i2)        ' Returns 63

Currently, I think this could be due to a couple things:

  • Some ASCII codes cannot be displayed (29 is GS, which I don't believe can be shown) and so reverts to using 29's alt code which is ↔.
  • The Asc() function may resort to other character encoding forms where ↔ is 63?
  • I have not used correct methods for doing this task - very possible.

Could someone explain to me why this occurs and/or show me a way of preventing this problem from happening? This seems like a pretty annoying issue for people who use VB and character encoding if it is a genuine problem, however I would be surprised by this as I haven't been using character encoding for long and have probably missed something.

Thanks in advance!

CodePudding user response:

The "↔" you typed at the line

Dim i2 As Integer = Asc("↔") 

is not the same as the result of

Dim i As String = Chr(29)

Because your source code is Unicode encoded, it's not ASCII

CodePudding user response:

You aren't converting the output that you get from Chr using Asc if you did then you'd get the expected result. This:

Dim i = 29
Dim ch = Chr(i)

Console.WriteLine(ch)

i = Asc(ch)

Console.WriteLine(i)

outputs this:

↔
29

What your code is doing is not what you describe.

  • Related