Thread: hexadecimal numbers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    21

    hexadecimal numbers

    hey ppl, i have a simple question. i'. supposed to WACP to accept an input, check if its a hexadecimal number or not, if yes then i have to convert all the alphabets in that number that are in upper case to lowercase and vice versa... atleast give me the logic of the program and i'll do the rest

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You've actually described most of the logic of your program, yourself.

    It isn't hard to find out what a hexadecimal number is, or what it's typical representation is - and doing the work for that will mean you learn more effectively.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    i know how to convert the case of the alphabets within the hexadecimal number...but i don't know how to work on the individual digits of a hexadecimal number..

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You should be able to just iterate through the input array.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    Quote Originally Posted by itsme86 View Post
    You should be able to just iterate through the input array.

    i don't understand what u mean by that

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You've got the your hex digits in a string, right? Something like char input[] = "34Fe6". Just iterate through it:
    Code:
    char *p;
    for(p = input;*p;++p)
      if(!strchr("0123456789ABCDEF", *p))
        printf("'%c' is not a hex digit!\n", *p);
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    i am not supposed to use strchr()... now how do i iterate thru it?

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    21

    i still haven't found a proper answer

    u guys i need help with this

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What do you want us to say, when you haven't even posted any code yet?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Step 1
    If the array is char array[10] then check each element in a loop: if (array[i] >= '0' && array[i] <= '9' || array[i] >= 'A' && array[i] <= 'F' || array[i] >= 'a' && array[i] <= 'f' ). If all characters pass that test then the number is a legitimate hexadeximal.

    Step 2:
    Loop through again to convert upper to lower case etc.

    I don't even know what WACP is... and yeah, let's see some code.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I eventually decided it meant "Write A C Program", but it took some doing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    hahahaha i'm sorry i didn know WACP wud coz such confusion...@salem yeah ur ryt it is Write a C Program... hey ppl thanks for the help... i finally did manage to come up with the code but its still generating errors please have a look.


    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
    		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++;
    	}
    	
    }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why not do low_up on your char array, rather than your int array?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    	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");
    	}
    i is 4 when the loop finishes, so you're referencing ch[4] in your if(), which is outside of the bounds of the array. Not good. And definitely not what you want to do. 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 where's the part of the if() that tests whether or not it's 0 - 9? Those are valid hex digits also, not just A - F.
    If you understand what you're doing, you're not learning anything.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    @salem
    hey that wud involve a long code!!!
    i have to do it for each character
    like,
    if 'a' then change to 'A'
    if 'b' then change to 'B'
    if 'c' then change to 'C'
    .
    .
    .
    .
    if 'f' then change to 'F'..
    besides that is not the issue here... there are errors related to the pointers, please address that first


    @itsme86
    your ryt about the if() being included in the for loop... but as for the including of 0 to 9, i don wanna do that coz the objective is that the moment you encounter an hex alphabet you have to display that it is a hexadecimal number and go about performing the upper to lower and lower to upper conversion... but if the user enters a decimal number(say 1234) it has to loop to the else statement and display that the input is not a hexadecimal number and the program shud end there.
    Last edited by Sonia; 11-24-2010 at 12:15 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