Hex/Dec Program in C getting the wrong output and cant use Scanf -


whenever run program, think getting wrong output using included test strings, though think first function working. tthe files have xbits.c xbits.h , 2 versions of showxbits.c, 1 instructor provided , other 1 trying use scanf with. program supposed convert integer hex string , hex string integer. main problem is, while think code works instructor test input, know doesn't work scanf showxbits because gives answers such 0xs when 127 inputed.

here xbits.c

#include <stdio.h> #include <math.h>  int hex_to_dec(int c) {         char hex_values[] = "aabbccddeeff";         int i;         int answer = 0;         (i=0; answer == 0 && hex_values[i] != '\0'; i++) {                 if (hex_values[i] == c) {                 answer = 10 + (i/2);                 }         }         return answer; } /* function represents int n hexstring places in hexstring array */ void itox(char* s, int n) {         char *digits = "0123456789abcdef";         int i=0,j;         char temp;         while(n > 0)         {                 s[i] = digits[n % 16];                  n /= 16;                 i++;         }         s[i] = '\0'; // add null terminator         i--;         // reverse in place         for(j=0; j < / 2; j++)         {                 temp = s[j];                 s[j] = s[i - j];                 s[i - j] = temp;         } } /* function converts hexstring array equivalent integer value  */ int xtoi(char hexstring[]) {         //printf("in xtoi, processing %s\n", hexstring);         int answer = 0;         int = 0;         int valid = 1;         int hexit;         if (hexstring[i] == '0') {                 ++i;                 if (hexstring[i] == 'x' || hexstring[i] == 'x') {                         ++i;                 }         }         while(valid && hexstring[i] != '\0') {                 answer = answer * 16;                 if(hexstring[i] >='0' && hexstring[i] <= '9') {                         answer = answer + (hexstring[i] - '0');                 }                 else {                         hexit = hex_to_dec(hexstring[i]);                         if (hexit == 0) {                                 valid = 0;                         }                         else {                         answer = answer + hexit;                         }                 }                 ++i;         }         if(!valid) {                 answer = 0;         }         return answer; } 

here showxbits.c provided instructor:

/*  *  stub driver functions study integer-hex conversions  *  */  #include <stdio.h> #include <string.h> #include "xbits.h"  #define enough_space 1000 /* not enough space */  int main() {   char hexstring[enough_space];   int m=0, n = 0x79feb220;   itox(hexstring, n);     /* stub testing: create fake input string */   strcpy(hexstring, "6bcd7890");    m = xtoi(hexstring);    printf("\t%12d %s %12d\n", n, hexstring, m);    return 0;  /* fine */ } 

and here showxbits has scanf in it:

/*  *  stub driver functions study integer-hex conversions  *  */  #include <stdio.h> #include <string.h> #include "xbits.h"  #define enough_space 100 /* not enough space */  int main() {     char hexstring[enough_space];     //int m=0, n = 0x79feb220;     int n, m;     while ((scanf("%d", &n)) == 1) {         itox(hexstring, n);         m = xtoi( hexstring);         printf("%12d  %s  %12d\n", n, hexstring, m);     }  return 0;  /* fine */ } 

like said, getting weird outputs when using scanf function. complete beginner programmer , appreciate can offered. thanks!

because there mistake in function itox , cause wrong result when reverse string. then, wrong hexstring itox result in abnormal output finally.

the quick fix replace j < / 2 j < / 2 + 1

void itox(char* s, int n) {         //......         // reverse in place         for(j=0; j < / 2 + 1 ; j++)         {                 temp = s[j];                 s[j] = s[i - j];                 s[i - j] = temp;         } } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -