C Progamming Assistence Please. HELP!!!!

UKspace

Banned
Dec 15, 2000
343
0
0
I started to teach myself to program in C about a month ago. I am only very basic. I was just wondering if anybody would be kind enough to help me write a progam that is part of an excericse sheet I am doing. I just can't do it. Everything I try to do makes my compiler swear (hyperthetically!) at me. If anybody could write it for me I would be extremely gratefu and their will be a prize! WOW!

Please, please please could someone solve the following problem: This is a copy of the problem statement on the excerice shet.


TASK Read a Hexadecimal Number

Write a program to read two charcters, and print their decimal value when interpreted as a 2-digit hexadecimal number.

Accept upper case letters 'A'...'F' for values from 10 to 15.

Print a message containing the word "Error" if either of the characters are not in the set {0..9, A..F}

Input Format

Two characters: hh
Typical input: A3

Output Format

Should be of the form: Hex hh is decimal dd
Typical output: Hex A3 is decimal 163

Notes and Hints

To read 2 characters, use:

char c1, c2;
scanf( "%c%c", &c1, %c2);

To find the "value" of a character, use

if (ch >= '0' &amp;&amp; ch <= '9' ) {
value = ch - '0'
}
if ( ch>= 'A' &amp;&amp; ch <= 'F' ) {
Value = 10 + ch - 'A';
}

This is probably simple to do but I am a complete newbee, but I will be forever grateful if you could solve it.

Thank you greatly! :)

(PM me, if you want to know what the prize is!)
 

Nevyn522

Senior member
Aug 11, 2000
208
0
0
Too bad you can't be private-messaged, I would have loved to have known about the prize.

I'm not going to give you all the code, but hopefully this will start you off in the right direction.

You're actually getting two hex digits, let's call them X and Y. The input then is XY.

The decimal conversion is rather simple.

decimal value of (XY) = 16*X + Y.

So if you write some sort of a switch statement that takes the character value of the input and convert it to a integer from 0 to 15... you should be set.

Psuedo code:

ReadIn(X, Y)
Switch (X):
'0'-'9': curr = X
'A', curr = 10
'B', curr = 11
...
'F', curr = 15
total = curr * 16
Switch (Y)
//As above

total = total + curr

Output(total)

--

You got it?
 

UKspace

Banned
Dec 15, 2000
343
0
0
Thanks.

I'll have a go, but I'm still not 100% sure.

BTW How come I can't have Personal Messages?

Thanks again.:)
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
UKspace

Hit the profile button on the top blue menu. Check the box &quot;Allow Personal Message&quot; and update. Other users can click the private messages icon on the right and send you a priveate message. If you have a private message, you will see it tomwards the top where it says &quot;No Private Messages&quot; most of the time....