# Thread: Multiply it by 10 then Int to Hex

1. ## Multiply it by 10 then Int to Hex

Write a C program that prompts the user to enter a hex value, muliplies it by ten, then displays the result in hex.

I already made a fuction hexToInt and intToHex, but I am still puzzled and how to mulitply it by 10. After I read in the string then I think I should convert the hex to int then mulitply it by 10 the convert the int back to hex.

I can't do a n*=10 ??

Code:
```#include <stdio.h>
#include "hexToInt.h"
#include "intToHex.h"
#define BUFFER_SIZE 9
int main()
{
unsigned char *n;
unsigned int temp;
n=malloc(BUFFER_SIZE);

fflush(stdout);

printf("%d\n",hexToInt(n));
/*i can't do n*=10 */
printf("%x\n",intToHex(n));
fflush(stdout);
return 0;
}```

2. > I can't do a n*=10 ??
Unless there's something odd in your assignment, it seems the best approach

3. Ok first off there is a c standard function that converts hex strings to an integer, strtol(). Secondly, whats the problem? Hex is just a way you and I read a number. To the computer hexadecimal, decimal, binary, octal, etc. are all the same. Your code could simply be this:

Code:
```#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 9
int main()
{
unsigned char *n;
unsigned int temp;

if((n=malloc(BUFFER_SIZE))) {
scanf("%x", &temp);

printf("%x", temp*10);
return 0;
}```

4. Originally Posted by master5001
Ok first off there is a c standard function that converts hex strings to an integer, strtol(). Secondly, whats the problem? Hex is just a way you and I read a number. To the computer hexadecimal, decimal, binary, octal, etc. are all the same. Your code could simply be this:

Code:
```#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 9
int main()
{
unsigned char *n;
unsigned int temp;

if((n=malloc(BUFFER_SIZE))) {
scanf("%x", &temp);

printf("%x", temp*10);
return 0;
}```

Acutally it can't I have to create a function that turns a hex to int and int to hex. Here is the project.
http://www.cs.sonoma.edu/~stauffer/2....Project1.html

I have everything done, now i just need to muplitply it by 10.

5. Silly me. Anyways, yeah what Salem said!!!

Here is where I am confused. Why can't you have a line that says n *= 10;

If anything why not just do this:

Code:
`printf("%x\n",intToHex(n*10));`
wait...what is the prototype for intToHex()? Shouldn't it be returning a string? Again hex is an abstract concept that you and I use. To the computer 10 is 10 whether you write it as 1010 or 0x0A or 012. If intToHex() is returning an int, then I think you are not understanding this concept.

Unless of course your code is:

Code:
```int intToHex(int i) {
return i;
}```

6. n+n+n+n+n+n+n+n+n+n?

7. lmao! I was going to say that and thought it may come across as too sarcastic. I honestly don't think he is stating his actual problem. No where in his assignment did it say he was not allowed to do n *= 10 or someFunction(n*10). He has just said he can't do it...like he is physically unable to type it.

8. n *= 10 doesn't give him the correct answer is what he means. Because n is a pointer to type char, not an integer. So n *= 10 is the same as saying multiply the address stored in n by 10 (i.e. 0x12345678 * 10).

The assignment expects the program to wait for the user to type a number in hex format. Then pass the user's input to hexToInt(). Then multiply the return value of hexToInt() by 10. Then pass that product to intToHex() and print the string that intToHex() produces.

9. Something like this I imagine:
Code:
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>

unsigned int hexToInt(char *str)
{
return (unsigned int)strtol(str, NULL, 16);
}

int intToHex(unsigned int num, char *buf)
{
return sprintf(buf, "%x", num);
}

int main(void)
{
char buf[256];
unsigned int num;

printf("Hex number: ");
fflush(stdout);

fgets(buf, sizeof(buf), stdin);
if(*buf && buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = '\0';

num = hexToInt(buf);
intToHex(num*10, buf);
printf("%s\n", buf);

return 0;
}```
The whole meat of the program is in 3 bold lines. I mean how do you point someone in the right direction when there's so little to do? The project link explicitly states what's passed to and returned from each function and it looks like the conversion functions are already written out for you. Even reading in the user input is done for you. The only thing to know how to do is call a function.

1) It's a ........-poor example of when to use low-level input/output. Why is that outlined as one of the learning goals?
2) intToHex() is a insufficient in its design. At the very least it should accept a 3rd argument that says how large the buffer is so you can prevent an overflow.

I don't know...to me it seems like they said "well they need to learn low-level I/O and they need to do I/O here so let's throw it into the mix.", but I think it's better to teach someone how to do something in a practical situation where you'd actually want to use it so the student can say "Oh! I see why and where this is advantageous!" FILE streams would be easier (read: better) to use in this situation I think.

The same goes with the intToHex() function. If the instructor supplied it then it should be up to snuff. But if the student is expected to write it then I can see the instructor holding off on things like explaining how and why buffer overflows are dangerous.

Maybe I'd make a horrible teacher though so who knows?

11. edit

12. You're not even using the functions correctly. Why did I bother posting example code if you're not going to read it? I'm not writing this stuff for my own benefit; I already know how to do it.

13. I did read your code, but i need to use the functinos that I have. This is my first time programing with C, please give me a break =/

14. Originally Posted by Mr. Acclude
This is my first time programing with C, please give me a break =/
Is it your first time thinking too? I swear to god everyone is stupid here today.

Quzah.

15. Originally Posted by quzah
Is it your first time thinking too? I swear to god everyone is stupid here today.

Quzah.
I been only programing for only 2 weeks, gives us a break you ass.