Gerhard
Gerhard

Reputation: 7061

Checking if an 8 bit character is alphanumeric

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.

Upvotes: 3

Views: 279

Answers (2)

Tohm
Tohm

Reputation: 305

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));
}

Upvotes: 1

György Kőszeg
György Kőszeg

Reputation: 18023

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

Upvotes: 5

Related Questions