Convert hex scanned (scanf) from an unsigned int array into decimal

20,415

Solution 1

You are passing a pointer, array, to printf(). There is no need for an array here, what you're trying to scan and print is a single number.

unsigned int number;

printf("Type in 4-digit hex:\n");

if (scanf("%x", &number) == 1)
  printf("Your number in dec is %u \n", number);

Also note that it's considered a good idea to check if scanf() succeeds or not, by inspecting the return value.

Solution 2

You don't need an array for that:

unsigned int val;
printf("Type in 4-digit hexa: \n");
scanf("%x", &val);
printf("Your number in dec is %u \n", val);

Solution 3

a. print array[0], not array.

(optional) b. scan to array, not to &array.

c. what is the point of the getchar()?

Share:
20,415
Autumn K.
Author by

Autumn K.

Hi, I'm an absolutely newbie to programming. I decided I should start somewhere and C language was my choice. I still don't know anything much and know nothing outside of stdio.h at all. Please be patient with me and feel free to guide/teach/explain, anything.

Updated on January 28, 2020

Comments

  • Autumn K.
    Autumn K. about 4 years

    I'm here trying to convert 4-digit hexa into dec but didn't succeed. Here is a my code.

    unsigned int array[4];                         
    
    printf("Type in 4-digit hexa: \n");
    
    scanf("%x", &array);
    
    while(getchar() != '\n');
    
    printf("Your number in dec is %u \n", array);
    

    I don't know what's wrong with it but it just wouldn't give out the correct dec output. Like when I put in EEFF, it's supposed to give out 61183 but the program kept on printing out 65518.

    Where's this number from? What's wrong with my code? I used unsigned int according to my consideration that FFFF is equals to 65385 and the range for unsigned int is 0 to 65535. There should be no problem with the range of data and I also used %u with it.

    The only thing I can think of right now after I've done some searching is that this problem might has sth to do with the size of unsigned int to int or sth. I read the explanation but didn't quite understand.

    I know, this might be a duplication but I'm here asking for an easier explanation of why this doesn't work. To be honest, I'm an absolutely newby for both this site and programming so please go easy on me with the coding. FYI, I don't really know anything outside of stdio.h .

    • Autumn K.
      Autumn K. about 12 years
      I tried using the code "unwind" suggested and it worked beautifully. Thank you, unwind, and also everyone who came in here and helped me!
    • enam
      enam about 12 years
      FYI cplusplus.com - this site has lots of example and might help you to understand the beauty of c/c++....
    • Autumn K.
      Autumn K. about 12 years
      Thank you for the recommendation. I'll certainly dig into it. :D
  • Autumn K.
    Autumn K. about 12 years
    Thank you so much for your answer. However, according to your answer, why "if (scanf("%x", &number) == 1)"?
  • Autumn K.
    Autumn K. about 12 years
    Thank you so much for your answer as well. Another question, if I may, when exactly do you need an array? I thought I need an array in this case since the input is 4 digits so I set up an array of 4.
  • Autumn K.
    Autumn K. about 12 years
    About checking if scanf() succeeds or not, I've traced the program step by step but it doesn't show any clue for me check. There's just a pop-up of my output window wanting me to type in the input (4-digit hexa) and just it showed me the result right away. I'm using TC complier on a 32-bits windows OS.
  • Autumn K.
    Autumn K. about 12 years
    Why array[0], not array? About the getchar(), I did it just to be safe that it doesn't scan and store the value of "\n".
  • Autumn K.
    Autumn K. about 12 years
    I really appreciate your help but please excuse me for not understanding your if-else condition in the for-loop. Would you mind explaining it?
  • asaelr
    asaelr about 12 years
    a. array is array of unsigned int. you want to print the unsigned int that is in the beginning of this array. hence, array[0]. c. don't do stuff like that. scanf is smart enough to do the checks. (and actually, since you do the getchar after the scanf, it's even not working.
  • Jens
    Jens about 12 years
    The clue is the return value from scanf. It's the number of successfully converted format specifiers. Guess why this is compared to 1?
  • Autumn K.
    Autumn K. about 12 years
    Oh, thank you! How exactly should I check whether the getchar() is working or not other than tracing the program because I did it and it didn't help me much.
  • Autumn K.
    Autumn K. about 12 years
    About the scanf() is smart enough to check, no offense since it might be just because of my poor coding skill, but there was a case I had scanf() my first int then use printf() to print it out and do the same with my second int. The result came out wrong because it stored the value of "\n" from the previous input. To be honest, I don't really know when while(getchar() != '\n'); works or doesn't even have the effect on the rest of the code but I just used it just to be safe ^^"
  • Autumn K.
    Autumn K. about 12 years
    According to my poor guess, is it concerned with 1 means true and 0 means fault?
  • asaelr
    asaelr about 12 years
    scanf("%d",...) search for digits, and doesn't store anything else.
  • Autumn K.
    Autumn K. about 12 years
    Oh, I've just known that! Well, I don't know what caused the second printf() to print out the value of "\n" from the first input now then. I thought it's because of the first scanf().....
  • ugoren
    ugoren about 12 years
    Why not let scanf do the text to number conversion? Also, your conversion code is very ugly, and badly needs some braces. And what's char *str=char[10]?
  • Jens
    Jens about 12 years
    Well, you've got one format specifier (%x), you've got a return value of 1, which is what you should expect for a successful conversion. If some conversion failed, the number is less than the format specifiers. E.g. in scanf("%x %d %u %i",&a,&b,&c,&d) you expect scanf to return 4. If it returns any other value, something failed. You MUST read the documentation for scanf to become enlightened. It may be frightening at first glance but it it will get you far.
  • Autumn K.
    Autumn K. about 12 years
    Oh, so it's the value according the number of format specifier used! Thank you!!! I'll surely search further more about scanf()! I only knew that it somehow scans and returns the value scanned, not the number of format specifier. I've never come across any sites saying anything about it. I just thought scanf() is just a simple command, but it actually has a lot more than that. Thank you for teaching me a lot today :D