简体   繁体   English

在C#中从char int直接转换为char

[英]Direct cast from char int to char in c#

I have a following c++ code which needs to convert to c#. 我有以下c ++代码,需要转换为c#。

char tempMask;

tempMask = 0xff;

I am not too sure how to convert this, could some one please let me know what is the use of below line in c++ 我不太确定如何将其转换,请让我知道c ++下面一行的用途是什么

tempMask = 0xff;

thanks 谢谢

It initializes tempMask with a byte containing value 0xFF 它使用包含值0xFF的字节初始化tempMask

In C#, you can write 在C#中,您可以编写

tempMask = (char)0xff;

or 要么

tempMask = Convert.ToChar(0xff);

It's just a simple initialisation of a variable tempMask in a hexadecimal system. 这只是十六进制系统中变量tempMask的简单初始化。 http://en.wikipedia.org/wiki/Hexadecimal OxFF = 15*16^0+ 15*16^1 = 255 . http://en.wikipedia.org/wiki/十六进制OxFF = 15 * 16 ^ 0 + 15 * 16 ^ 1 = 255。 In C# you cannot init char with int value without explicit conversion but you can write : char tempMask = (char)0xFF; 在C#中,如果不进行显式转换,则不能使用int值初始化char,但是可以编写:char tempMask = (char)0xFF; , or if you really want a 8-bit integer - try byte tempMask = 0xFF; ,或者如果您确实想要8位整数-尝试byte tempMask = 0xFF;

A signed char in C/C++ is not a 'char' in C#, it's an 'sbyte' - no cast required: C / C ++中的带符号字符不是C#中的“字符”,而是“字节”-无需强制转换:

sbyte tempMask;

tempMask = 0xff;

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

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