Thread: Multiply it by 10 then Int to Hex

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    15

    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 "readIn.h"
    #include "hexToInt.h"
    #include "intToHex.h"
    #define BUFFER_SIZE 9
    int main()
    {
    unsigned char *n;
    unsigned int temp;
    n=malloc(BUFFER_SIZE);
    
    printf("Enter a Hexadecimal >> ");
    fflush(stdout);
    
    readIn(n, BUFFER_SIZE);
    
    printf("%d\n",hexToInt(n));
    /*i can't do n*=10 */
    printf("%x\n",intToHex(n));
    fflush(stdout);
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I can't do a n*=10 ??
    Unless there's something odd in your assignment, it seems the best approach
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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))) {
        printf("Enter a Hexadecimal >> ");
        scanf("%x", &temp);
    
        printf("%x", temp*10);
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    15

    Question

    Quote 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))) {
        printf("Enter a Hexadecimal >> ");
        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. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    n+n+n+n+n+n+n+n+n+n?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There's a couple of things that I really don't like about this assignment:

    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?
    Last edited by itsme86; 09-23-2004 at 02:54 PM.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    edit
    Last edited by Mr. Acclude; 09-23-2004 at 08:06 PM.

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    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. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    Quote 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM