Thread: hexadecimal numbers

  1. #16
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    so what you mean that 1234 is NOT a hex number ?

    you can set a flag to check if it is a hex number and if it is, then move to change cases
    Last edited by gaurav9991; 11-24-2010 at 12:41 AM.

  2. #17
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Sonia
    but if the user enters a decimal number(say 1234)
    Uhh...1234 is also a hexadecimal number...
    If you understand what you're doing, you're not learning anything.

  3. #18
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    yeah i cud that... but it shud work fine with the method i have used too. there are errors being generated with that pointer can u pl address that

  4. #19
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    it will be easy for us to help you if you tell us about errors
    please specify what are they

  5. #20
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    1.
    Code:
    low_up(&temp);
    generates and error " cannot convert int[4] * to int" and "type mismatch in parameter 1"

    2.
    Code:
    else
    		printf("this is not a hexadeimal number");
    generates a misplaced else error.

  6. #21
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void low_up(int);
    void main()
    {
    	char ch[4];
    	int temp[4],i;
    	clrscr();
    	printf("Enter a hexadecimal number");
    	for(i=0;i<4;i++)
    	{
    		scanf("%c", &ch[i]);
    		temp[i]=ch[i];
    	}
    	if((ch[i]>='a' && ch[i]<='f')||(ch[i]>='A' && ch[i]<='F'))
    	{
    		printf("This is a hexadecimal number");
    	}
    	low_up(&temp);
    	for(i=0;i<4;i++)
    		printf("%d\n", temp[i]);
    	else // where is it's if ?
    		printf("this is not a hexadeimal number");
    	getch();
    
    }
    void low_up(int *j)
    {
    	int k;
    	for(k=0;k<4;k++)
    	{
    		if(*j>=97 && *j<=102)
    			*j=*j - 32;
    		else if(*j>=65 && *j<=70)
    			*j=*j + 32;
    		j++;
    	}
    	for(i=0;i<4;i++)
    	{
    		printf("%c", *j);
                    j++;
    	}
    	
    }
    in low_up you are passing an integer pointer(array pointer)
    name of an array itself represents it's base address, so no need to use & while calling

  7. #22
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Your whole low_up() function is convoluted and ugly and the bottom loop is wrong. Why not make it a little easier to read?
    Code:
    void low_up(int *j)
    {
      int k;
      for(k = 0;k < 4;++k)
        j[k] = isupper(j[k]) ? tolower(j[k]) : toupper(j[k]);
    }
    The problem with the bottom part of your loop is that j has already walked off the end of your array by the end of the first loop, so you're ending up with undefined operations since j is outside the bounds of the array.
    If you understand what you're doing, you're not learning anything.

  8. #23
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    well see the prob is that if the number does not contain any alphabets i want it to print " it is not a hexadecimal number" and also after the low up function is done i want to print the number... so i don understand where to put that else... i'm confused.,..


    and even if i enter &temp instead of temp. it still gives the same error
    Last edited by Sonia; 11-24-2010 at 01:52 AM.

  9. #24
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Sonia View Post
    well see the prob is that if the number does not contain any alphabets i want it to print " it is not a hexadecimal number" and also after the low up function is done i want to print the number... so i don understand where to put that else... i'm confused.,..
    That's because you don't listen. Remember when I said:
    Quote Originally Posted by itsme86
    you want the if() to be inside the loop instead, and you need to write it to see if ch[i] is not a hex digit instead.
    ...and you decided to blow it off? That's why you're having trouble figuring out how to print out the messages.
    If you understand what you're doing, you're not learning anything.

  10. #25
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    here is the code but don't copy as it is! I am posting the complete code just you to understand how flag can be used (assuming you don't know) and the low_up fun is from the upper post. look at the data-type of ch and temp(using int will be lengthy)

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void low_up(char *);
    int main()
    {
    	char ch[4], temp[4], i, flag;
    
    	printf("Enter a hexadecimal number : ");
    
    	for (i = 0; i < 4; i++)
    	{
    		scanf("%c", &ch[i]);
    		temp[i] = ch[i];
    	}
    
    	flag = 0;
    
    	for (i = 0; i < 4; i++)
    	{
    
    		if ((ch[i] >= 'a' && ch[i] <= 'f') || (ch[i] >= 'A' && ch[i] <= 'F') || (ch[i] >= 1 && ch[i] <= 9)) ;
    		{
    			flag++;
    		}
    	}
    	if (flag == 4)
    	{
    		printf("this is a hexadeimal number");
    		low_up(ch);
    		printf("\n\n");
    	}
    	else
    	{
    		printf("this is NOT a hexadeimal number");
    	}
    	getch();
    	return 0;
    }
    
    void low_up(char *j)
    {
    	int k, i;
    
    	for (k = 0; k < 4; ++k)
    		j[k] = isupper(j[k]) ? tolower(j[k]) : toupper(j[k]);
    
    	for (i = 0; i < 4; i++)
    	{
    		printf("%c", *j);
    		j = j + 1;
    	}
    }

  11. #26
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    @gaurav9991: I'm guessing that's a booby trap in your code.
    If you understand what you're doing, you're not learning anything.

  12. #27
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    which one ??

  13. #28
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39

    Talking 'Eat' is not hex!

    Hello. How is the weather? We have had 2" of snow here and a bit too cold to go out, so I decided to look for a program to write.

    Is this what you were trying to do?
    Code:
    anch -run ishex.a.c #execute source code!
    Enter a hex number.
    Eat
    'Eat' is not hex!
    
    anch -run ishex.a.c
    Enter a hex number.
    FeeD
    'FeeD' is hex!
    This is actually C.
    I just forgot a few brackets and stuff to test this free, open-source pseudocode compiler.
    Anchor | freshmeat.net converts it to C, compiles it, and even invokes tcc to compile it to memory and run it directly.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX_STRING 4
    
    #ifndef stricmp
    #define stricmp strcasecmp
    #endif
    
    int isHex  char *s
        static int i
        char u[MAX_STRING+1]
        if  s
            sscanf(s,"%x",&i)
            sprintf(u,"%x",i)
            if  !stricmp(s,u)
                return 1
        return 0
    
    int main  void
        char s[MAX_STRING+1]
        char fmt[10]
        sprintf  fmt,"%%%is",MAX_STRING
        printf  "Enter a hex number.\n"
        scanf  fmt,&s
        printf  "'%s' is %shex!\n",s,isHex(s)?"":"not "
        return 0

  14. #29
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Oh no python fan?

  15. #30
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    @gaurav...
    your code is good and it made clear that flag part... but then i'm not allowed to use isupper or islower. and so for that i need to compare the ascii values for which i need the int array temp[].

    @itsme86 i really don understand that if() func part man..

    @hellork
    i didn even get a single part of ur program...

    u guys i'm, jus losing it now... i'm so confused
    Last edited by Sonia; 11-24-2010 at 03:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM