Strange ASC Codes

S-Soft 646 Reputation points
2022-10-10T12:54:04.797+00:00

Hello all, I'm not familiar with chars and encodings, but need to filter input strings char by char and just allow few chars to pass out

Convert.ToInt32(InputString.Chars(LoopNo))

I'm confused with some chars ASC codes, like:

133 which is Horizontal ellipsis … but in some conditions I get 8230 instead of 133

146 which is Right single quotation mark ’ but in some conditions I get 8217 instead of 146

Each char has 2 ASC codes?
How to cover them all in my function?

For LoopNo As Integer = 0 To InputString.Length - 1
Select Case Convert.ToInt32(InputString.Chars(LoopNo))
Case 133, 146, etc...
OutString += InputString.Chars(LoopNo)
Case Else
'Disregard
End Select
Next

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2022-10-10T13:43:34.893+00:00

    Hi
    I would do something like this. Lots of room for improvements as I don't really know what you are planning on.

    Public Class Form1  
    	Dim teststring As String = "1  2A" & Chr(133) & "BA C" & Chr(146) & "de fA"  
    	Dim wanted() As Integer = {65, 133, 146}  
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
    		Dim OutString As String = GetChars(teststring)  
    		Stop  
    		' hover over the OutString variable above to see what it holds.  
    		' Here it is "A…A’A"  
    	End Sub  
    	Function GetChars(s As String) As String  
    		Dim newstring As String = Nothing  
    		For Each c As Char In s  
    			If wanted.Contains(Asc(c)) Then newstring &= c  
    		Next  
    		Return newstring  
    	End Function  
    End Class  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 43,246 Reputation points
    2022-10-10T13:13:23.03+00:00

    I guess you mean more ASCII code, not ASC(ending)

    but in some conditions I get 8217 instead of 146

    ASCII can represent max. 255 characters = 1 byte in size. Unicode uses 2 bytes = can have value higher then 255, see https://www.codetable.net/decimal/8217

    0 comments No comments

  2. LesHay 7,126 Reputation points
    2022-10-10T13:20:43.66+00:00

    Hi
    See THIS LINK for some info.

    As for your code. Do you want to add multiple characters to the OutString when it already has that character? e.g. InputString = "ABCDAFGAHIJ" so OutString would contain character 'A' value 3 times. or, if already in OutString would only contain the value for 'A' once?