简体   繁体   English

十六进制转换为dec / dec转换为十六进制程序。 无法获取程序为char数组打印字符串

[英]Hex to dec/ dec to hex program. Cannot get program to print string for char array

I am writing a program that converts hex to dec and dec to hex. 我正在编写一个将十六进制转换为dec并将dec转换为十六进制的程序。 I have written two functions: itox(hexstring, n) and int xtoi(hexstring). 我编写了两个函数:itox(hexstring,n)和int xtoi(hexstring)。 These two functions are implemented by a driver. 这两个功能由驱动程序实现。 When I use printf to return the int and the hexstring, the hexstring never shows up in the output. 当我使用printf返回int和hexstring时,hexstring永远不会出现在输出中。 Also, the program never seems to make it to the printf statement within the xtoi() function. 另外,该程序似乎从未在xtoi()函数中使用printf语句。 This is the output I'm getting : 这是我得到的输出:

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

I am new to C. Below is my code. 我是C的新手。以下是我的代码。 Any advice is appreciated. 任何建议表示赞赏。

The driver code is: 驱动程序代码为:

  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

The code for the two functions is : 这两个函数的代码是:

  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

missing single quotes 缺少单引号

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 

Here you are going wrong 在这里你错了

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

for loop condition wrong. for循环条件错误。

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

Modify Like this 像这样修改

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.

I don't have enough rep to comment. 我没有足够的代表发表评论。 This post helped me out a lot. 这篇文章对我帮助很大。 If anyone else comes across this the issue for why his second function won't print is that his return statement is before his printf statement. 如果还有其他人遇到这个问题,为什么他的第二个函数不打印是因为他的return语句在他的printf语句之前。 If here were to flip them around: 如果要翻转它们:

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

Then the second function will print the line. 然后第二个功能将打印该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM