简体   繁体   English

十进制到二进制、八进制和十六进制数在 C 中失败

[英]decimal to binary, oct and hex number failing in C

Following simple decimal to binary, oct or hex conversion fails.简单的十进制到二进制、八进制或十六进制转换失败。 Following is a problem statement,以下是问题陈述,

Write a function called itoc(), which converts an unsigned integer to a binary, octal, or hexadecimal character string.编写一个名为 itoc() 的 function,它将无符号 integer 转换为二进制、八进制或十六进制字符串。 Pass the conversion base as a parameter to itoc() and have it return a NULL terminated character string.将转换基数作为参数传递给 itoc() 并让它返回一个 NULL 终止的字符串。

The first part where I convert an input number in reverse order.我以相反顺序转换输入数字的第一部分。 These intermediate results correct (or may be I perceive it correct).这些中间结果是正确的(或者我认为它是正确的)。 The final reversal returns nothing.最后的逆转没有任何回报。 The watch window on 2 character array str[] and outPutString[] shows following... which think an error in my code but I am unable to figure out what is missing. 2 个字符数组 str[] 和 outPutString[] 上的手表 window 显示以下...这认为我的代码中有错误,但我无法弄清楚缺少什么。

在此处输入图像描述

//itoc.c
    
#include <stdio.h>
#include <string.h>

char *itoc(unsigned int, unsigned int);

int main(void)
{
   // variable to store base and decimal number for conversion
   unsigned int base, number;

   printf("Enter <number> and <base> here: ");
   scanf("%u %u", &number, &base);
   printf("%u is %s\n", number, itoc(base, number));

   return 0;
}

// function converts any decimal number and convert based upon base
char *itoc(unsigned int base, unsigned int number)
{
   static char str[80], outPutStr[80];       //string to hold the intermediate
                                             // results and final result
   char *ptrChar, *ptrEnd;                   // current location and final location
   unsigned int digit;

   //divide a number by base and append the remainder to an intermediate array continue to divide the 
   //number until remainder is zero. in case of hex, number greater than 9 are added with offset
   for(ptrChar = str; number > 0;)
   {
      digit = number % base;
      
      if(digit <= 9)
         *ptrChar++ = digit + '0';
      else
         *ptrChar++ = digit + 'A' - 10;

      number /= base;
   }
   *ptrChar = '\0';                       // terminate the array

   ptrChar = outPutStr;
   ptrEnd = str + (int)strlen(str);       // point to the last location of
                                          // intermediate array

   // un-reverse the internediate array holds converted number in reverse order
   for(; ptrEnd >= str;)
   {
      *ptrChar++ = *ptrEnd--;
   }
   *ptrChar = '\0';

   return outPutStr;                      // return pointer to final result
}

Here's the problem:这是问题所在:

ptrEnd = str + (int)strlen(str);       

The end pointer starts off pointing to the null terminating byte at the end of the string.结束指针开始指向字符串末尾的 null 终止字节。 So you swap that with the first character, and now your string starts with a null byte, giving you an empty string.所以你用第一个字符交换它,现在你的字符串以 null 字节开头,给你一个空字符串。

Change it to point to the last character in the string:将其更改为指向字符串中的最后一个字符:

ptrEnd = str + (int)strlen(str) - 1;       

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

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