Some help in my code
I''m preparing for an internship position and this is the most often asked question, can someone help me to move further.
--C program to convert a base 16 number(23DA) to its base 10, without using the standard C libraries or scanf().The program should allow the user to input the number.
I''m having trouble compiling in MS Visual compiler.
I did the following:
char hex[]="23DA";
int i, base10;
for ( i = 0 ; i < 4 ; i++ )
{
char ch = hex;
if ( ch >= ''0'' && ch <= ''9'' ) printf("Value of ch=%c is %d\n", ch, ch-''0'' );
if ( ch >= ''A'' && ch <= ''F'' ) printf("Value of ch=%c is %d\n", ch, ch-''A''+10 );
base10 = (hex[1]* 16^3) + (hex[2]* 16^2) + (hex[3]* 16^1) + (hex[4]* 16^0);
printf("the number in decimal is%d", base10);
}
Array indices are 0-based so you need to be referring to hex[0], hex[1], hex[2] and hex[3] (the value of hex[4] will be 0 as it''s the terminator on the string).
Iain HutchisonProgrammer, Silicon DreamsThe views expressed here are my own, not those of my employer.
yes, thanks, point noted pieman, but that still doesn''t seem to help fix it.
char ch = hex; should be char ch=hex;
edit:
hmm i can't post between [
you should index it
char ch=hex;<br><br><br><br><br><br><SPAN CLASS=editedby>[edited by - Pilatus on February 21, 2003 10:32:05 AM]</SPAN> </i> <br><br><SPAN CLASS=editedby>[edited by - Pilatus on February 21, 2003 10:34:19 AM]</SPAN>
edit:
hmm i can't post between [
you should index it
char ch=hex;<br><br><br><br><br><br><SPAN CLASS=editedby>[edited by - Pilatus on February 21, 2003 10:32:05 AM]</SPAN> </i> <br><br><SPAN CLASS=editedby>[edited by - Pilatus on February 21, 2003 10:34:19 AM]</SPAN>
#include <stdio.h>
int main()
{
char hex[]="23DA";
int i, base10;
for(i = 0; i < 4; i++)
{
char ch = hex;
if ( ch >= ''0'' && ch <= ''9'' ) printf("Value of ch=%c is %d\n", ch, ch-''0'' );
if ( ch >= ''A'' && ch <= ''F'' ) printf("Value of ch=%c is %d\n", ch, ch-''A''+10 );
}
base10 = (hex[0]* 16^3) + (hex[1]* 16^2) + (hex[2]* 16^1) + (hex[3]* 16^0);
printf("the number in decimal is%d", base10);
return 0;
}
int main()
{
char hex[]="23DA";
int i, base10;
for(i = 0; i < 4; i++)
{
char ch = hex;
if ( ch >= ''0'' && ch <= ''9'' ) printf("Value of ch=%c is %d\n", ch, ch-''0'' );
if ( ch >= ''A'' && ch <= ''F'' ) printf("Value of ch=%c is %d\n", ch, ch-''A''+10 );
}
base10 = (hex[0]* 16^3) + (hex[1]* 16^2) + (hex[2]* 16^1) + (hex[3]* 16^0);
printf("the number in decimal is%d", base10);
return 0;
}
i think you are trolling us :-p
16^3 is xor not taking the power or whatever it's called in english :-p
Also you should put your real number back into your array not just display it.
I would recommend you not to pursue this internship but learn a bit more first!
[edited by - Pilatus on February 21, 2003 11:14:25 AM]
16^3 is xor not taking the power or whatever it's called in english :-p
Also you should put your real number back into your array not just display it.
quote:
I'm preparing for an internship position and this is the most often asked question
I would recommend you not to pursue this internship but learn a bit more first!
[edited by - Pilatus on February 21, 2003 11:14:25 AM]
Don''t know why I didn''t notice this first but you''re using the ^ operator as if it does power-of functionality. In C the ^ operator is the exclusive-or operator so 16^2 isn''t 256 but 18.
Try this, it should work for any length of string you through at it (up to 8 digits obviously) :
Try this, it should work for any length of string you through at it (up to 8 digits obviously) :
int Hex2Base10(const char* pHex){ int base10 = 0; const char* pChar = pHex; while (*pChar) { int digit; if ((*pChar >= ''0'') && (*pChar <= ''9'')) digit = *pChar - ''0''; else if ((*pChar >= ''A'') && (*pChar <= ''F'')) digit = 10 + *pChar - ''A''; else if ((*pChar >= ''a'') && (*pChar <= ''f'')) digit = 10 + *pChar - ''a''; else return -1; // error! base10 = base10 * 16 + digit; pChar++; } return base10;}
Iain HutchisonProgrammer, Silicon DreamsThe views expressed here are my own, not those of my employer.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement