Hexadecimal to octal conversion program in C program
我们得到一个十六进制数字作为字符串;任务是将其转换为八进制。要将十六进制数转换为八进制数,我们必须 -
- 找到与十六进制数等效的二进制数。
- 将二进制数转换为八进制数。
什么是十六进制数
十六进制数是以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
我们将用来解决给定问题的方法 -
- 获取输入并将其存储为字符串。
- 转换十六进制数或表达式转换为二进制,按照以下方法 -
- 通过添加各自的二进制表示来检查所有 16 种十六进制情况。
- 返回结果。
- 按照以下步骤将二进制数转换为八进制数 -
- 通过比较二进制数与八进制数的所有可能情况,取 3 个位置.
- 设置八进制的值=(val * place)+八进制;
- 二进制数除以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
The above is the detailed content of Hexadecimal to octal conversion program in C program. For more information, please follow other related articles on the PHP Chinese website!

There are significant differences in how C# and C implement and features in object-oriented programming (OOP). 1) The class definition and syntax of C# are more concise and support advanced features such as LINQ. 2) C provides finer granular control, suitable for system programming and high performance needs. Both have their own advantages, and the choice should be based on the specific application scenario.

Converting from XML to C and performing data operations can be achieved through the following steps: 1) parsing XML files using tinyxml2 library, 2) mapping data into C's data structure, 3) using C standard library such as std::vector for data operations. Through these steps, data converted from XML can be processed and manipulated efficiently.

C# uses automatic garbage collection mechanism, while C uses manual memory management. 1. C#'s garbage collector automatically manages memory to reduce the risk of memory leakage, but may lead to performance degradation. 2.C provides flexible memory control, suitable for applications that require fine management, but should be handled with caution to avoid memory leakage.

C still has important relevance in modern programming. 1) High performance and direct hardware operation capabilities make it the first choice in the fields of game development, embedded systems and high-performance computing. 2) Rich programming paradigms and modern features such as smart pointers and template programming enhance its flexibility and efficiency. Although the learning curve is steep, its powerful capabilities make it still important in today's programming ecosystem.

C Learners and developers can get resources and support from StackOverflow, Reddit's r/cpp community, Coursera and edX courses, open source projects on GitHub, professional consulting services, and CppCon. 1. StackOverflow provides answers to technical questions; 2. Reddit's r/cpp community shares the latest news; 3. Coursera and edX provide formal C courses; 4. Open source projects on GitHub such as LLVM and Boost improve skills; 5. Professional consulting services such as JetBrains and Perforce provide technical support; 6. CppCon and other conferences help careers

C# is suitable for projects that require high development efficiency and cross-platform support, while C is suitable for applications that require high performance and underlying control. 1) C# simplifies development, provides garbage collection and rich class libraries, suitable for enterprise-level applications. 2)C allows direct memory operation, suitable for game development and high-performance computing.

C Reasons for continuous use include its high performance, wide application and evolving characteristics. 1) High-efficiency performance: C performs excellently in system programming and high-performance computing by directly manipulating memory and hardware. 2) Widely used: shine in the fields of game development, embedded systems, etc. 3) Continuous evolution: Since its release in 1983, C has continued to add new features to maintain its competitiveness.

The future development trends of C and XML are: 1) C will introduce new features such as modules, concepts and coroutines through the C 20 and C 23 standards to improve programming efficiency and security; 2) XML will continue to occupy an important position in data exchange and configuration files, but will face the challenges of JSON and YAML, and will develop in a more concise and easy-to-parse direction, such as the improvements of XMLSchema1.1 and XPath3.1.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft