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

note that input cannot be taken as string...its taken input as an integer...

The computer doesn't store the number in decimal form. It just displays it that way. Also your input isn't being converted into the internal binary number either, unless you call a conversion function to do so. How are you getting the input (command-line, cin, fread, gets, ReadFile, text box)? You'll just want to inspect the string you get, which actually is an array just like you're wanting (although you may have to look for and ignore the '0', 'x' at the beginning).

Of course you can also convert an integer into a series of hexadecimal digits (with e.g. sprintf and %x). But the integer variable has no information on whether it originally was in decimal or hexadecimal, had leading zeros, had commas, or any other sort of information about the string representation.

The computer doesn't store the number in decimal form. It just displays it that way. Also your input isn't being converted into the internal binary number either, unless you call a conversion function to do so. How are you getting the input (command-line, cin, fread, gets, ReadFile, text box)? You'll just want to inspect the string you get, which actually is an array just like you're wanting (although you may have to look for and ignore the '0', 'x' at the beginning).

Of course you can also convert an integer into a series of hexadecimal digits (with e.g. sprintf and %x). But the integer variable has no information on whether it originally was in decimal or hexadecimal, had leading zeros, had commas, or any other sort of information about the string representation.

dude 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

Taking a input of hexadecimal number;

int s = 0;
cin >> hex;  //set input to be taken as hexadecimal
cin >> s;

Printing hex;

int a = 15;
cout << hex; //set hex output
cout << a;

dude 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

Like I said, sprintf and %x (and make your array big enough to hold the trailing NUL.

But: do you mean
arr[5] = 'f';
or
arr[5] = 0x0f;
?

For the second version, just use bit manipulation since there are exactly four bits in a hexadecimal digit:
int place = 0;
while (x) {
arr[place++] = x & 0x0f;
x >>= 4;
}

Like I said, sprintf and %x (and make your array big enough to hold the trailing NUL.

But: do you mean
arr[5] = 'f';
or
arr[5] = 0x0f;
?

For the second version, just use bit manipulation since there are exactly four bits in a hexadecimal digit:
int place = 0;
while (x) {
arr[place++] = x & 0x0f;
x >>= 4;
}

hey dude...m sorry but the problem still persists..

according to ur code above the values stored in the array are actaully ascii values of that particular integer and not the values of the integer.

arr[0]-> holds ascii value of f(15)
arr[1]-> holds ascii value of f(15)
arr[2]->holds ascii value of a(15)...
.
.
. so on

if i declare arr[] as an integer arrray , then it holds the values in decimal form....
arr[0]=15;
arr[1]=15;
arr[2]=10.....
.
.
..so on..

so my problem is still unsolved!!!!!!

Make use of the standard functions given to you,without using that you'll never be able to exploit C to its full potential.

You can do this:
1) take the hex number as the input into a character array without the prefix "0x" or "0X" as :

char arr[10];
cout<<"Enter hex number without 0x or 0X prefix";
cin>>arr;

Now your objective of storing the different digits in different array indexes is accomplished.

2) When ever you want to use the hex number you can always do this :

long a;
a = strtol( arr , 0 , 16 );

Now you even have the decimal value of the hex number for your usage.

Make use of the standard functions given to you,without using that you'll never be able to exploit C to its full potential.

You can do this:
1) take the hex number as the input into a character array without the prefix "0x" or "0X" as :

char arr[10];
cout<<"Enter hex number without 0x or 0X prefix";
cin>>arr;

Now your objective of storing the different digits in different array indexes is accomplished.

2) When ever you want to use the hex number you can always do this :

long a;
a = strtol( arr , 0 , 16 );

Now you even have the decimal value of the hex number for your usage.

sorry....but the problem not solved yet....

the input has to be taken as an integer....not as a character array!!!!

You still haven't told us whether your goal is to get ASCII values or not.

Your "what I want" won't even compile, it is illegal syntax. I offered the two closest things which probably include the "right" way, but you didn't say which one.

You still haven't told us whether your goal is to get ASCII values or not.

Your "what I want" won't even compile, it is illegal syntax. I offered the two closest things which probably include the "right" way, but you didn't say which one.

hey...let me elaborate it further...

i take from console input two types of values decimal and hexadecimal..
Let's suppose these are...
int x1;
cin>>x1// x1=0x4e2aff or x1=12345


now i have to double each of these values and output the results in those particular forms.

(i.e)
cout<<x1; //x1= 9c55fe (2* 0x4e2aff) or 24690 (2* 12345)


now the problem is how do i make sure that on inputting a hexadecimal value i print out an hexadecimal value and similarly on inputting a dcemial value i print a decimal output...

note the input is only in the form of an integer and it can be randomly a hexadecimal or a decimal integer.

i hope that's clear enough now.... :)

int x1;
cin>>x1// x1=0x4e2aff or x1=12345

If you wan't to do this with int, and not a string, then it either has to
be a hex or a decimal, or octal.

int Num = 0;
	
	cin >> hex; //Tell cin to prepare to take in hex
	cin >> Num; //Get hex value
	cout <<  "Decimal value : "<<(2*Num) << endl; //Its decimal equivelance	
	cout << hex << showbase; //Tell cout to prepare to print in hex	
	cout <<  "Hexidecimal value : "<< (2*Num) << endl; //show in hex form

Taking a input of hexadecimal number;

int s = 0;
cin >> hex;  //set input to be taken as hexadecimal
cin >> s;

Printing hex;

int a = 15;
cout << hex; //set hex output
cout << a;

thanx buddy...
that was helpful...
well i have one more doubt...

the method you gave above is cool if i take input from stream...
what if i pass a decimal or hexadecimal integer to a function from my main function..and then have to decide in that function whether the argument is a decimal or hexadecimal value...

for ex:

int main()
{
int count=2;
int x[]={0x620058, 0x665eaf};// or int x[]={123,44};
func(x,count);
return 0;
}

the func is....

void func(int x[],int count)
{
int a;
for(int i=0;i<count;i++)
a+=x;
}

here , how do i make sure that the integers passed in an array to the func() are in decimal or hexadecimal form???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.