# Thread: Hex to Dec Problem

1. ## Hex to Dec Problem

I tried to use writeStr to print out if the Hex converted to a decimal but all I get is the input i passed thru strToInt.

for example i chose A and it prints out an A but i wanted to print out a 10

Code:
```#include <stdio.h>
#include "writeStr.h"
#include "strToInt.h"

#define BUFFER_SIZE 33
#define HEX_SIZE 9
#define BIN_SIZE 33
#define DEC_SIZE 11
#define BASE_SIZE 2

int main()
{

char input[BUFFER_SIZE];
char base[2];
unsigned int x;

writeStr("\nEnter the base of the value to be converted");

if(base[0] =='d' || base[0] =='D')
{
writeStr("Enter the Decimal Value: ");
}

else if(base[0] =='h' || base[0] =='H')
{
writeStr("Enter the Hex Value: ");
}
else if(base[0] =='b' || base[0] =='B')
{
writeStr("Enter the Binary Value: ");
}
else
{writeStr("Invalid Input - Goodbye");}

strToInt(input, base[0]);
writeStr(input);

return 0;
}```

Code:
```int strToInt(char *string, int base)
{
int decimal_val;
int result = 0;
int i;

if(base == 'h' || base =='H')
{

/* Goes through each value of string*/
for ( i = 0 ; string[i] != '\0' ; i++ )
{
/* The conversion */
decimal_val =
'0' <= string[i] && string[i] <= '9' ?
string[i] - '0' :
'a' <= string[i] && string[i] <= 'f' ?
string[i]- 'a' + 10 :
'A' <= string[i] && string[i] <= 'F' ?
string[i]- 'A' + 10 :
0;

result = result * 16 + decimal_val;
}

}
return result;

}```

2. Originally Posted by gqchynaboy
I tried to use writeStr to print out if the Hex converted to a decimal but all I get is the input i passed thru strToInt.

for example i chose A and it prints out an A but i wanted to print out a 10

Code:
```int main()
{

strToInt(input, base[0]);
writeStr(input);

}```
strToInt() returns the integer value of the input string. You don't use this value; instead you merely print the input string. And you were expecting ???

Regards,

Dave

3. Originally Posted by Dave Evans
strToInt() returns the integer value of the input string. You don't use this value; instead you merely print the input string. And you were expecting ???

Regards,

Dave
Code:
```int strToInt(char *string, int base)
{
int decimal_val;
int result = 0;
int i;

if(base == 'h' || base =='H')
{

/* Goes through each value of string*/
for ( i = 0 ; string[i] != '\0' ; i++ )
{
/* The conversion */
decimal_val =
'0' <= string[i] && string[i] <= '9' ?
string[i] - '0' :
'a' <= string[i] && string[i] <= 'f' ?
string[i]- 'a' + 10 :
'A' <= string[i] && string[i] <= 'F' ?
string[i]- 'A' + 10 :
0;

result = result * 16 + decimal_val;
}

}
return result;

}```
I thought it will convert a Hex to a decimal and return the decimal integer..

4. Originally Posted by gqchynaboy

I thought it will convert a Hex to a decimal and return the decimal integer..
It does. That is, it converts a string of characters representing hex digits to an integer value.

So how do you expect to display the value? (What do you do with the integer value returned to main()?)

Regards,

Dave

5. Code:
```unsigned int i;

printf("Enter a base 10 positive integer: ");
scanf("%u",&i);
printf("The value of %u in base 16 is %x",i,i);

----------

printf("Enter a base 16 positive integer :");
scanf("%x",&i);
printf("The value of %x in base 10 is %u",i,i);```
why do so much work when its all already in the standard library? Check the info on scanf and printf for your implementation though, but those are pretty standard IIRC.

The question isn't retorical either, as I don't really know what you are doing, so this many not help.

6. Originally Posted by Xipher
Code:
```unsigned int i;

printf("Enter a base 10 positive integer: ");
scanf("%u",&i);
printf("The value of %u in base 16 is %x",i,i);

----------

printf("Enter a base 16 positive integer :");
scanf("%x",&i);
printf("The value of %x in base 10 is %u",i,i);```
why do so much work when its all already in the standard library? Check the info on scanf and printf for your implementation though, but those are pretty standard IIRC.

The question isn't retorical either, as I don't really know what you are doing, so this many not help.
we can't use scanf and printf......that is why we have to convert it ourselfs

7. Originally Posted by gqchynaboy
we can't use scanf and printf......that is why we have to convert it ourselfs
Ok, thats why I was asking.

After reading the other replies, the #1 problem I see, is you don't use your result at all from the coversion, your simply printing the input string. Now if you want to print the result, I suspect you will also need to convert the int value into a string, and print that.

