C#控制台基础 无符号十六进制小数转换为十进制

镇场诗:
———大梦谁觉,水月中建博客。百千磨难,才知世事无常。
———今持佛语,技术无量愿学。愿尽所学,铸一良心博客。
——————————————————————————————————————————

1 code

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6
  7 namespace ConsoleApplication15
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             //这个字典是为了遇到16进制到10进制转换时遇到字母准备的
 14             //如果是字母,那么运行字典转换成十进制数
 15             Dictionary<char, int> tableOf16_10 = new Dictionary<char, int>();
 16             tableOf16_10.Add(‘a‘, 10);
 17             tableOf16_10.Add(‘b‘, 11);
 18             tableOf16_10.Add(‘c‘, 12);
 19             tableOf16_10.Add(‘d‘, 13);
 20             tableOf16_10.Add(‘e‘, 14);
 21             tableOf16_10.Add(‘f‘, 15);
 22
 23
 24
 25             Console.WriteLine("your turn:");
 26             string value = Console.ReadLine();
 27
 28             //分离字符串,以小数点为界限
 29             string[] values = value.Split(new char[] { ‘.‘ }, StringSplitOptions.RemoveEmptyEntries);
 30
 31             //存储小数部分的字符串
 32             string decimalsPart = values[1];
 33             //存储整数部分的字符串
 34             string integerPart = values[0];
 35
 36             //小数部分出来的10进制数字一定是double类型的
 37             double sumOfDecimalsPart = 0.0;
 38
 39             char temp;
 40             for (int i = 0; i < decimalsPart.Length; i++)
 41             {
 42                 //decimalsPart[i]是char类型,所以要加一个tostring
 43                 //在算法设计的初级阶段,不要写出一个有多个变量的长式子,
 44                 //要把式子的每个变量都用单独命名,然后调试的时候可以检测到是哪一个变量出了问题
 45                 //int itemp = Convert.ToInt32(decimalsPart[i].ToString());
 46
 47                 temp = decimalsPart[i];
 48
 49                 //判断decimalsPart[i]是否是字母
 50                 if (char.IsLetter(temp))
 51                 {
 52                     if(char.IsUpper(temp))
 53                     {
 54                         temp =Convert.ToChar(Convert.ToInt32(temp)+32);
 55
 56                     }
 57                     //如果是字母,就用字典来转换
 58                     sumOfDecimalsPart += Convert.ToDouble(tableOf16_10[temp] * Math.Pow(16, -(i + 1)));
 59                 }
 60                 else
 61                 {
 62                     //如果不是字母正常对待
 63                     sumOfDecimalsPart += Convert.ToDouble(Convert.ToInt32(decimalsPart[i].ToString()) * Math.Pow(16, -(i + 1)));
 64                 }
 65
 66             }
 67
 68
 69             int sumOfintegerPart = 0;
 70             for (int i = 0; i < integerPart.Length; i++)
 71             {
 72                 //decimalsPart[i]是char类型,所以要加一个tostring
 73                 //在算法设计的初级阶段,不要写出一个有多个变量的长式子,
 74                 //要把式子的每个变量都用单独命名,然后调试的时候可以检测到是哪一个变量出了问题
 75                 //int itemp = Convert.ToInt32(decimalsPart[i].ToString());
 76                 temp = integerPart[i];
 77                 //判断decimalsPart[i]是否是字母
 78                 if (char.IsLetter(temp))
 79                 {
 80                     if (char.IsUpper(temp))
 81                     {
 82                         temp = Convert.ToChar(Convert.ToInt32(temp) + 32);
 83
 84                     }
 85                     //如果是字母,先转换成小写字母,再用字典来转换
 86                     sumOfintegerPart += Convert.ToInt32(tableOf16_10[char.ToLower(temp)] * Math.Pow(16, i));
 87                 }
 88                 else
 89                 {
 90                     //如果不是字母正常对待
 91                     sumOfintegerPart += Convert.ToInt32(Convert.ToInt32(integerPart[i].ToString()) * Math.Pow(16,i));
 92                 }
 93             }
 94
 95
 96             Console.Write("decimalsPart:");
 97             Console.WriteLine(decimalsPart);
 98
 99             Console.Write("sumOfDecimalsPart:");
100             Console.WriteLine(sumOfDecimalsPart);
101
102             Console.Write("integerPart:");
103             Console.WriteLine(integerPart);
104
105             Console.Write("sumOfintegerPart:");
106             Console.WriteLine(sumOfintegerPart);
107
108             Console.Write("result:");
109
110             //                                 这里实现的是整数部分与小数部分的拼接
111             Console.WriteLine(sumOfintegerPart+sumOfDecimalsPart.ToString().Substring(1));
112             Console.ReadKey();
113         }
114     }
115 }

2 show1

show2

时间: 2024-12-31 21:09:23

C#控制台基础 无符号十六进制小数转换为十进制的相关文章

C#控制台基础 无符号八进制小数转换为十进制

镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 code 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Co

C#Winform基础 无符号二进制数(整数)转换为十进制数

镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 UI 2 code 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using S

C#控制台基础 无符号二进制小数转换为十进制

镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 code 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Co

C#Winform基础 无符号二进制数(整数)转换为十六进制(小大写版本)

镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 UI 2 code 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using S

C#Winform基础 无符号二进制数(整数)转换为八进制数

镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 UI 2 code 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using S

VC中,如何将十六进制字符串转换为十进制数?

原文链接:http://blog.csdn.net/bennyfun79/article/details/6934843 在程序中,我们有时需要将一个十六进制字符串转换为十进制数字.比如: 1 char *ptr="0x11"; 2 int n=0; 3 //我们想让n等于0x11,即17 通常我们在C中,想将一个字符串转换为一整形数字,通常会使用下面的方法: view plaincopy to clipboardprint? 1 char *ptr="123";

将一个十六进制字符串转换为十进制数值的问题

在程序中,我们有时需要将一个十六进制字符串转换为十进制数字.比如:char *ptr="0x11";int n=0;//我们想让n等于0x11,即17 通常我们在C中,想将一个字符串转换为一整形数字,通常会使用下面的方法: char *ptr="123"; int n=0; n=atoi(ptr); printf("%d/n",n); //输出:123 但是atoi库函数只能将十进制字符串转化为int整形,比如下面的例子: #include &l

MATLAB把ISE中产生的1QN格式的十六进制数据转换为十进制数进行分析

数据格式为1位符号位,1位整数位,8位小数位. N=64; fid = fopen('e:\sin.txt','r'); for i = 1 : N; num(i) = fscanf(fid, '%x', 1) %从fid所指的文件以16进制方式读出数据 if(num(i)>512) num(i)=-(num(i)-512)/2^8;%第10位(符号位)是1,转换为负数.8位小数位,除以2^8 else num(i)=num(i)/2^8; end end fclose(fid); plot(n

C#Winform基础 十六进制数转换为八进制数(整数,无符号)

镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 UI 2 code 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using S