来自链接 https://bbs.csdn.net/topics/380229242,https://blog.csdn.net/kjlrzzyffmx/article/details/49053931#commentsedit
1.浮点数转换成字节数组
private void button1_Click(object sender, EventArgs e)
{
float l_value = 10.1f;
l_value = Convert.ToSingle(l_value);
byte[] l_bytes = BitConverter.GetBytes(l_value);
for (int i = 0; i < l_bytes.Length; i++)
{
Console.WriteLine("{0}", l_bytes[i]);
}
}
2.字节数组转换成浮点数
private void button2_Click(object sender, EventArgs e)
{
float l_value;
byte[] l_bytes = new byte[4];
l_bytes[0] = 0x9A;
l_bytes[1] = 0x99;
l_bytes[2] = 0x21;
l_bytes[3] = 0x41;
l_value = BitConverter.ToSingle(l_bytes, 0);
Console.WriteLine("{0:0.000}", l_value);
}
3.数值转换成2进制字符串
private void button3_Click(object sender, EventArgs e)
{
byte l_value = 3;
string l_bin = Convert.ToString(l_value, 2).PadLeft(8, '0');
Console.WriteLine("{0}", l_bin);
}
4.取余与整除
private void button4_Click(object sender, EventArgs e)
{
Int16 l_value = 5;
Int16 l_value1 = 0;
Int16 l_value2 = 0;
//求余数
l_value1 = (Int16)(l_value % 2);
//整除运算,除数与被除数为整数.
l_value2 = (Int16)(l_value / 2);
Console.WriteLine("{0}", l_value1);
Console.WriteLine("{0}", l_value2);
}
5.进制互转(2进制,10进制,16进制),来自链接 https://blog.csdn.net/kuui_chiu/article/details/80579940
int d = 10;
//十进制转二进制字符串
Console.WriteLine(Convert.ToString(d,2));
//输出: 1010
//十进制转十六进制字符串
Console.WriteLine(Convert.ToString(d,16));
//输出: a
//二进制字符串转十进制数
string bin = "1010";
Console.WriteLine(Convert.ToInt32(bin,2));
//输出: 10
//二进制字符串转十六进制数
string bin = "1010";
Console.WriteLine(string.Format("{0:x}",Convert.ToInt32(bin,2));
//输出: a
//十六进制转二进制字符串
Console.WriteLine(Convert.ToString(0xa,2));
//输出: 1010
//MessageBox.Show(Convert.ToInt32(txtDaiZhuan.Text, 16).ToString()); //16进制转十进制
//十六进制转十进制数
Console.WriteLine(Convert.ToString(0xa,10));
//输出: 10
6.位操作符
位逻辑与运算符 &
位逻辑或运算符 |
位逻辑非运算符 ~
位逻辑异或运算符 ^
扫描二维码关注公众号,回复:
6017087 查看本文章
![](/qrcode.jpg)
位左移运算符 <<
位右移运算符 >>