C语言程序:将十六进制转换为八进制
我们得到一个以字符串形式表示的十六进制数;任务是将其转换为八进制。要将十六进制数转换为八进制,我们需要:
- 找到十六进制数的二进制等价形式。
- 将二进制数转换为八进制。
什么是十六进制数
十六进制数是以16为基数的数字,数字范围从0到9,从10开始,数字表示为A表示10,B表示11,C表示12,D表示13,E表示14,F表示15。
要将十六进制数转换为二进制数,每个数字都转换为其4位的二进制等价形式,然后将这些数字组合起来形成一个对应的二进制数。
什么是八进制数
计算机中的八进制数以8为基数表示,范围为0到7,八进制数由三个二进制数或三个二进制位组成。
我们需要做什么
例如,我们有一个十六进制数1A6,这意味着1、10和6。现在,对于十六进制到八进制的转换,我们首先需要找到十六进制数的二进制等价形式,即:
所以,1A6的二进制表示为:0001 1010 0110
现在,在找到十六进制数的二进制表示后,下一步任务是找到二进制数的八进制表示。
在此之前,我们将二进制数分组为三个一组。分组后,我们将得到000 110 100 110
其八进制表示为:
所以,十六进制数1A6的八进制表示为:646
示例
Input: 1A6 Output: Octal Value = 646 Explanation: Input: 1AA Output: 652
我们将使用的方法来解决给定的问题:
- 获取输入并将其存储为字符串。
- 按照以下方法将十六进制数或表达式转换为二进制:
- 检查所有16种十六进制情况,并添加其相应的二进制表示。
- 返回结果。
- 将二进制数转换为八进制数,请按照以下步骤操作:
- 通过比较二进制数到八进制的所有可能情况,取3位。
- 设置八进制的值 = (val * place) + octal;
- 将二进制数除以1000
- place *= 10
- 返回结果。
算法
Start Step 1-> In function long long int hexa_binary(char hex[]) Declare variables binary, place Declare and initialize i = 0, rem, val Initialize t n = strlen(hex) Initialize binary = 0ll and place = 0ll Loop For i = 0 and hex[i] != '\0' and i++ { binary = binary * place; switch (hex[i]) { case '0': binary += 0 case '1': binary += 1 case '2': binary += 10 case '3': binary += 11 case '4': binary += 100 case '5': binary += 101 case '6': binary += 110 case '7': binary += 111 case '8': binary += 1000 case '9': binary += 1001 case 'a': case 'A': binary += 1010 case 'b': case 'B': binary += 1011 case 'c': case 'C': binary += 1100 case 'd': case 'D': binary += 1101; break; case 'e': case 'E': binary += 1110; break; case 'f': case 'F': binary += 1111; break; default: printf("Invalid hexadecimal input."); } place = 10000; } return binary; } long long int binary_oct(long long binary) { long long int octal, place; int i = 0, rem, val; octal = 0ll; place = 0ll; place = 1; while (binary > 0) { rem = binary % 1000; switch (rem) { case 0: val = 0; break; case 1: val = 1; break; case 10: val = 2; break; case 11: val = 3; break; case 100: val = 4; break; case 101: val = 5; break; case 110: val = 6; break; case 111: val = 7; break; } octal = (val * place) + octal; binary /= 1000; place *= 10; } return octal; } long long int hexa_oct(char hex[]) { long long int octal, binary; // convert HexaDecimal to Binary binary = hexa_binary(hex); // convert Binary to Octal octal = binary_oct(binary); return octal; } int main() { char hex[20] = "1a99"; printf("Octal Value = %lld", hexa_oct(hex)); return 0; }
示例
#include <stdio.h> #include <string.h> #include <math.h> //To convert hex to binary first long long int hexa_binary(char hex[]) { long long int binary, place; int i = 0, rem, val; int n = strlen(hex); binary = 0ll; place = 0ll; for (i = 0; hex[i] != '\0'; i++) { binary = binary * place; switch (hex[i]) { case '0': binary += 0; break; case '1': binary += 1; break; case '2': binary += 10; break; case '3': binary += 11; break; case '4': binary += 100; break; case '5': binary += 101; break; case '6': binary += 110; break; case '7': binary += 111; break; case '8': binary += 1000; break; case '9': binary += 1001; break; case 'a': case 'A': binary += 1010; break; case 'b': case 'B': binary += 1011; break; case 'c': case 'C': binary += 1100; break; case 'd': case 'D': binary += 1101; break; case 'e': case 'E': binary += 1110; break; case 'f': case 'F': binary += 1111; break; default: printf("Invalid hexadecimal input."); } place = 10000; } return binary; } //To convert binary to octal long long int binary_oct(long long binary) { long long int octal, place; int i = 0, rem, val; octal = 0ll; place = 0ll; place = 1; // giving all binary numbers for octal conversion while (binary > 0) { rem = binary % 1000; switch (rem) { case 0: val = 0; break; case 1: val = 1; break; case 10: val = 2; break; case 11: val = 3; break; case 100: val = 4; break; case 101: val = 5; break; case 110: val = 6; break; case 111: val = 7; break; } octal = (val * place) + octal; binary /= 1000; place *= 10; } return octal; } // to convert the hexadecimal number to octal long long int hexa_oct(char hex[]) { long long int octal, binary; // convert HexaDecimal to Binary binary = hexa_binary(hex); // convert Binary to Octal octal = binary_oct(binary); return octal; } //main function int main() { char hex[20] = "5CD"; printf("Octal Value = %lld", hexa_oct(hex)); return 0; }
输出
Octal Value = 2715
广告