6 Replies - 3716 Views - Last Post: 30 July 2012 - 06:29 AM Rate Topic: -----

#1 m*pasha  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 17-July 12

char string to decimal value?

Posted 29 July 2012 - 07:05 AM

this is a simple program found in some excercise of K&R C book that takes character string which is supposed to be a hex number/code must be preceded by a 0x or 0X also a-f and A-F both must be supported.
the program just compiles ok but it just doesn't work i don't know where is the fault.Can someone help me out???
output looks like this:
Enter the hex value:
hex value must be preceded by 0x or 0X
0x03f
error:invalid hex value

/*htoi() converts char string to a decimal value */
#include<stdio.h>
#include<stdlib.h>
int ctoi(int,int);
int main()
{ int i = 0,p = 0,dec = 0,res = 0;
int s[8];
	printf("Enter the hex value:\nhex value must be preceded by 0x or 0X\n");
	while((s[i]=getchar())!='\n'){
		i++;
	}
	if(s[0] =='0')
	{
      if((s[1]=='x')||(s[1]=='X'))
      {
		while(i>=2)
		{
		res = ctoi(s[i],p);
		dec = dec + res;
		p++;
		i--;
			}
		printf("decimal value of hex number is : %d\n",dec);
		  }
      else{printf("hex value must begin with 0x or 0X\n");getchar();exit(0);}		
	}	
	else{printf("hex value must begin with 0x or 0X\n");getchar();exit(0);}
	getchar();
	return 0;
	
}
	
int ctoi(int c ,int p)
{ int n = 0;
	if(c >='A'&&c<='F')
	{ n = c - 55;while(p>=1){n = n*16;p--;} }
	else if (c>='a'&&c<='f')
	{n = c-85;while(p>=1){n = n*16;p--;}}
	else if(c>='0'&&c<='9')
	{n=c-48;while(p>=1){n = n*16;p--;}}
	else{printf("error:invalid hex value");exit(0);}
	return n;
}




Is This A Good Question/Topic? 0
  • +

Replies To: char string to decimal value?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: char string to decimal value?

Posted 29 July 2012 - 07:15 AM

First you need to find an indentation style you like and use it consistently. Quit trying to put so many elements on one line, this will make following the logic and using the debugger extremely hard.

In your ctoi() function:
int ctoi(int c ,int p)
{  
   int n = 0;
   if(c >='A'&&c<='F')

You defined your function to take two int parameters, yet you seem to be trying to use characters inside your function. If you want to work with characters in the function maybe you should pass characters into the function.

Jim
Was This Post Helpful? 2
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,625
  • Joined: 19-February 09

Re: char string to decimal value?

Posted 29 July 2012 - 06:01 PM

What value of i is being used to pass the character?


09	    while((s[i]=getchar())!='\n'){
10	        i++;
11	    }




input =
[0][x][1][\n]
i=3
s[3]=[\n]


Was This Post Helpful? 1
  • +
  • -

#4 m*pasha  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 17-July 12

Re: char string to decimal value?

Posted 30 July 2012 - 12:01 AM

View Postjimblumberg, on 29 July 2012 - 07:15 AM, said:

First you need to find an indentation style you like and use it consistently. Quit trying to put so many elements on one line, this will make following the logic and using the debugger extremely hard.

In your ctoi() function:
int ctoi(int c ,int p)
{  
   int n = 0;
   if(c >='A'&&c<='F')

You defined your function to take two int parameters, yet you seem to be trying to use characters inside your function. If you want to work with characters in the function maybe you should pass characters into the function.

Jim

Was This Post Helpful? 0
  • +
  • -

#5 m*pasha  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 17-July 12

Re: char string to decimal value?

Posted 30 July 2012 - 12:12 AM

Great! help decermenting
i
after the
while
loop fixed that ......
such lengthy codes are creepy and really hard to point out mistakes.
thanks #define and jimblumberg.
Was This Post Helpful? 0
  • +
  • -

#6 busyme12srv  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 18-June 12

Re: char string to decimal value?

Posted 30 July 2012 - 12:42 AM

Check this part of the code first:

if(c >='A'&&c<='F')  

     { n = c - 55;while(p>=1){n = n*16;p--;} }  

    else if (c>='a'&&c<='f')  

    {n = c-85;while(p>=1){n = n*16;p--;}}  

    else if(c>='0'&&c<='9')  



All if statements with logical operators should be properly bracketted.

eg. if(c >='A'&&c<='F')

should be:

if ((c >='A')&& (c<='F'))



This is because && has the highest priority in precedence list of operators.
Correct all the If .. stmts in the code.

:code:

This post has been edited by jimblumberg: 30 July 2012 - 06:31 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? -1
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: char string to decimal value?

Posted 30 July 2012 - 06:29 AM

busyme12srv
You have enough posts that you should know how to use code tags. Use them when posting code!!!

Quote

All if statements with logical operators should be properly bracketted.

eg. if(c >='A'&&c<='F')

should be:

if ((c >='A')&& (c<='F'))


There is nothing actually wrong with the first section of code. Parentheses surrounding the individual clauses are not required, recommended maybe, but not required. A little white space wouldn't hurt but that too is not a requirement.

if(c >='A'&&c<='F')  // Could use consistent spacing between elements.
if(c >= 'A' && c <= 'F')// IMO this is easier to read.



Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1