Thread: isxdigit(int c) of ctype.h! Where is the mistake??

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    34

    Unhappy isxdigit(int c) of ctype.h! Where is the mistake??

    I am passing a hexadecimal number to isxdigit() but still it is not recognizing it! Please help me out!
    Code:
    //int  isxdigit(int c)
    
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
        int var = 0xE;
        
        if(isxdigit(var))
        {
            printf("\n|%x| is hexadecimal \n",var);
        }
        else
        {
            printf("\n|%x| is not hexadecimal\n",var);
        }
    }

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    I think you might be confused about the nature of this function. It checks if the passed int maps onto a hexadecimal character (0-9, A-F) in the ASCII table. 0xE (hex) evaluates to 14 (decimal), and that's not one of those characters. The character "E" would be 69.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    isxdigit() checks to see whether the passed integer corresponds to a hexadecimal digit. If you understand the difference between 0 and '0', you'll see why it doesn't work. For reference, take a look at this:
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    You can check it yourself, do:
    Code:
    putchar(0xE);
    It doesn't output a hexadecimal digit, does it?
    Last edited by GReaper; 11-17-2017 at 08:31 AM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Oh so it checks a single character is hexadecimal or not , and not whether the passing integer is hexadecimal or not. So for 'E' it shows hexadecimal but what if I pass "FF" as a char string ? In that case it only takes 'F' and the next 'F' is left behind. Now since 'F' is hexadecimal it shows hexadecimal. Is there any workaround to check if "FF" is hexadecimal ?

    Thanx!

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Thanx Man!

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    810
    Quote Originally Posted by ranadas View Post
    Oh so it checks a single character is hexadecimal or not , and not whether the passing integer is hexadecimal or not. So for 'E' it shows hexadecimal but what if I pass "FF" as a char string ? In that case it only takes 'F' and the next 'F' is left behind. Now since 'F' is hexadecimal it shows hexadecimal. Is there any workaround to check if "FF" is hexadecimal ?

    Thanx!
    You would have to code a function yourself to check each character in the string. Not that hard to do. Give it a try!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ctype.h?? n fflush??
    By rev91 in forum C Programming
    Replies: 4
    Last Post: 06-18-2011, 06:37 AM
  2. ctype.h HELP !!!
    By player1 in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2011, 05:46 PM
  3. Understanding ctype.h
    By yougene in forum C Programming
    Replies: 27
    Last Post: 03-05-2008, 05:27 PM
  4. ctype segmentation fault
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 04-11-2007, 07:15 AM
  5. Ctype.h problem?
    By caroundw5h in forum C Programming
    Replies: 1
    Last Post: 12-26-2003, 08:42 PM

Tags for this Thread