Thread: Hexadecimal Blues!!!!

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    11

    Question Hexadecimal Blues!!!!

    hi Guyz...
    need some help here...
    i want to input an integer but it can be either in hexadecimal form or in integer form.
    the problem is that i would like to store that input in that particular form and separate all hexadecimal values in to an array.

    for ex: if input is 0x3ff24e, then i want to create an array that stores the values 3,f,f,2,4,e in a an array of 6 variables.

    now the problem is, as soon as i input the above number it gets converted into decimal form and hence i am unable to create an array of hexadecimal values??
    so can someone please guide...

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    96
    read your input as a string (getline() would be useful here) and then process the string. If the first 2 characters of the string are 0x then you can easily extract the rest and store them in those 6 variables. If not then you can use a stringstream to easily convert the number to an int and store it that way.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    11

    Unhappy Reply

    the constraint is that the input cannot be taken as a string. had that been the case, it would have been pretty simple.

    but the input is only in the form of an integer.....

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ayan_2587
    the constraint is that the input cannot be taken as a string.
    Artificial constraints. Tsk. Are you allowed to read the input as characters?

    EDIT:
    By the way, if this is the exact wording of the instructions:
    either in hexadecimal form or in integer form
    Then the "either" and "form or in" is redundant since it obviously means "hexadecimal representation of an integer".
    Last edited by laserlight; 10-05-2009 at 01:13 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    why don't you simply read it and store it in a char** instead? you will have the array filled the way you want, dont you?

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    11
    Quote Originally Posted by yezaim View Post
    why don't you simply read it and store it in a char** instead? you will have the array filled the way you want, dont you?
    cant take it as an char**.
    it has to be read in as an integer.

    for ex:
    if i take it from a console input its got to be...

    int x;
    cin>>x;// where input is 0x6a2eff

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Artificial constraints. Tsk. Are you allowed to read the input as characters?

    EDIT:
    By the way, if this is the exact wording of the instructions:

    Then the "either" and "form or in" is redundant since it obviously means "hexadecimal representation of an integer".
    what i meant to say was...
    the input can either be a hexadecimal form or in decimal form...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, if you are only allowed to read in using formatted operator>> to an int, then I suspect that the problem either cannot be solved, or requires some cunning yet pointless hack. The problem is that you have no way to determine if the input should be read in decimal or hexadecimal representation. This is easy to decide if you could read in the first two characters separately: if you detect the "0x" prefix, you just set the hex flag and read.

    EDIT:
    Okay, an idea for a cunning yet pointless hack has just crossed my mind: read to the int variable in decimal. If the read fails, clear the error flags, then set the hex flag and read in as hex. If that fails, zap the user for entering invalid input.
    Last edited by laserlight; 10-05-2009 at 01:27 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Well, if you are only allowed to read in using formatted operator>> to an int, then I suspect that the problem either cannot be solved, or requires some cunning yet pointless hack. The problem is that you have no way to determine if the input should be read in decimal or hexadecimal representation. This is easy to decide if you could read in the first two characters separately: if you detect the "0x" prefix, you just set the hex flag and read.

    EDIT:
    Okay, an idea for a cunning yet pointless hack has just crossed my mind: read to the int variable in decimal. If the read fails, clear the error flags, then set the hex flag and read in as hex. If that fails, zap the user for entering invalid input.
    hey, i am still unclear!!

    let me show u a snippet....

    /* int x;
    x=0x4e2aff;
    char arr[6];
    */
    now what i want is to store these values at six locations in an array.
    i.e
    arr[0]=4;
    arr[1]=e;
    arr[2]=2;
    arr[3]=a;
    arr[4]=f;
    arr[5]=f;

    how to get this done????? remember x is an integer, not a string

    could u write the code nd explain?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Anyway, my hack is not cunning enough: I did not account for the initial '0' being read without causing a failed read.

    Quote Originally Posted by ayan_2587
    how to get this done????? remember x is an integer, not a string
    That part is easy. Recall how you would convert an integer in decimal representation to its constituent digits. The same approach works for hexadecimal, except that now you deal with 16 instead of 10.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Anyway, my hack is not cunning enough: I did not account for the initial '0' being read without causing a failed read.


    That part is easy. Recall how you would convert an integer in decimal representation to its constituent digits. The same approach works for hexadecimal, except that now you deal with 16 instead of 10.
    cud u pls write me the code....??

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ayan_2587
    cud u pls write me the code....??
    No. But yeah, I suggest just tackling this part first, and pretend that the user will always enter an integer in hexadecimal format for now.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    11
    Quote Originally Posted by laserlight View Post
    No. But yeah, I suggest just tackling this part first, and pretend that the user will always enter an integer in hexadecimal format for now.
    thats ur call....nywayz Thanx...

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    11
    Quote Originally Posted by laserlight View Post
    No. But yeah, I suggest just tackling this part first, and pretend that the user will always enter an integer in hexadecimal format for now.
    its still not working yaar!!

    dividing that no: by 16 gives a remainder that comes out in decimal form and not in a hexadecimal form. so i have to convert it into a hexadecimal form in order to store it...

    but i dont want to do that what i want is some method by which i can directly store the values...

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ayan_2587
    dividing that no: by 16 gives a remainder that comes out in decimal form and not in a hexadecimal form. so i have to convert it into a hexadecimal form in order to store it...

    but i dont want to do that what i want is some method by which i can directly store the values...
    You are mistaken. The remainder is an integer. You can represent that integer in decimal format, or you can represent that integer in hexadecimal format, or indeed in many other representations. If you choose to represent that integer in hexadecimal format, you presumably would map it to 0-9 and a-f (e.g., by using a switch or an array), which appears to be what you are trying to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-02-2007, 05:55 AM
  2. would you explain to me? hexadecimal to decimal
    By shteo83 in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 03:55 PM
  3. Converting Float to Hexadecimal
    By denizengt in forum C Programming
    Replies: 3
    Last Post: 09-28-2003, 06:11 PM
  4. hexadecimal
    By pierremk in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2002, 08:05 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM