10进制转换成2、8、16进制の转换器

目前只能实现简单的功能

 1 #include <stdio.h>
 2 #include <limits.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <ctype.h>
 7 int size;
 8 char string[CHAR_BIT * sizeof(int) + 1];
 9 void change_to_16(void)
10 {
11     printf("0x");
12     for (int j = 0, index = 0, count = 0, k = 0; j <size; j++, index++)
13     {
14         if (index == 0) count += (string[j] - ‘0‘) * 8;
15         if (index == 1) count += (string[j] - ‘0‘) * 4;
16         if (index == 2) count += (string[j] - ‘0‘) * 2;
17         if (index == 3)
18         {
19             count += (string[j] - ‘0‘);
20             if (count < 10) printf("%d", count);
21             else
22             {
23                 switch (count)
24                 {
25                 case 10:printf("a"); break;
26                 case 11:printf("b"); break;
27                 case 12:printf("c"); break;
28                 case 13:printf("d"); break;
29                 case 14:printf("e"); break;
30                 case 15:printf("f"); break;
31                 }
32             }
33             index = -1;
34             count = 0;
35         }
36     }
37 }
38 void change_to_8(void)
39 {
40     printf("0");
41     char string8[1000] = { 0 };
42     for (int j = size - 1, index = 0, count = 0, k = 0; j >= 0; j--, index++)
43     {
44         if (index == 0) count += string[j] - ‘0‘;
45         if (index == 1) count += (string[j] - ‘0‘) * 2;
46         if (index == 2)
47         {
48             count += (string[j] - ‘0‘) * 4;
49             string8[k++] = count + ‘0‘;
50             index = -1;
51             count = 0;
52         }
53     }
54     int len = strlen(string8);
55     for (int i = len - 1; i >= 0; i--)
56         printf("%c", string8[i]);
57 }
58 void change_to_2(void)
59 {
60     int j = 0;
61     while (string[j])
62     {
63         putchar(string[j]);
64         if (++j % 4 == 0 && string[j])
65             putchar(‘ ‘);
66     }
67 }
68 int main(void)
69 {
70     int number;
71     char button;
72     printf("choose the mode:\n");
73     printf("A,to binary\nB,to octal\nC,to hexadecimal\n");
74     scanf("%c", &button);
75     button = toupper(button);
76     fflush(stdin);
77     printf("Enter a decimal number:");
78     while (~scanf("%d", &number))
79     {
80         if (fabs(number) <= 15) size = 4;
81         else if (fabs(number) < 256&&fabs(number)>15) size = 8;
82         else if (fabs(number) >= 256 && fabs(number) < 4096) size = 12;
83         else if (fabs(number) >= 4096 && fabs(number) < 65536) size = 16;
84         else size = 20;
85         for (int i = size - 1; i >= 0; i--, number >>= 1)
86             string[i] = (01 & number) + ‘0‘;
87         string[size] = ‘\0‘;
88         switch (button)
89         {
90         case ‘A‘:change_to_2(); break;
91         case ‘B‘:change_to_8(); break;
92         case ‘C‘:change_to_16(); break;
93         }
94         putchar(‘\n‘);
95         printf("Enter a decimal number:");
96     }
97     return 0;
98 }
时间: 2024-12-09 23:39:50

10进制转换成2、8、16进制の转换器的相关文章

JS-011-颜色进制转换(RGB转16进制;16进制转RGB)

在网页开发的时候,经常需要进行颜色设置,因而经常需要遇到进行颜色进制转换的问题,例如:RGB转16进制:16进制转RGB),前几天在测试的时候,发现网站的颜色进制转换某类16进制颜色(例如:#000080,#FA08FA)转换时总是提示颜色非法,看了一下 js 源码,发现其进制转换的方法是错误的.找了一下度娘和谷大爷,最终写了一个颜色转换的小方法,源码如下所示: String.prototype.colorHex2Rgb = function(){ var reg = /^#([0-9a-fA-

十进制转换成十六进制、16进制转2进制

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h> int main() { int i,v; char bs[33]; char b[33]; char hs[9]; char h[9]; char s[4]; char *e; // 十进制整数转二进制串: i=1024; ltoa(i,b,2); sprintf(bs,"%032s&quo

10进制转换成8进制

package cast; import java.util.Collections;import java.util.LinkedList;import java.util.List; public class Cast {    //测试    public static void main(String[] args) {        int a = Cast.castNum(100);        System.out.println(a);            }        

从16进制转换成汉字

/// <summary> /// 从16进制转换成汉字 /// </summary> /// <param name="hex"></param> /// <returns></returns> public static string GetChsFromHex(string hex) { if (hex == null) throw new ArgumentNullException("hex&qu

CAD文件转换成DWF格式文件怎么在转换器中转换?

CAD文件转换成DWF格式文件怎么在转换器中转换?大家都知道DWF文件是一种高度压缩.开放.安全的文件格式,它可以将丰富的设计数据高效的分发给需要查看.评审或者打印这些数据的人看,因为在编辑器中编辑完的图纸都是dwg格式的,所以就需要将CAD图纸进行转换,那CAD文件转换成DWF格式文件怎么在转换器中转换?具体要怎么来进行操作?下面小编就来教教大家,想要了解的朋友也可以一起来看看,希望能够帮助到你们. 第一步:打开电脑,在电脑桌面上任意的打开一个浏览器,在浏览器的搜索框中搜索迅捷CAD转换器,然

汇编语言程序:16进制转换成10进制(三种方法)

1 ;天水浪客(Gouki Jiang) , 16进制to10进制例题 2007.5.18 2 STACK SEGMENT PARA STACK 3 S_AREA DW 100H DUP(?) 4 S_TOP EQU $-S_AREA 5 STACK ENDS 6 7 DATA SEGMENT PARA 8 VALUE DW 0EB9AH ;060314 9 RESULT DB 5 DUP(?),'$' 10 DATA ENDS 11 12 CODE SEGMENT PARA 13 ASSUME

Java中将10进制转换成16进制

import java.util.Scanner; public class Decimal2HexConversion {     public static void main(String[] args){         Scanner input = new Scanner(System.in);         System.out.print("输入一个十进制数: ");         int decimal = input.nextInt();         Sys

SQL SERVER 自定义函数 整数转成指定长度的16进制 转换成指定长度的16进制 不足补0

最近做项目扩展的时候,遇到问题就是将整型转换成指定长度的16进制 刚开始就是直接使用 cast(12 as varbinary(4))但是发现这个不能解决我的问题 所以就上网搜了一下,然后改了改,下面就是函数: 1 Create Function IntToHexLength(@Num int,@HxLength int) 2 returns varchar(16) 3 as 4 begin 5 declare @Mods int,@res varchar(16),@Length int 6 s

Nginx将utf8编码的url解码成\x的16进制格式导致无法匹配静态文件的问题处理

例如请求/touch/article/北京/full.html,到达nginx后变成/ /touch/article/%E5%8C%97%E4%BA%AC/full.html, Nginx静态文件配置: location ~* ^/touch/article/.*\.html$ { expires -1; root /home/htmlfile; charset UTF-8; if ( !-f $request_filename ){ proxy_pass http://client.api.c

0063-二进制转换成十进制

二进制转换成十进制 难度级别:A: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给定一个不超过 10 位的二进制数 n,将其转换成十进制数后输出. 输入 一个符合规范的二进制的数. 输出 一个数,符合题目要求的结果. 输入示例 1000100 输出示例 68 此题考查大家对数学基本知识的了解程度.从二进制转换为十进制的方法为:从右往左数第n位乘2的n次方. 通过while循环(即满足括号里条件是执行操作)进行当n不为零(即未转换完成)时