如何在C ++/CLI中将Int类型转换为字节

如何在C ++/CLI中将Int类型转换为字节

问题描述:

大家好,我再次提出了一个新手问题.好吧,我需要将整数INT转换为字节类型,例如0x00到0xFF.我真的不知道该怎么做,也找不到信息.如果您对此有任何了解,请帮助我.

顺便说一句,我是否需要添加一个特殊的标头来转换

Hello everybody, I come up with a newbie question again. Well I need to convert integers INT to Byte types like 0x00 to 0xFF. I really dont know how to do this and wcan''t find the info as well. If any of you know about this please help me.

Btw do I need to add an special header for converting

如果您的意思是要显示整数作为一系列十六进制字节,那么您只需要使用%x%X 格式规范 [ ^ ] //msdn.microsoft.com/en-us/library/wc7014hz.aspx> printf() [
If you mean you wish to display an integer as a series of hex bytes, then you just need to use the %x or %X format specification[^] in a printf()[^] statement.

[edit]
That''s fine then you just use a cast such as
byte b = (byte)integerValue;


但是,对于大于255的整数值,您需要这样分割整数:


However for integer values greater than 255, you need to split the integer thus:

for (int i = 3; i >= 0; ++i)
{
    byte b = (integervalue >> 8 * i) & 0xFF;
    send(b);
}


[/edit]


[/edit]