3

This C# code would return an unexpected result:

char x = Convert.ToChar(0xff); Console.WriteLine( Char.IsLetterOrDigit(x)); 

It prints True were I was hoping for a False. I assume this is because IsLetterOrDigit is expecting a Unicode character as input versus the extended ascii value I convert from.

How could I make this work? I am reading a continuous binary string from a serial port where problematic characters needs to be removed for reporting.

3
  • stackoverflow.com/questions/5657467/… Commented Sep 8, 2021 at 11:40
  • 1
    As a side note, you could throw a lookup table at this, create an array of 255 booleans and use the value you put into toChar right now as the index. i don't know how much performance is a factor here, if you're processing huge gobs of data it might shave off some processing time in the long run. Commented Sep 8, 2021 at 11:44
  • 2
    0xff is the unicode code-point ÿ, which absolutely is a letter, according the the unicode specification; so: why are you expecting it not to be? what code-point were you expecting? note that "extended ascii" is ambiguous; you need to talk about a specific code-page or encoding for that to make sense Commented Sep 8, 2021 at 11:50

2 Answers 2

5

Char always represents a Unicode UTF-16 character.

You need to specify which 8 bit code page you use. For example, the OEM US 437 Encoding has a non-letter/digit character at code point #255:

int codePage = 437; var encoding = Encoding.GetEncoding(codePage); char x = encoding.GetChars(new byte[] { 0xFF })[0]; Console.WriteLine(Char.IsLetterOrDigit(x)); // False 
Sign up to request clarification or add additional context in comments.

1 Comment

1
ASCIIEncoding ascii = new ASCIIEncoding(); char x = ascii.GetString( new Byte[] { 0xff })[0]; Console.WriteLine(Char.IsLetterOrDigit(x)); 

With Rune manipulation

ASCIIEncoding ascii = new ASCIIEncoding(); var x = ascii.GetString(new Byte[] { 0xff }); foreach(var r in x.EnumerateRunes()) { Console.WriteLine(Rune.IsLetterOrDigit(r)); } 

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.