暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Converter_we.cs 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class Converter_we : MonoBehaviour
  6. {
  7. public static string BintoSng(string num){
  8. string sign, exponent, fraction, con_fra;
  9. int con_exp;
  10. double fra_up, fra_down;
  11. if(num.Length==32){
  12. sign=num.Substring(0,1);
  13. exponent = num.Substring(1, 8);
  14. if(exponent!="11111111"){
  15. fraction = "1" + num.Substring(9);
  16. con_exp = Convert.ToInt32(exponent, 2) - 127;
  17. if(con_exp >= 0){
  18. fra_up = FBinToSng(fraction.Substring(0, con_exp + 1));
  19. fra_down = DBinToSng(fraction.Substring(con_exp + 1));
  20. }else{
  21. fraction = ("1" + num.Substring(9)).PadLeft(24 + Math.Abs(con_exp), '0');
  22. fra_up = FBinToSng(fraction.Substring(0, 1));
  23. fra_down = DBinToSng(fraction.Substring(1));
  24. }
  25. con_fra = String.Format("{0:###,##0.#}",fra_up + fra_down);
  26. }else{
  27. con_fra = "NaN";
  28. }
  29. return con_fra;
  30. }else{
  31. return "輸入資料有誤";
  32. }
  33. }
  34. private static double DBinToSng(string num){
  35. int i,s;
  36. double dceStr=0f;
  37. s=num.Length;
  38. //Debug.Log("s=="+s);
  39. //Debug.Log(num);
  40. for(i=1;i<=s;i++){
  41. dceStr += double.Parse(num.Substring(i-1,1)) * Math.Pow(2f,-i);
  42. }
  43. //Debug.Log(dceStr);
  44. return dceStr;
  45. }
  46. private static double FBinToSng(string num){
  47. int i,s;
  48. double dceStr=0f;
  49. s=num.Length;
  50. for(i=1;i<=s;i++){
  51. dceStr += double.Parse(num.Substring(i-1,1)) * Math.Pow(2f,s-i);
  52. }
  53. return dceStr;
  54. }
  55. public static string Powercon(int num1,int num2,int num3,int num4){
  56. double ans1 = num1 * Math.Pow(65534,3) * 1e-9;
  57. double ans2 = num2 * Math.Pow(65534,2) * 1e-9;
  58. double ans3 = num3 * 65534 * 1e-9;
  59. double ans4 = num4 * 1e-9;
  60. double answer = ans1 + ans2 + ans3 + ans4;
  61. if(answer.ToString().IndexOf("E") != -1){
  62. int le = answer.ToString().Length;
  63. string res1 = answer.ToString().Substring(0, 1);
  64. string res2 = answer.ToString().Substring(2, le - 6);
  65. string res3 = answer.ToString().Substring(le - 2, 2);
  66. return "0." + (res1 + res2).PadLeft(Int32.Parse(res3) + res2.Length, '0');
  67. }else{
  68. return String.Format("{0:###0.##}",answer);
  69. }
  70. }
  71. }