Rumah > pembangunan bahagian belakang > C++ > Program penukaran heksadesimal kepada perlapanan dalam program C

Program penukaran heksadesimal kepada perlapanan dalam program C

WBOY
Lepaskan: 2023-08-29 14:17:02
ke hadapan
1321 orang telah melayarinya

我们得到一个十六进制数字作为字符串;任务是将其转换为八进制。要将十六进制数转换为八进制数,我们必须 -

  • 找到与十六进制数等效的二进制数。
  • 将二进制数转换为八进制数。

什么是十六进制数

十六进制数是以16为基数的数字,数字从0到9,从10开始数字表示为A其中代表 10,B 代表 11,C 代表 12,D 代表 13,E 代表 14,F 代表 15。

要将十六进制数转换为二进制数,每个数字都会转换为 4 位的二进制数

什么是八进制

计算机中的八进制以8为基数表示,即0-7的八进制数由三个二进制数或三个二进制数字组成。

我们必须做什么

就像我们有一个十六进制数 1A6,所以它现在对于十六进制表示 1、10 和 6首先,我们必须找到十六进制数的二进制等价物,即,

Program penukaran heksadesimal kepada perlapanan dalam program C

因此,1A6 的二进制 = 0001 1010 0110

现在找到十六进制数的二进制后,下一个任务是找到八进制

在此之前,我们将二进制数分为三组。分组为 3 后,我们将得到 000 110 100 110

其八进制表示形式为 -

Program penukaran heksadesimal kepada perlapanan dalam program C

因此十六进制数 1A6 的八进制表示为 − 646

示例

Input: 1A6
Output: Octal Value = 646
Explanation:

Input: 1AA
Output: 652
Salin selepas log masuk

我们将用来解决给定问题的方法 -

  • 获取输入并将其存储为字符串。
  • 转换十六进制数或表达式转换为二进制,按照以下方法 -
    • 通过添加各自的二进制表示来检查所有 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] != &#39;\0&#39; and i++ {
          binary = binary * place;
          switch (hex[i]) {
             case &#39;0&#39;:
                binary += 0
             case &#39;1&#39;:
                binary += 1
             case &#39;2&#39;:
                binary += 10
             case &#39;3&#39;:
                binary += 11
             case &#39;4&#39;:
                binary += 100
             case &#39;5&#39;:
                binary += 101
             case &#39;6&#39;:
                binary += 110
             case &#39;7&#39;:
                binary += 111
             case &#39;8&#39;:
                binary += 1000
             case &#39;9&#39;:
                binary += 1001
             case &#39;a&#39;:
             case &#39;A&#39;:
                binary += 1010
             case &#39;b&#39;:
             case &#39;B&#39;:
                binary += 1011
             case &#39;c&#39;:
             case &#39;C&#39;:
                binary += 1100
             case &#39;d&#39;:
             case &#39;D&#39;:
                binary += 1101;
                break;
             case &#39;e&#39;:
             case &#39;E&#39;:
                binary += 1110;
                break;
             case &#39;f&#39;:
             case &#39;F&#39;:
                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;
    }
    Salin selepas log masuk

    示例

    #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] != &#39;\0&#39;; i++) {
          binary = binary * place;
          switch (hex[i]) {
          case &#39;0&#39;:
             binary += 0;
             break;
          case &#39;1&#39;:
             binary += 1;
             break;
          case &#39;2&#39;:
             binary += 10;
             break;
          case &#39;3&#39;:
             binary += 11;
             break;
          case &#39;4&#39;:
             binary += 100;
             break;
          case &#39;5&#39;:
             binary += 101;
             break;
          case &#39;6&#39;:
             binary += 110;
             break;
          case &#39;7&#39;:
             binary += 111;
             break;
          case &#39;8&#39;:
             binary += 1000;
             break;
          case &#39;9&#39;:
             binary += 1001;
             break;
          case &#39;a&#39;:
          case &#39;A&#39;:
             binary += 1010;
             break;
          case &#39;b&#39;:
          case &#39;B&#39;:
             binary += 1011;
             break;
          case &#39;c&#39;:
          case &#39;C&#39;:
             binary += 1100;
             break;
          case &#39;d&#39;:
          case &#39;D&#39;:
             binary += 1101;
             break;
          case &#39;e&#39;:
          case &#39;E&#39;:
             binary += 1110;
             break;
          case &#39;f&#39;:
          case &#39;F&#39;:
             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;
    }
    Salin selepas log masuk

    输出

    Octal Value = 2715
    Salin selepas log masuk

    Atas ialah kandungan terperinci Program penukaran heksadesimal kepada perlapanan dalam program C. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:tutorialspoint.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan