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 { v = Convert.ToString(i, 2).PadLeft(16, '0'); 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 { v = Convert.ToString(i, 2).PadLeft(32, '0'); 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 { v = Convert.ToString(i, 2).PadLeft(64, '0'); 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) { M = M.PadRight(23, '0'); } 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) { M = M.PadRight(23, '0'); } 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; } } }