我们得到一个十六进制数字作为字符串;任务是将其转换为八进制。要将十六进制数转换为八进制数,我们必须 -
十六进制数是以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
现在找到十六进制数的二进制后,下一个任务是找到八进制
在此之前,我们将二进制数分为三组。分组为 3 后,我们将得到 000 110 100 110
其八进制表示形式为 -
因此十六进制数 1A6 的八进制表示为 − 646
Input: 1A6 Output: Octal Value = 646 Explanation: Input: 1AA Output: 652
我们将用来解决给定问题的方法 -
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
Das obige ist der detaillierte Inhalt vonHexadezimal-Oktal-Konvertierungsprogramm im C-Programm. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!