K & R Exercise 2-3 "Hex to int converter" Problem

The program I wrote works in demographics consisting of only hexadecimal values. (Probably not the most elegant solution, but I'm a new programmer). My question is, how do I handle handling multiple hexadecimal digits like 0xAF or 0xFF, etc.? I'm not quite sure, and it seemed to me that I was very confused in trying. I do not ask someone to hold my hand, but give me advice where I made a mistake in this code and thoughts on how to fix it.

Thanks:)

/* Exercise 2-3.  Write the function htoi(s), which converts a string of
 * hexadecimal digits (including an optional 0x or 0X) into it equivalent
 * integer value. The allowable digits are 0...9 - A...F and a...f.
 * 
 */

#include <stdio.h>
#include <string.h>

#define NL '\n'
#define MAX 24

int htoi(char *hexd);

int
main(void)
{
    char str[MAX] = {0};
    char hex[] = "0123456789ABCDEFabcdef\0";
    int c;
    int i;
    int x = 0;

    while((c = getchar()) != EOF) {
        for(i = 0; hex[i] != '\0'; i++) {
            if(c == hex[i])
                str[x++] = c;
        }
        if(c == NL) {
            printf("%d\n", htoi(str));
            x = 0, i = x;
        }
    }
    return 0;
}

int
htoi(char *hexd) 
{
    int i;
    int n = 0;

    for(i = 0; isdigit(hexd[i]); i++)
        n = (16 * i) + (hexd[i] - '0');
    for(i = 0; isupper(hexd[i]); i++) /* Let just deal with lowercase characters */
        hexd[i] = hexd[i] + 'a' - 'A';
    for(i = 0; islower(hexd[i]); i++) {
        hexd[i] = hexd[i] - 'a';
        n = (16 + i) + hexd[i] + 10;
        n = hexd[i] + 10;
    }
    return n;
}
+3
source share
4 answers

- alredy (hex to int, k & r 2.3). , , .

[K & R ]

Edit:

char hex[] = "0123456789ABCDEFabcdef\0";

\ 0 . hex - alredy nul. Len (0... f) + 1 = 17 .

+1

. :

for(i = 0; isdigit(hexd[i]); i++)
    n = (16 * i) + (hexd[i] - '0');

, , , , ...

  • , isdigit() TRUE.
  • , isdigit() - FALSE.
  • , isdigit('\0') FALSE. , .
  • , 0-9.

:

  • , , . . , , htoi("1234"), undefined. .
  • .
  • , 0123456789ABCDEF0123456789ABCDEF stdin?
  • 80000000? ? ?
  • NL '\n'. C \n , , ...
+1

, MAX 10 18 24. ( int , .)

10: htoi() int, int 4 ( ), 8 ( 4 1 , 8 ), 0x 0x.

18: , htoi() long 8 ( , ), 16 , 0x 0x.

Please note that the size intand longdepend on the machine and, please look at the exercise 2.1 in the book of K & R, to find them.

0
source

Here is my version of the classic htoi () function for converting multiple hexadecimal values ​​to decimal integers. This is a complete working program that compiles and runs it.

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

int htoi(const char*);
int getRawInt(char);

int main(int argc, char **argv) {
    char hex[] = "       ";
    printf("Enter a hexadecimal number (i.e 33A)\n");
    scanf("%s", hex);

    printf("Hexedecimal %s in decimal is %d\n", hex, htoi(hex)); // result will be 826
    return 0;
}

int htoi(const char *hex) {
    const int LEN = strlen(hex) -1;
    int power = 1;
    int dec = 0;

    for(int i = LEN; i >= 0; --i) {
        dec += getRawInt(hex[i]) * power;
        power *= 16;
    }

    return dec;
}

int getRawInt(char c) {
    if(isalpha(c)) {
        return toupper(c) - 'A' + 10;
    } return c-'0';
}
0
source

Source: https://habr.com/ru/post/1707383/


All Articles