8. Originally Posted by Xipher
Ok, thats why I was asking.

After reading the other replies, the #1 problem I see, is you don't use your result at all from the coversion, your simply printing the input string. Now if you want to print the result, I suspect you will also need to convert the int value into a string, and print that.
http://www.cs.sonoma.edu/~stauffer/2....Project1.html

"intToStr.c - This function takes three arguments, an unsigned int, the address of a char array, and a char indicating the string base. The string base argument is a 'd' for decimal, 'h' for hexadecimal, and 'b' for binary."

so the function woudl basically look something like this, then I would have to convert hex to string, dec to string, and binary to string? anywhere i can find some code/help

intToStr(int x, char *string, char base)

If i enter a hex how can i print it out to show a decimal? I call strToInt and convert it..but then I dont know where to go from there

Code:
```int main()
{

char input[BUFFER_SIZE];
char base[2];
unsigned int x;

writeStr("\nEnter the base of the value to be converted");

if(base[0] =='d' || base[0] =='D')
{
writeStr("Enter the Decimal Value: ");
}

else if(base[0] =='h' || base[0] =='H')
{
writeStr("Enter the Hex Value: ");
}

else if(base[0] =='b' || base[0] =='B')
{
writeStr("Enter the Binary Value: ");
}

else
{writeStr("Invalid Input - Goodbye");}

strToInt(input, base[0]);

writeStr("Input in decimal =");

writeStr(input);

/*intToStr(x, input, 'd');*/
return 0;
}```

9. I call strToInt and convert it..but then I dont know where to go from there

Are you even paying attention to what people are telling you? USE THE RETURN VALUE!

Code:
```int x;
...

x = strToInt( input, base[0] );
printf("The returned value is %d!", x );```
Quzah.

10. Originally Posted by gqchynaboy
[url]
"intToStr.c - This function takes three arguments, an unsigned int, the address of a char array, and a char indicating the string base. The string base argument is a 'd' for decimal, 'h' for hexadecimal, and 'b' for binary."
1. Use readstr() to get the string in input buffer.
OK; You've done that.

2. Use the buffer that you used in readStr() as an argument to strToInt() to convert to an int of whatever base the input is supposed to be.
OK; You've done that.

3. Use the value returned from strToInt() as an argument of intToStr(). The other argument is the base.
You haven't done that, in spite of gentle and not-so-gentle hints given to you.

4. Use the string in the buffer that was an argument to intToStr() as an argument to writeStr() so that you can print the result of the conversion to whatever base the output is supposed to be (decimal).

I think you have totally missed the point of step 3.

From my original post:
Originally Posted by Dave Evans

strToInt() returns the integer value of the input string. You don't use this value; instead you merely print the input string.
From my other post (about strToInt()):
Originally Posted by Dave Evans
it converts a string of characters representing hex digits to an integer value.

So how do you expect to display the value? (What do you do with the integer value returned to main()?)
Regards,

Dave

11. Originally Posted by quzah

Are you even paying attention to what people are telling you? USE THE RETURN VALUE!

Code:
```int x;
...

x = strToInt( input, base[0] );
printf("The returned value is %d!", x );```
Quzah.
I can't use printf, but writeStr so when i use writeStr("Decimal is %d", x); there are to many arguements of course

Code:
```#include <unistd.h>
#include "writeStr.h"

int writeStr(char *theString)
{
unsigned int length =0;

write(STDOUT_FILENO, theString,1);

while(*theString !='\0')
{
theString++;
length++;

write(STDOUT_FILENO, theString,1);
}

return length;
}```

12. Try to be a little imaginative:
Code:
```{
char buf[100];

x = strToInt( input, base[0] );
sprintf(buf, "The returned value is %d!", x );
writeStr(buf);
}```

13. Originally Posted by itsme86
Try to be a little imaginative:
Code:
```{
char buf[100];

x = strToInt( input, base[0] );
sprintf(buf, "The returned value is %d!", x );
writeStr(buf);
}```

Where not allowed to use sprintf or any other libraries just writeStr.

14. Well then I guess you'd better write yourself a function to change an integer into a string too, huh?

Quzah.

15. Originally Posted by gqchynaboy
Where not allowed to use sprintf or any other libraries just writeStr.
from my previous post:
Originally Posted by Dave Evans
Use the string in the buffer that was an argument to intToStr() as an argument to writeStr() so that you can print the result of the conversion to whatever base the output is supposed to be (decimal).
So, if you called intToStr(buf, 'd'), for example, maybe you could do something like this:

Code:
```  writeStr("The converted value is ");
writeStr(buf);
writeStr("\n");```
Or am I just being dense?

Regards,

Dave