12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Converter_we : MonoBehaviour
- {
- public static string BintoSng(string num){
- string sign, exponent, fraction, con_fra;
- int con_exp;
- double fra_up, fra_down;
- if(num.Length==32){
- sign=num.Substring(0,1);
- exponent = num.Substring(1, 8);
- if(exponent!="11111111"){
- fraction = "1" + num.Substring(9);
- con_exp = Convert.ToInt32(exponent, 2) - 127;
- if(con_exp >= 0){
- fra_up = FBinToSng(fraction.Substring(0, con_exp + 1));
- fra_down = DBinToSng(fraction.Substring(con_exp + 1));
- }else{
- fraction = ("1" + num.Substring(9)).PadLeft(24 + Math.Abs(con_exp), '0');
- fra_up = FBinToSng(fraction.Substring(0, 1));
- fra_down = DBinToSng(fraction.Substring(1));
- }
- con_fra = String.Format("{0:###,##0.#}",fra_up + fra_down);
- }else{
- con_fra = "NaN";
- }
- return con_fra;
- }else{
- return "輸入資料有誤";
- }
- }
- private static double DBinToSng(string num){
- int i,s;
- double dceStr=0f;
- s=num.Length;
- //Debug.Log("s=="+s);
- //Debug.Log(num);
- for(i=1;i<=s;i++){
- dceStr += double.Parse(num.Substring(i-1,1)) * Math.Pow(2f,-i);
- }
- //Debug.Log(dceStr);
- return dceStr;
-
- }
- private static double FBinToSng(string num){
- int i,s;
- double dceStr=0f;
- s=num.Length;
- for(i=1;i<=s;i++){
- dceStr += double.Parse(num.Substring(i-1,1)) * Math.Pow(2f,s-i);
- }
- return dceStr;
- }
- public static string Powercon(int num1,int num2,int num3,int num4){
- double ans1 = num1 * Math.Pow(65534,3) * 1e-9;
- double ans2 = num2 * Math.Pow(65534,2) * 1e-9;
- double ans3 = num3 * 65534 * 1e-9;
- double ans4 = num4 * 1e-9;
- double answer = ans1 + ans2 + ans3 + ans4;
- if(answer.ToString().IndexOf("E") != -1){
- int le = answer.ToString().Length;
- string res1 = answer.ToString().Substring(0, 1);
- string res2 = answer.ToString().Substring(2, le - 6);
- string res3 = answer.ToString().Substring(le - 2, 2);
- return "0." + (res1 + res2).PadLeft(Int32.Parse(res3) + res2.Length, '0');
- }else{
- return String.Format("{0:###0.##}",answer);
- }
- }
- }
|