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 < /ul>
- 返回结果。
算法
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Given below is a C language algorithm to convert Roman numerals to decimal numbers: Algorithm Step 1 - Start Step 2 - Read Roman numerals at runtime Step 3 - Length: = strlen(roman) Step 4 - For i=0 to Length-1 Step 4.1-switch(roman[i]) Step 4.1.1-case'm': &nbs

Lexicographic string comparison means that strings are compared in dictionary order. For example, if there are two strings 'apple' and 'appeal', the first string will come last because the first three characters of 'app' are the same. Then for the first string the character is 'l' and in the second string the fourth character is 'e'. Since 'e' is shorter than 'l', it will come first if we sort lexicographically. Strings are compared lexicographically before being arranged. In this article, we will see different techniques for lexicographically comparing two strings using C++. Using the compare() function in C++ strings The C++string object has a compare()

Linked lists use dynamic memory allocation, i.e. they grow and shrink accordingly. They are defined as collections of nodes. Here, a node has two parts, data and links. The representation of data, links and linked lists is as follows - Types of linked lists There are four types of linked lists, as follows: - Single linked list/Singly linked list Double/Doubly linked list Circular single linked list Circular double linked list We use the recursive method to find the length of the linked list The logic is -intlength(node *temp){ if(temp==NULL) returnl; else{&n

The rename function changes a file or directory from its old name to its new name. This operation is similar to the move operation. So we can also use this rename function to move files. This function exists in the stdio.h library header file. The syntax of the rename function is as follows: intrename(constchar*oldname,constchar*newname); The function of the rename() function accepts two parameters. One is oldname and the other is newname. Both parameters are pointers to constant characters that define the old and new names of the file. Returns zero if the file was renamed successfully; otherwise, returns a nonzero integer. During a rename operation

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

A map is a special type of container in C++ in which each element is a pair of two values, namely a key value and a map value. The key value is used to index each item, and the mapped value is the value associated with the key. Regardless of whether the mapped value is unique, the key is always unique. To print map elements in C++ we have to use iterator. An element in a set of items is indicated by an iterator object. Iterators are primarily used with arrays and other types of containers (such as vectors), and they have a specific set of operations that can be used to identify specific elements within a specific range. Iterators can be incremented or decremented to reference different elements present in a range or container. The iterator points to the memory location of a specific element in the range. Printing a map in C++ using iterators First, let's look at how to define

Using strings or characters is sometimes very useful when solving some logic programming problems. A string is a collection of characters, which is a 1-byte data type used to hold symbols in ASCII values. Symbols can be English letters, numbers, or special characters. In this article, we will learn how to check if a character is an English letter or a letter of the alphabet using C++. Checking the isalpha() function To check if a number is a letter, we can use the isalpha() function in the ctype.h header file. This takes a character as input and returns true if it is an alphabet, false otherwise. Let us look at the following C++ implementation to understand the usage of this function. The Chinese translation of Example is: show

Modern science relies heavily on the concept of plural numbers, which was first established in the early 17th century by Girolamo Cardano, who introduced it in the 16th century. The formula for complex numbers is a+ib, where a holds the html code and b is a real number. A complex number is said to have two parts: the real part <a> and the imaginary part (<ib>). The value of i or iota is √-1. The plural class in C++ is a class used to represent complex numbers. The complex class in C++ can represent and control several complex number operations. Let's take a look at how to represent and control the display of plural numbers. imag() member function As mentioned above, complex numbers are composed of real part and imaginary part. To display the real part we use real()
