Converting binary to octal using strings

喜你入骨 提交于 2019-12-14 03:35:01

问题


I have to convert hexadecimal numbers to octal numbers by converting hex numbers to binary first, and from binary to octal.

#include <stdio.h>
#include <string.h>
int main(){
    char binarni_brojevi[16][5] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    char heksadekadni_broj[] = "AF1";
    int i, vrednost;
    char binarni[50];
    binarni[0] = '\0';
    for(i = 0; heksadekadni_broj[i]; i++) {
        if(isalpha(heksadekadni_broj[i]))
            vrednost = (heksadekadni_broj[i] - 'A' + 10);
        else
            vrednost = (heksadekadni_broj[i] - '0');
        strcat(binarni, binarni_brojevi[vrednost]);
    }
    // what do I do from here? How should I group by 3
    return 0;
}

回答1:


To group characters by 3, first count how many there are:

int num_of_binary_digits = strlen(binarni);

This may not be divisible by 3. For example:

Binary string: 00001111
Subdivided into groups of 3: 00|001|111

To count the number of octal digits, divide by 3 with rounding up:

int num_of_octal_digits = (num_of_binary_digits + 2) / 3;

To determine how many binary digits there are in the first group, use some basic arithmetic (I left it out for brevity).

Then do nested loops:

for (int od = 0; od < num_of_octal_digits; ++od)
{
    int bits_in_group = (od == 0) ? digits_in_first_group : 3;
    for (int bd = 0; bd < bits_in_group; ++bd)
    {
        ...
    }
}

Inside the inner loop you will have to convert a string of characters like "11" or "110" into a number like 3 or 6. This should be easy.




回答2:


It is not very clear what you are trying to achieve.

You can convert an integral variable to octal like this: sprintf(string, "%o", value);. The question only has meaning in the context of human readable formats, if your numeric variables are held as strings ;)

This example shows how to convert the hex string to an octal string. I don't know why you need a set of 16 binary string patterns.

#include <stdio.h>

int main(void){
    char heksadekadni_broj[] = "AF1";
    char octal[50];
    int vrednost;

    sscanf(heksadekadni_broj, "%x", &vrednost);     // read in the hex value
    sprintf(octal, "%o", vrednost);                 // put out as octal

    printf ("Heks: %s, Octal: %s\n", heksadekadni_broj, octal);
    return 0;
}

Program output:

Heks: AF1, Octal: 5361



回答3:


To convert a binary string to an octal string, if in the range of [0 UINTMAX_MAX], use library functions strtoumax() and snprintf().

#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>

// return 0 on success, else return 1
int binary_to_octal_string(char *ostr, size_t osize, const char *bstr) {
  char *endptr;
  errno = 0;
  uintmax_t y = strtoumax(bstr, &endptr, 2);
  // If trouble with conversion (overflow, empty string, non-'0' or '1' found)
  if (errno || bstr == endptr || *endptr) return 1;

  int n = snprintf(ostr, osize, "%" PRIoMAX, y);
  // If destination buffer too small or unlikely encoding error ...
  if (n >= osize || n < 0) return 1;

  return 0;
}   


来源:https://stackoverflow.com/questions/32208150/converting-binary-to-octal-using-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!