Thread: what datatype do I assign hexadecimal input

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    33

    what datatype do I assign hexadecimal input

    The user inputs a hexadecimal number. What data type do I declare the user's input to be...string? or...?


    I'm going to be converting it to the corresponding decimal place number.

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    So why not input it as an integer?

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    is that OK?? I thought it wouldn't be because hexadecimals include a,b,c,d, and e. But I don't know, is it OK?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One way is to input it as an integer. Input it as hexadecimal with the hex specifier.
    Code:
    int num;
    cout << "Enter number in hex:";
    cin >> hex >> num;

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    Thanks to both of you! That makes things so much easier.

    Also, if it's not too much to ask, I was curious to know whether I need to know how many digits/characters the hexadecimal number( user input) is and how to navigate each character...is there something for integers like inputstr[] for strings?

    I was thinking of using each chracter place value a power of 16 multiplied by the corresponding placevalue and summing those
    Last edited by m712; 11-17-2002 at 01:48 AM.

  6. #6
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Originally posted by swoopy
    One way is to input it as an integer. Input it as hexadecimal with the hex specifier.
    Code:
    int num;
    cout << "Enter number in hex:";
    cin >> hex >> num;
    Cool! What other 'specifiers' are there?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I learned the hex specifier from another thread here. I knew you could use it for output, but didn't know you can also use it for input.

    Well, there's dec (for decimal which is the default of course), oct, and hex. No bin.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It depends on the machine how long an int is. You can use sizeof(int) to find out. On my computer it's 4 bytes and a long is also 4 bytes. So to print the whole int in hex, you can use printf().
    Code:
       int num;
       cout << "Enter number in hex:";
       cin >> hex >> num;
    
       cout << "num:" << num << endl;
       printf("%08X\n",num);
    You can also store the hex number as a string with sprintf().
    Code:
       char buf[20];
       sprintf(buf,"%08X",num);
       cout << buf << endl;

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    I'm so sorry, I made a mistake; the program works correctly. I am going to delete that post.

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Just a thought: Why on earth doesn't the standard specify a bin modifier?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    It mght have something to do with the fact that negative binary numbers are represented differently than negaitve numbers in other bases.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Just a thought: Why on earth doesn't the standard specify a bin modifier?
    If you can find a way to portably do it then we'd be glad to hear it. You have to consider unsigned values, obscure signed representations as well as sign-magnitude, one's complement, and two's complement, positive and negative values, big-endian, little-endian, mixed-endian and other obscure endianness representations, the list goes on. Binary representations are just too varied to easily create that kind of functionality.

    -Prelude
    My best code is written with the delete key.

  13. #13
    666
    Guest
    Surely if the cout statement knew the data type and the storage of that particular type i.e. signed or unsigned it would be able to call the appropriate binary function to deal with that ie.

    Code:
    int main(void)
    {
      unsigned char Num = 125;
    
      cout << bin << Num;
    
      return 0;  
    }
    There maybe some standards that would need to be adopted before it was fully implemented, i.e. whether to use 2's complement or a bias etc to display numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. reading input without knowing datatype
    By aldajlo in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 05:53 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM