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??

2. 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.

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. 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".

5. 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. Originally Posted by yezaim
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. Originally Posted by laserlight
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. 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.

9. Originally Posted by laserlight
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. Anyway, my hack is not cunning enough: I did not account for the initial '0' being read without causing a failed read.

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.

11. Originally Posted by laserlight
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. 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.

13. Originally Posted by laserlight
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. Originally Posted by laserlight
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. 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.