【问题标题】:Hex to dec/ dec to hex program. Cannot get program to print string for char array十六进制到十进制/十进制到十六进制程序。无法让程序打印字符数组的字符串
【发布时间】:2013-10-16 05:09:34
【问题描述】:

我正在编写一个将十六进制转换为十进制并将十进制转换为十六进制的程序。我写了两个函数:itox(hexstring, n) 和 int xtoi(hexstring)。这两个功能由驱动程序实现。当我使用 printf 返回 int 和 hexstring 时,hexstring 永远不会出现在输出中。此外,该程序似乎永远不会进入 xtoi() 函数中的 printf 语句。这是我得到的输出:

--------------:~/cs240/hw3$ gcc showxbits.c xbits.c -o showxbits
--------------:~/cs240/hw3$ ./showxbits
in itox, processing 47
                  47             0

我是 C 新手。下面是我的代码。任何建议表示赞赏。

驱动代码为:

  1 /*
  2  *  stub driver for functions to study integer-hex conversions
  3  *
  4  */
  5
  6 #include <stdio.h>
  7 #include "xbits.h"
  8
  9 #define ENOUGH_SPACE 100 /* not really enough space */
 10
 11 int main() {
 12   char hexstring[ENOUGH_SPACE];
 13   int m = 0, n = 47;
 14   itox( hexstring, n);
 15   printf("%s", hexstring);
 16
 17   /* for stub testing: create a fake input string*/
 18
 19   m= xtoi(hexstring);
 20
 21   printf("\t%12d %s %12d\n", n, hexstring, m);
 22
 23   return 0;  /* everything is just fine */
 24 }
 25
 26

这两个函数的代码是:

  1 /*
  2  *  stubs for functions to study
  3  *  integer-hex conversions
  4  *
  5  */
  6
  7 #include <stdio.h>
  8 #include "xbits.h"
  9
 10 /* function represents the int n as a hexstring which it places in the
 11 hexstring array */
 12
 13 void itox( char hexstring[], int n) {
 14         char hexkey[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 15         int rem;
 16         int res = n;
 17         int i;
 18         for(i = 0; res < 16; i++){
 19                 rem = res%16;
 20                 res = res/16;
 21                 hexstring[i] = hexkey[rem];
 22         }
 23         i++;
 24         hexstring[i] = hexkey[res];
 25         i++;
 26         hexstring[i] = '\0';
 27
 28    printf("in itox, processing %d\t%s\n", n, hexstring);
 29 }
 30
 31 /* function converts hexstring array to equivalent integer value  */
 32
 33 int xtoi( char hexstring[]) {
 34         int cursor;
 35         int count = 0;
 36         char current;
 37         int dec = 0;
 38         int pow = 1;
 39         int i;
 40         int j;
 41         char hexkey[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 42
 43         for(cursor = (2*sizeof(char)); cursor >= 0; --cursor){
 44                 current = hexstring[cursor];
 45                 for(i = 0; i < 16; ++i){
 46                         if(current == hexkey[i]){
 47                                 if(count == 0){
 48                                         dec = dec + i;
 49                                 }
 50                                 else{
 51                                         for(j = 0; j < count; j++)
 52                                                 pow = pow * 16;
 53                                 dec = dec + pow*i;
 54                                 pow = 1;
 55                                 }
 56                         }
 57                 }
 58                 ++count;
 59         }
 60         return dec;
 61
 62   printf("in xtoi, processing %s\n", hexstring);
 63 }
 64

【问题讨论】:

  • 0 的值与字符'0' 不同。 0 的值实际上是字符串终止符。
  • 还有更简单的从数字构造字符串的方法:snprintf。而相反的可以通过strtol来完成。

标签: c


【解决方案1】:

缺少单引号

char hexkey[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
                 ^ ^


 0=='\0' ==> Nul character used to terminate strings.

 0!='0'    

'0' ASCII value 48 

你错了

for(i = 0; res < 16; i++){     
               ^ 
                  rem = res%16;
                  res = res/16;
                 hexstring[i] = hexkey[rem];
         }

for 循环条件错误。

   n==47 ==> res==47 and res<16 ==> 47 < 16 failed.   

这样修改

for(i = 0; res >0; i++)
         {
                  rem = res%16;
                  res = res/16;
                  hexstring[i] = hexkey[rem];
         }
                   hexstring[i] = '\0';

 printf("in itox, processing %d==%s\n", n, hexstring); // You need to reverse it.

【讨论】:

  • 很抱歉,我对您的评论感到困惑。如果我理解正确,您是说零字符 == '\0' (我认为'\0' 用于终止字符串)而不是 0 == '0'?
  • 好的,所以我的问题是,当我调用 hexstring 时,程序认为该字符串会立即终止?
  • 我知道这是一个问题并已更改它....即使程序中不存在 hexstring,我仍然没有得到任何输出。
  • 是的,只是再次运行它,单引号变回了。相同的输出:(
  • 这无疑是一个错误。现在我进入 itox,处理 47 2 2 47 2 527 输出....所以 2 是 hexstring 的输出
【解决方案2】:

我没有足够的代表发表评论。这篇文章对我帮助很大。如果其他人遇到这个问题,为什么他的第二个函数不会打印是他的 return 语句在他的 printf 语句之前。如果在这里翻转它们:

  printf("in xtoi, processing %s\n", hexstring);
  return dec;

然后第二个函数将打印该行。

【讨论】:

    猜你喜欢
    • 2018-05-17
    • 2023-03-14
    • 2010-10-04
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2023-04-08
    • 2012-12-12
    相关资源
    最近更新 更多