What is the char represented by keycode (15) c#?

1.3k views Asked by At

I have a string which is pulled from a SQL database using EF, through a view. string sample is "mystring" with length of 8. When I compare this string to "mystring" I am getting "false".

I've converted the string to (byte) array:

("mystring").Select(m=>(byte)m).ToArray();

I see I have an extra char with keycode "15" at the end of the array.

I've looked anywhere what is the keycode '15' and I didn't see it in the keycode tables.

My questions are:

  1. What is the char represented by "15" when converted to byte?

  2. Why is it added to the string? (it is a string that my users can change with admin screen)

Added: it doesn't happen every time, only with (for now) one string.

Edit: I copy paste part of that string to Immediate window, to check for Length and I get Length = 2

This is the string:

"a‏".Length 
2

The GetBytes in UF8 of this string is:

{byte[4]}
[0]: 97
[1]: 226
[2]: 128
[3]: 143

The int value of the 2nd char (which does not exist in my eyes) is:

var intvalue = (int)test[1];
8207

Update: Now when I do inspect element on that "a" string i copy pasted before I see "a&rlm" --> what is it and how I get rid of it?

3

There are 3 answers

2
Chris On BEST ANSWER

Looking at the string you have in your question in the HTML source it is written as a‏. A quick google shows that rlm is the left to right mark which is unicode U+200F (http://en.wikipedia.org/wiki/Right-to-left_mark).

Combine this with other people's observation that casting to byte leaves only the final byte explains why you are getting 0F = 15 as your byte number.

Where this is coming from is something for you to investigate but I would imagine it comes from the original user input (ie the user is inputting text right to left).

3
D Stanley On

Your character is likely not the ASCII character with a code 15, but is the Unicode character with a value of XX0F. When you cast a unicode character to byte, you lose the "upper" byte since unicode characters are two bytes.

I don't know how that applies to your "mystring" comparison, but I suspect you're looking for something that ends with one character and getting something that ends with another. I'm not an expert in cultures, so perhaps a culture-sensitive search is appropriate?

Note that any unicode character ending in 0F would result in a vlaue of 15. Here are the first 15:

010f ď 
020f ȏ 
030f ̏ 
040f Џ 
050f ԏ 
060f ؏ 
070f ܏ 
080f ࠏ 
090f ए 
0a0f ਏ 
0b0f ଏ 
0c0f ఏ 
0d0f ഏ 
0e0f ฏ 
0f0f ༏ 
4
nvoigt On

A char in .NET is not a byte. Your conversion from string to byte array is flawed.

You need to use GetBytes to get the bytes in an encoding of your choice.