Home > Blockchain >  Conversion hexadecimal to decimal
Conversion hexadecimal to decimal

Time:12-26

I've already written a program, but it's a bit incorrect. I will be very grateful if you show me how to fix it.

So, here is the trouble: You have to convert the number from hexadecimal to decimal form. The main problem is that program must CALCULATE MANUALLY the number , instead of using a format specification ( as i did )

I apologize for possible inaccuracies and mistakes in English, I am just starting to familiarize myself with programming))

There is my variant:

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

#define SIZE 20

int main(void) {

    system("chcp 1251");
    char str[SIZE][SIZE], (*pstr)[SIZE];
    pstr = str;
    char* pcode;
    int kst;
    printf("Specify the number of lines you want to enter: ");
    scanf_s("%d", &kst);
    getchar();
    while (kst > SIZE)
    {
        printf("You have exceeded the allowed value (the number of lines must be less than 20): ");
        scanf_s("%d", &kst);
        getchar();
    }
    while (kst < 1)
    {
        printf("You entered a number that is too small, try entering a number between 1 and 20:");
        scanf_s("%d", &kst);
        getchar();
    }


    int isXDigit;

    printf("\nEnter the lines:\n\n");

    while (pstr < str   kst)
    {
        isXDigit = 1;

        gets_s(*pstr);

        for (pcode = *pstr; *pcode != '\0'; pcode  ){
            
            if (isxdigit(*pcode) == 0){
                **pstr = 0;
                break;
            }
        }

        pstr  ;
    }
    printf("\nThe result\n");

    unsigned long long sixteen;

    for (pstr = str; pstr < str   kst; pstr  )
    {
        if (**pstr == 0)
            printf("16: The error!\n");
        else {
            sixteen = strtoull (*pstr, NULL, 16);
            printf("16: %#020llx | 10: %llu\n", sixteen, sixteen);
        }
    }
    return 0;
}

CodePudding user response:

unsigned long long hexToNum(const char *hexSTR)
{
    const char *digits = "0123456789ABCDEF";
    const char *found = NULL;
    unsigned long long result = 0;

    while(*hexSTR)
    {
        result *= 0x10;
        if(found = strchr(digits, toupper((unsigned char)*hexSTR)))
            result  = found - digits;
        else { result = 0; break;}
        hexSTR  ;
    }
    return result;
}

CodePudding user response:

Textual representation of a number

“Hexadecimal” and “decimal” (and “octal” and “binary” et cetera) are all textual representations of a number.

That is, we write numbers with a radix, but a number exists independently of any particular representation.

That is why 0xA equals 10 equals 012 equals 0b1010 (hexadecimal, decimal, octal, and binary representations).

Remember in grade school when you were taught digit places? That is because a number is a polynomial representation of a value.

123 equals 100 20 3

Or, written as a polynomial:

 → 1×102 2×101 3×100

That 10 is the radix. If we change the radix, the number gets written differently. The following, even though it has the same digits, is a totally different number:

 → 1×162 2×161 3×160

We can see this by doing the math.

  • 3×160 is 3
  • 2×161 is 32
  • 1×162 is 256
  • 256 32 3 is 291

Dissecting a number to a polynomial representation with radix

To get the least-significant digit of a number, you simply take the remainder of dividing it by the radix:

 → 123 ÷ 10 is 12 with a remainder of 3

You can repeat this process to get every digit in the given radix:

 → 12 ÷ 10 is 1 with a remainder of 2
 → 1  ÷ 10 is 0 with a remainder of 1

So the digits, radix 10, of 123 are, least-significant to most-significant, 3, 2, and 1. We write this as 123.

Suppose we take a radix of 16?

 → 123 ÷ 16 is 7 with a remainder of 11 (which we write as B)
 → 7   ÷ 16 is 0 with a remainder of 7

So the hexadecimal (radix == 16) value of 123 is 7B.

Suppose we take a radix of 8?

 → 123 ÷ 8 is 15 with remainder 3
 → 15  ÷ 8 is 1 with remainder 7
 → 1   ÷ 8 is 0 with remainder 1

So the octal (radix == 8) value of 123 is 173.

In C the operators for getting the quotient and remainder are / and %:

  • 42 / 10 is 4 (quotient)
  • 42 % 10 is 2 (remainder)

Building a number from a polynomial representation with radix

If you are given a textual number representation and wish to build that into an actual numeric value, you only need to know the radix and have access to multiplication and addition.

For example, given 2 7 B:

 0 * 16   2   →    0   2   →  2
 2 * 16   7   →   32   7   →  39
39 * 16   11  →  624   11  →  635

Indeed this is correct: The hexadecimal representation of 635 is 27B. Said another way, the decimal representation of 0x27B is 635.

Functions!

We can make ourselves functions both to build and dissect polynomial text representations.

int hex_to_int( const char * text );

void int_to_hex( int number, char result[] );

Digit symbols

One thing to be aware of is the number of digit symbols available to a given radix.

  • For decimal we use the digit symbols 0 through 9.
  • For octal we only use the digit symbols 0 through 7.
  • For binary we only need 0 and 1.

But we don’t have enough Arabic digit symbols for hexadecimal, which needs 16 digit symbols. So we just start using the alphabet:

 → 0 1 2 3 4 5 6 7 8 9 A B C D E F

Converting to hex is easy. Just use an array:

char digits[] = "0123456789ABCDEF";

To output the digit symbol for digit 13, just use the array:

printf( "%c", digits[13] );  // prints "D"

Going the other way is a little trickier. I recommend you make yourself a little function:

int hex_digit_to_value( int digit )
{
  if ((digit >= '0') and (digit <= '9')) return (digit - '0');
  if ((digit >= 'A') and (digit <= 'F')) return (digit - 'A')   10;
  if ((digit >= 'a') and (digit <= 'f')) return (digit - 'a')   10;
  return 0;  // can't happen? maybe return -1?
}
  •  Tags:  
  • c
  • Related