Tuesday, July 14, 2009

Anyone can help me to make a "C language" program?

1. To convert hexadecimal to decimal


Suppose the input is :"4FDA12"


And the output should be:"5233170"


Suppose the input is :"F"


And the output is:"15"


2. To convert decimal to hexadecimal


Suppuse the input is "5233170"


And the output is "4FDA12"


Suppose the input is "15"


And the output is "F"





Please let me know how it work and how is the algorithm. I try to make it for 3 days but i confuse. I have a problem with how we save tomporaty the number and the counter to count how many digit is the output.


I still learn C language and i dont understand C++





Could someone please explain me and help me to build the 2 programs. I need the source code to improve my Algorithm skill.


:)





Thanks for ur help.


Good luck. Have a nice weekend..

Anyone can help me to make a "C language" program?
Just use strtoul to convert from string to integer, and sprintf


to convert from an interger back to a string.





Example 1:


val = strtoul(hexstr, NULL, 16); /* hex string to interger */





Example 2:


val = strtoul(decstr, NULL, 10); /* dec string to integer */





Example 3:


sprintf(hexstr, "%X", val); /* integer to hex string */





Example 4:


sprintf(decstr, "%d", val); /* integer to dec string */








If you're doing this for a class, then the instructor might have told


you that you are not allowed to use functions like strtoul and sprintf to do the conversion. In that case, you can do something like this...





// the dots at the beginning of each line are there because


// yahoo removes leading spaces





unsigned int hexstr2uint(char *hexstr)


{


. . unsigned int val = 0;


. . int temp;


. .


. . if (hexstr != NULL)


. . . . for( ; *hexstr ; hexstr++)


. . . . {


. . . . . . temp = hexchr2int( *hexstr );


. . . . . . if (temp == -1) /* stop if we reach an invalid char */


. . . . . . . . break;


.


. . . . . . val *= 16;


. . . . . . val += (unsigned int) temp;


. . . . }


.


. . return val;


}





You should be able to write the hexchr2int function, and


the decstr2uint function will be similar to the one I've shown


above.





To convert an integer to a string w/o using standard C functions...





char *uint2hexstr(unsigned int val, char *outbuf)


{


. . int digits[32], ndigits = 0, i = 0 ;


.


. . do


. . {


. . . . digits [ ndigits++ ] = val % 16;


. . . . val /= 16;


. . }


. . while(val %26gt; 0);


.


. . for ( i = 0; i %26lt; ndigits ; i++ )


. . . . outbuf [ i ] = int2hexchr(digits [ ndigits - i - 1 ]);


.


. . outbuf [ ndigits ] = '\0'; // null terminate


.


. . return outbuf;


}





again, you can write the int2hexchr function, and the


decimal version will be similar.
Reply:Is this your homework? You haven't posted your approach to solve the problem. If you do that I might be able to tell you where you are wrong. Anyway you take see for the algorithm at http://www.permadi.com/tutorial/numDecTo...





--


Ck


http://www.gfour.net


No comments:

Post a Comment