【类型转换C#】IEEE 754 Float Convertion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IEEE754
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "IEEE 754 float Convertion JellyBeanci (c)";
            bool state = true;
            do
            {
                try
                {
                    ConsoleColor color = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("IEEE 754 Float Convertion.");
                    Console.ForegroundColor = color;
                    Console.Write("Enter the Number-> ");
                    float number = GetUserInput();
                    Console.WriteLine(FloatToBinary(number));
                    color = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Real number: {0}\n", number);
                    Console.ForegroundColor = color;
                }
                catch
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Illegal Input!\n");
                    state = false;
                }
            }
            while (state);
            Console.Write("Press any button to Exit");
            Console.ReadKey();
        }
        static float GetUserInput()
        {
            float value = -1;
            try
            {
                string rawInput = Console.ReadLine();
                if (rawInput.Contains("."))
                {
                    // if has '.' we need to convert ','
                    var sep = rawInput.Split('.');
                    // split from '.'
                    if (sep.Length == 2)
                    {
                        // must has 2 cause (foo) . (bar) more than 1 point doesn't make any sense.
                        // Whole.Fraction
                        float whole = Int32.Parse(sep[0]);

                        // number / 10^number's length 
                        float fraction = (float)(Int32.Parse(sep[1]) / Math.Pow(10, sep[1].Length));
                        if(whole < 0)
                        {
                            //if negative must subtract
                            value = whole - fraction;
                        }
                        else
                        {
                            //if positive must add
                            value = whole + fraction;
                        }
                    }
                    else
                    {
                        // throw new IllegalArgumentException
                        throw new ArgumentException("Illegal Argument");
                    }
                }
                else
                {
                    return float.Parse(rawInput);
                }
            }
            catch
            {
                throw new ArgumentException("Illegal Argument");
            }
            return value;
        }
        static string FloatToBinary(float f)
        {
            StringBuilder sb = new StringBuilder();
            Byte[] ba = BitConverter.GetBytes(f);
            foreach (Byte b in ba)
            {
                for (int i = 0; i < 8; i++)
                {
                    sb.Insert(0, ((b >> i) & 1) == 1 ? "1" : "0");
                }
            }
            string s = sb.ToString();
            string r = s.Substring(0, 1) + " " + s.Substring(1, 8) + " " + s.Substring(9); //sign exponent mantissa
            return r;
        }
    }
}

/*A JELLYBEANCI SOFTWARE*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值