Reputation: 235
Assuming that I have an variable like:
int n = 23;
it is possible split it, and convert to:
int x = n ?? ??; //2
int y = n ?? ??; //3
have no idea how to do this. Any help is very appreciated. Thanks in advance.
Upvotes: 1
Views: 866
Reputation: 16389
different approach using libc library. .. there are others as well.
int *
val2arr(int *arr, const int val)
{
char tmp[32]={0x0}; // more than digits in LONG_MAX on 64 bit
char *p=tmp;
int *i=arr;
sprintf(p, "%d", val);
for(; *p; i++, p++ )
*i=*p - 48;
*i=-1; // mark end with -1
return arr;
}
Upvotes: 0
Reputation: 3125
You can use a loop to grab each value. You'll have to keep track of x
here differently of course, but I think this will work for you.
while (n != 0)
{
x = n % 10;
n = n / 10;
}
Upvotes: 1
Reputation: 993183
It is not necessary to use bit operators for this. In fact, since bit operators work with the binary representation of numbers, they're generally no good for base 10 calculations.
int n = 23;
int x = n / 10;
int y = n % 10;
Upvotes: 7