This is a discussion on hexadecimal numbers within the C Programming forums, part of the General Programming Boards category; hey ppl, i have a simple question. i'. supposed to WACP to accept an input, check if its a hexadecimal ...

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. 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.

3. 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. You should be able to just iterate through the input array.

5. Originally Posted by itsme86
You should be able to just iterate through the input array.

i don't understand what u mean by that

6. 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);```

7. i am not supposed to use strchr()... now how do i iterate thru it?

8. ## i still haven't found a proper answer

u guys i need help with this

9. What do you want us to say, when you haven't even posted any code yet?

10. 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. I eventually decided it meant "Write A C Program", but it took some doing.

12. 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();
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'))
{
}
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. Why not do low_up on your char array, rather than your int array?

14. 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'))
{
}```
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.

15. @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.

Page 1 of 3 123 Last