Search code examples
c#.net-standard

Why do Strings.Asc('š') and Strings.AscW('š') return different values?


Strings.Asc('š') = 154
Strings.AscW('š') = 353

I'm porting an old solution to netstandard from netframework. Strings.Asc doesn't existing in the visual basic package, the only option being Strings.AscW. On the surface they appear to be a functionally the same, however they return different solutions

Why? I'd like the same result (154) from Strings.AscW, is there a similar solution that will work on .netstandard?


Solution

  • I’m guessing you’ll have to go the long route via an explicit encoding, and constructing a char array, since there does not seem to be a method for decoding individual chars.

    int result = Encoding.GetEncoding(1252).GetBytes(new char[] {'š'})[0];