gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:13:17 26 4
gpt4 key购买 nike

每当我运行我的程序时,我认为我使用包含的测试字符串得到了错误的输出,尽管我认为我的第一个函数正在工作。我拥有的文件是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com