# C# 整型数和浮点型数的进制转换

1.十进制转二进制

        /// <summary>
/// 十进制转二进制
/// </summary>
public class Convert10To2
{
/// <summary>
/// 将十进制的16位整型数转换为16位二进制字符串
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static bool TryTo2(short i, out string v)
{
try
{
return true;
}
catch (Exception)
{
v = default;
return false;
}
}

/// <summary>
/// 将十进制的32位整型数转换为32位二进制字符串
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static bool TryTo2(int i, out string v)
{
try
{
return true;
}
catch (Exception)
{
v = default;
return false;
}
}

/// <summary>
/// 将十进制的64位整型数转换为64位二进制字符串
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static bool TryTo2(long i, out string v)
{
try
{
return true;
}
catch (Exception)
{
v = default;
return false;
}
}

/// <summary>
/// 将十进制的32位单精度浮点数转换为32位二进制字符串
/// </summary>
/// <param name="i"></param>
/// <param name="v"></param>
/// <returns></returns>
public static bool TryTo2(float i, out string v)
{
try
{
float j = i;
i = Math.Abs(i);
int i_left = Convert.ToInt32(Math.Floor(i));//获取整数部分
float i_right = i - i_left;//获取小数部分
TryTo2(i_left, out string left);//整数部分转换为二进制
string right = "";
while (i_right != 1)//小数部分转换为二进制
{
float temp = i_right * 2;
right += temp >= 1 ? "1" : "0";
if (temp == 1 || temp == 0)
{
break;
}
i_right = temp > 1 ? temp - 1 : temp;
}
string S = j > 0 ? "0" : "1";//获取符号位的二进制
if (!left.Contains("1"))
{
int e = 127 - (right.IndexOf('1') + 1);//获取指数位的十进制
TryTo2(e, out string E);//获取指数位的二进制
string M = right.Substring(right.IndexOf('1') + 1);
if (M.Length > 23)
{
M = M.Substring(0, 23);
}
else if (M.Length < 23)
{
}
v = S + E.Substring(E.Length - 8) + M;
}
else
{
int e = left.Length - left.IndexOf('1') - 1 + 127;//获取指数位的十进制
TryTo2(e, out string E);//获取指数位的二进制
var z = (left.IndexOf('1') + 1);//获取最高正数位
string M = ((z >= left.Length ? "" : left.Substring(z)) + right);//获取尾数
if (M.Length > 23)
{
M = M.Substring(0, 23);
}
else if (M.Length < 23)
{
}
v = S + E.Substring(E.Length - 8) + M;
}
return true;
}
catch (Exception)
{
v = default;
return false;
}
}
}

2.二进制转十进制

        /// <summary>
/// 二进制转十进制
/// </summary>
public class Convert2To10
{
/// <summary>
/// 将32位二进制字符串转换为十进制的单精度浮点数
/// </summary>
/// <param name="i"></param>
/// <param name="v"></param>
/// <returns></returns>
public static bool TryTo10(string i, out float v)
{
try
{
var s = i.Substring(0, 1);//获取符号位字符串
var e = i.Substring(1, 8);//获取指数位字符串
var m = i.Substring(9);//获取尾数字符串
TryTo10(s, out int S);//把符号转换成十进制
TryTo10(e, out int E);//获取指数转换成十进制
var m_left = \$"1{m.Substring(0, E - 127)}";//获取整数部分二进制字符串
var m_right = m.Substring(E - 127);//获取小数部分二进制字符串
TryTo10(m_left, out int left);//把整数部分转换为十进制
var right = 0f;
for (int index = 0; index < m_right.Length; index++)//把小数部分转换为十进制
{
var temp_value = Convert.ToSingle(m_right[index].ToString());
var temp_index = Convert.ToSingle(Math.Pow(2, -(index + 1)));
right += (temp_value * temp_index);
}
v = left + right;//合并整数部分和小数部分
v = S == 1 ? -v : v;//添加符号
return true;
}
catch
{
v = default;
return false;
}
}

/// <summary>
/// 将32位二进制字符串转换为十进制的32位整型数
/// </summary>
/// <param name="i"></param>
/// <param name="v"></param>
/// <returns></returns>
public static bool TryTo10(string i, out int v)
{
try
{
v = Convert.ToInt32(i, 2);
return true;
}
catch
{
v = default;
return false;
}
}
}

posted @ 2019-06-20 16:29  鸑鷟虽孤  阅读(2497)  评论(1编辑  收藏  举报