如何在C ++/CLI中将Int类型转换为字节 [英] How to Convert Int type to Byte in c++/CLI

查看:134
本文介绍了如何在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]


这篇关于如何在C ++/CLI中将Int类型转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