c - C 中的十六进制/十进制程序得到错误的输出并且无法使用 Scanf

标签 c

每当我运行我的程序时,我认为我使用包含的测试字符串得到了错误的输出,尽管我认为我的第一个函数正在工作。我拥有的文件是 xbits.c xbits.h 和 showxbits.c 的两个版本,一个是讲师提供的,另一个是我尝试使用 scanf 的文件。该程序应该将整数转换为十六进制字符串,然后将十六进制字符串转换为整数。我的主要问题是,虽然我认为我的代码适用于讲师测试输入,但我知道它不适用于 scanf showxbits,因为当输入 127 时它会给出诸如 0xS 之类的答案。

这是 xbits.c

#include <stdio.h>
#include <math.h>

int hex_To_dec(int c) {
        char hex_values[] = "aAbBcCdDeEfF";
        int i;
        int answer = 0;
        for (i=0; answer == 0 && hex_values[i] != '\0'; i++) {
                if (hex_values[i] == c) {
                answer = 10 + (i/2);
                }
        }
        return answer;
}
/* function represents the int n as a hexstring which it places in the
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--;
        // Now reverse it in place
        for(j=0; j < i / 2; j++)
        {
                temp = s[j];
                s[j] = s[i - j];
                s[i - j] = temp;
        }
}
/* function converts hexstring array to equivalent integer value  */
int xtoi(char hexstring[]) {
        //printf("in xtoi, processing %s\n", hexstring);
        int answer = 0;
        int i = 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;
}

这里是讲师提供的showxbits.c:

/*
 *  stub driver for functions to study integer-hex conversions
 *
 */

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

#define ENOUGH_SPACE 1000 /* not really enough space */

int main() {
  char hexstring[ENOUGH_SPACE];
  int m=0, n = 0x79FEB220;
  itox(hexstring, n);


  /* for stub testing: create a fake input string */
  strcpy(hexstring, "6BCD7890"); 
  m = xtoi(hexstring);

  printf("\t%12d %s %12d\n", n, hexstring, m);

  return 0;  /* everything is just fine */
}

这是包含 scanf 的 showxbits:

/*
 *  stub driver for functions to study integer-hex conversions
 *
 */

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

#define ENOUGH_SPACE 100 /* not really 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;  /* everything is just fine */
}

就像我说的,使用 scanf 函数时我得到了奇怪的输出。我是一名完全的初学者程序员,非常感谢您提供的任何帮助。谢谢!

最佳答案

因为函数itox有错误,这会在反转字符串时导致错误的结果。然后,来自 itox 的错误十六进制字符串最终会导致输出异常。

快速修复方法是替换 j < i / 2j < i / 2 + 1

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

关于c - C 中的十六进制/十进制程序得到错误的输出并且无法使用 Scanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30925204/

相关文章:

c - 将 C 结构翻译为 Fortran 等价物

c - __PTRDIFF_TYPE__ 与 ptrdiff_t

html - C编程任务,html源文件

c - 宏未编译

c - 编写一个创建子进程的程序。父进程应显示父进程和子进程的PID?

c - 为什么我的 BST 根指针由于某些未知原因而改变?

c - 我的字符指针返回奇怪的字符串

c++ - 使用前声明、包含和 header /源拆分的历史原因。需要寻找合适的引用

c - 共享库局部变量线程安全吗?

c - 如何获取与 Linux 上的登录关联的用户 ID?