-1

I have this code, but it seems to print only last 4 characters of the hexadecimal conversion.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main ()
{
int i;
char test [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,test,16);
printf ("hexadecimal: %s\n",test);
getch();
}
  • Input: 3219668508
  • Output: 3e1c
  • Expected Output: bfe83e1c

HELP?

Dev2017
  • 857
  • 9
  • 31
  • 4
    Remember that `int` is a signed type, and often only 32 bits. Then you need to learn [the ranges of integer values](http://en.cppreference.com/w/c/language/arithmetic_types#Range_of_values). Now, what is the maximum value of a signed 32-bit integer? – Some programmer dude Mar 10 '17 at 11:57
  • 3
    also how is that c++, I see c code – xander Mar 10 '17 at 11:58
  • @xander I see C-style C++ code, because in C `int main ()` would be `int main(void)`. I see no reason why it is C and not C++. If OP uses a C++ compiler, it is C++. – mch Mar 10 '17 at 12:02
  • Integers will be 32 bits. but they only go up to approx 2 billion before overflow, so scanf will fail. – Malcolm McLean Mar 10 '17 at 12:02
  • 1
    @mch Unfortunately it's all to common for beginners to not explicitly use `void` when a function has no arguments. And most compiler frontends these days support both languages, so one could e.g. use `g++` to compile C source, and `gcc` to compile C++ source. So unless we know the name of the source file there's really nothing to go on. – Some programmer dude Mar 10 '17 at 12:13
  • @mch well `int main()` is still valid c with variable arguments if anybody actually uses this syntax anymore. but OP didn't even return anything from main so that is worse. – xander Mar 10 '17 at 12:21
  • @xander the C standard requires `int main(void)` or `int main(int, char **)`. `int main()` is not in the list. There is an implicit `return 0;` statement at the end of `main`. – mch Mar 10 '17 at 12:29
  • @xander Actually it's valid to not have an explicit `return` in the `main` function in both C and C++. Leaving it out means the compiler will add an implicit `return 0;` at the end. – Some programmer dude Mar 10 '17 at 12:29
  • @Someprogrammerdude Now I'm a little bit confused - if you program C++, do you recommend an explicit void in the parameters or did you meant that for C? I'm asking in order to improve my style. – Aziuth Mar 10 '17 at 13:00
  • @Aziuth In C++ you don't need a `void` if the function has no arguments, it is optional. In C it is really mandatory, as e.g. `int foo()` means something very different from `int foo(void)`. When coding in C++, leaving it out or not is up to personal preference. I usually leave it out as it's less to write. :) ***Except*** when I make a library with functions that needs to be called from C, then the declarations in the header file needs the explicit `void` argument. – Some programmer dude Mar 10 '17 at 13:15

2 Answers2

0

Another option is to use %x printf format specifier:

printf("hexadecimal: %x\n", i);

Avoid itoa function, it is error-prone.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
0

I found a alternative to the code above, its a bit longer but works correctly.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
unsigned long int decnum, rem, quot;
char hexdecnum[100];
int i=1, j, temp;
cout<<"Enter any decimal number : ";
cin>>decnum;
quot=decnum;
while(quot!=0)
{
    temp=quot%16;
    // to convert integer into character
    if(temp<10)
    {
        temp=temp+48;
    }
    else
    {
        temp=temp+55;
    }
    hexdecnum[i++]=temp;
    quot=quot/16;
}
cout<<"Equivalent hexadecimal value of "<<decnum<<" is : \n";
for(j=i-1; j>0; j--)
{
    cout<<hexdecnum[j];
}
getch();
}

Will need to combine the elements of the array at last part though.

  • 1
    So the goal is to be able to input an arbitrarily long decimal number and convert it to hexadecimal notation? If you mentioned it in your question then we would have been able to help you much better, and probably point you to some arbitrary precision library like [The GNU Multiple Precision Arithmetic Library](https://gmplib.org/). – Some programmer dude Mar 10 '17 at 12:33
  • 1
    Oh and I suggest you [find a modern beginners book about C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and more importantly a modern environment and compiler. The `` header file is from before C++ was standardized in 1998. It doesn't exist anymore. – Some programmer dude Mar 10 '17 at 12:34
  • @Someprogrammerdude you are concerned about the header file or the code? –  Mar 10 '17 at 12:38
  • 1
    I'm concerned about your future as a C++ programmer. You appear to use a very old pre-standard variant of C++, and you're not really using all the features C++ can give you and use it as a glorified C compiler. That means you will be badly prepared for a future as a C++ programmer. – Some programmer dude Mar 10 '17 at 12:40
  • 1
    Regarding the code, adding a link to where you found it could be useful for future reference. And there are still problems with the size of your variables. Try using a value of e.g. 5 billion and you still will have problems, since I doubt an old compiler would have 64-bit `long` types even on 64 bit systems. Heck, even *modern* compilers sometimes don't have 64-bit `long` on a 64 bit system (e.g. the Visual C++ compiler). Unfortunately you can't really use fixed-width types like `uint64_t` which was standardized later in both C and C++. – Some programmer dude Mar 10 '17 at 12:48
  • @Someprogrammerdude thanks and here's the link http://www.cplusplus.com/forum/beginner/89588/ –  Mar 11 '17 at 11:24