如何将int值转换为字节值 [英] how to convert int value to byte value

查看:142
本文介绍了如何将int值转换为字节值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是将int值转换为字节值

this is the converting the int value to byte value

推荐答案

这是不可能的.一个字节是0到255.一个int整数要大得多.因此,您可以将int转换为字节流,但不能转换为字节.
It''s not possible. A byte is 0 to 255. An int is a whole lot bigger than that. So, you can convert an int to a stream of bytes, but not to a byte.


使用Convert.ToByte(intValue)会将整数值转换为字节.如果输入的值太大或太小,它将通过OverFlowException.

最好使用Byte.TryParse(...)方法.
Use Convert.ToByte(intValue) which will convert your integer value to byte. If the entered value is too big or too small, it will through OverFlowException.

Better to use the Byte.TryParse(...) method.


int intValue = 2;

字节byteValue = Convert.ToByte(intValue);

这就是您要找的东西吗?

正如下面确实指出的那样,一个字节一直到数字255,然后停止,因为一个字节可以容纳8位

二进制:11111111 = 255
int intValue = 2;

byte byteValue = Convert.ToByte(intValue);

This is what you''re looking for?

As indeed pointed out below a byte goes until the number 255 and then stops because a byte can holds 8 bits

binary: 11111111 = 255


这篇关于如何将int值转换为字节值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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