2 回答
TA貢獻1777條經驗 獲得超10個贊
對于 ASCII 字符(0-127 范圍內的字符),您可以簡單地轉換它們
public const byte dollar = (byte)'?';
除此以外:
public const byte dollar = 0x0024;
所以char列。刪除U+并添加 0x。僅對 0x0000-0x007F 范圍內的字符有效。
請注意,編譯后的代碼沒有區(qū)別:sharplab:
public const byte dollar = (byte)'$';
public const byte dollar2 = 0x0024;
被編譯為:
.field public static literal uint8 dollar = uint8(36)
.field public static literal uint8 dollar2 = uint8(36)
使用 C# 7.0,如果您討厭這個世界并且想要混淆您的代碼,您可以:
public const byte dollar = 0b00100100;
(他們添加了二進制文字,0b是前綴)
TA貢獻1839條經驗 獲得超15個贊
您所指的字符不是 UTF-8 字符。所以它們是單字節(jié)字符。(注意 UTF-8 只對 ASCII 字符集以外的字符使用 2 個字節(jié))
由于上述原因,您可以將它們轉換為:
public const byte dollar = (byte)'$';
如果您需要以字節(jié)為單位的 UTF-8 字符,則應使用:
public static readonly byte[] trademark = new byte[] { 194, 153 };或者,更明確,但對性能來說也是最差的:
public static readonly byte[] trademark = Encoding.UTF8.GetBytes("\u0099");- 2 回答
- 0 關注
- 157 瀏覽
添加回答
舉報
