В Строке/Кодировке символов C#, каково различие между GetBytes (), GetString () и Преобразовывает ()?

Это непосредственно не отвечает на Ваш вопрос, но в большом количестве случаев, можно добавить runat = "сервер" к регулярному HTML-тэгу для создания ASP.NET, знающего о нем. Это могло бы сделать вещи легче для разработчика, если Вы хотите быть в состоянии динамично изменить страницу, но все еще позволить разработчику работать над ним.

<div id="myDiv" runat="server"></div>
<span id="mySpan" runat="server"></span>

Редактирование:

Одна вещь, которую я забыл упоминать (как указано steve_c) состоит в том, что добавление runat = "сервер" изменит идентификатор для тега, который может быть чем-то вроде боли. Вы являетесь довольно неудачливыми, если Вы используете идентификатор в своем CSS, но в Вашем JavaScript можно добавить что-то как < % = myDiv. % ClientID> для получения идентификатора, который был сгенерирован.Net.

6
задан 3 revs 15 September 2009 в 12:15
поделиться

1 ответ

After a much troubled and confusing morning, we found the answer to this problem.

The key point we were missing, which was making this very confusing, was that string types are always encoded in 16-bit (2-byte) Unicode. This means that when we do a GetString() on the bytes, they are automatically being re-encoded into Unicode behind the scenes and we are no better off than we were in the first place.

When we started to get character errors, and double byte data at the other end, we knew something was wrong but at a glance of the code we had, we couldn't see anything wrong. After learning what we have explained above, we realised that we needed to send the byte array if we wanted to preserve the encoding. Luckily, MicrosoftFunc() had an overload which was able to take a byte array instead of a string. This meant that we could convert the unicode string to an encoding of our choice and then send it off exactly as we expect it. The code changed to:

// Convert from a Unicode string to an array of bytes (encoded as UTF8).
byte[] source = Encoding.UTF8.GetBytes(unicode); 

// Send the encoded byte array directly! Do not send as a Unicode string.
MicrosoftFunc(source);

Summary:

So in conclusion, from the above we can see that:

  • GetBytes() amongst other things, does an Encoding.Convert() from Unicode (because strings are always Unicode) and the specified encoding the function was called from and returns an array of encoded bytes.
  • GetString() amongst other things, does an Encoding.Convert() from the specified encoding the function was called from to Unicode (because strings are always Unicode) and returns it as a string object.
  • Convert() actually converts a byte array of one encoding to another byte array of another encoding. Obviously strings cannot be used (because strings are always Unicode).
11
ответ дан 9 December 2019 в 22:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: