Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hexadecimal input?

Status
Not open for further replies.

Crashke

Programmer
Aug 5, 2002
1
BE
Hello,

I'm looking for a way to read integer values, and to make it clear to the computer that those are hexadecimal values.
Anybody got an idea?
Thanks a lot,

Crashke.
 
Here is some old code I used

Code:
=================================================================================================

int main()
{

	unsigned int value;
	int str_len;
	char selection[64];
	char input_string[64];
	int done = 0;

	while(!done)
	{
	cout<<&quot;(b) - enter a binary value\n&quot;
	      &quot;(d) - enter a decimal value\n&quot;
	      &quot;(h) - enter a hex value\n&quot;
	      &quot;(o) - enter an octal value\n&quot;
	      &quot;(q) - to quit\n&quot;
	      &quot;Selection: &quot;;

	//cin.getline(selection,63);
	cin>>selection;

	switch(*selection)
	{
		case 'b':
			cout<<&quot;Enter your binary value: &quot;;
			cin>>input_string;
			str_len = strspn(input_string,&quot;01&quot;);
			if(strlen(input_string)!=str_len)
			{
				cerr<<&quot;Invalid Binary String Entered\n&quot;;
				continue;
			}
			else
			{
				value = 0;
				for(int i=0,j=str_len-1;i<str_len;i++,j--)
					if(input_string[i] == '1')		
						value |= 1<<j;
			}
		break;
		case 'd':
			cout<<&quot;Enter your decimal value: &quot;;
			cin>>dec>>value;
		break;
		case 'h':
			cout<<&quot;Enter your hex value: &quot;;
			cin>>hex>>value;
		break;
		case 'o':
			cout<<&quot;Enter your octal value: &quot;;
			cin>>oct>>value;
		break;
		case 'q':
			done = 1;
			continue;
		break;
		default:
			cerr<<&quot;invalid choice!\n\n&quot;;
			continue;
		break;
	}
	
	bin_struct bin_out;
	bin_out = value;
	cout<<&quot;\n******************************************\n&quot;;
	cout<<&quot;Binary: &quot;<<bin_out<<endl;
	cout<<&quot;Decimal: &quot;<<dec<<value<<endl;
	cout<<&quot;Hex: &quot;<<hex<<value<<endl;
	cout<<&quot;Octal: &quot;<<oct<<value<<endl;
	cout<<&quot;******************************************\n&quot;;
	
	}

	return 0;



disregard the binstruct stuff.  The basics of what you need are there.

Matt
 
Hi,
I realize this is the C++ forum but SCANF still works.

scanf(&quot;%x&quot;,&num);

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top