繁体   English   中英

如何在C#中将unicode转换为单字节?

[英]How do I convert from unicode to single byte in C#?

如何在C#中将unicode转换为单字节?

这不起作用:

int level =1;
string argument;
// and then argument is assigned

if (argument[2] == Convert.ToChar(level))
{
    // does not work
}

和这个:

char test1 = argument[2];
char test2 = Convert.ToChar(level);

产生时髦的结果。 test1可以是: test2 49 '1'test2将是1 ''

如何在C#中将unicode转换为单字节?

这个问题没有道理,示例代码只会使情况变得更糟。

Unicode是从字符到代码点的映射。 代码点的编号从0x0到0x10FFFF,其值远远大于单个字节中可以存储的值。

并且示例代码具有一个int ,一个string和一个char 任何地方都没有byte

您到底想做什么?

使用UnicodeEncoding.GetBytes()

UnicodeEncoding unicode = new UnicodeEncoding();
Byte[] encodedBytes = unicode.GetBytes(unicodeString);

charstring在.NET中始终是Unicode。 您无法按照尝试的方式进行操作。

实际上,您要完成什么?

如果要测试int 级别是否与char 参数[2]相匹配,请使用

  if (argument[2] == Convert.ToChar(level + (int)'0'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM