# Thread: Integer to String - C

1. ## Integer to String - C

Hi all,
I am trying to write a function which takes an integer and converts it into it's equilvalent string value. Now the integer has been encoding to the base 256.

for example:
let a=1
b=2
c=3 (just for the example)

abc = ((1)^2*256) + ((2)^1*(256)) + ((3)^0*(256))=1024

now this is correct and returns the correct value.....
does anybody know how i could reverse this operation to get these values out the number 1024....the above is working correctly in my program and i just would like to know if any of you had ideas...
thanks...: )

2. When converting 10-base to 2-base, this is done as follows.

23 mod 2 = 11 left 1
11 mod 2 = 5 left 1
5 mod 2 = 2 left 1
2 mod 2 = 1 left 0
1 mod 2 = 0 left 1

So 23 (base10) is equal to 10111 (base2). This algorithm can be generalized as follows:

Let N be a random integer value with base 10. Let B be the new base. Let R be an array which contains the converted integer. Then the algorithm could like like something like this in C:

Code:
```i = 0;
while (N != 0)
{
M = N / B;
R [i++] = N % B;
N = M;
}```

3. ## Confused

Hi,

Are you trying to convert 123 base(256) to a decimal number?

That is

1 times 256 raised to the 2nd power plus
2 times 256 rasied to the 1st power plus
3 times 256 rasied to the 0 power

If so that is 66,051.

Please be clearer as to what you are trying to do.

BTW:

^ is the bitwise XOR operator

4. Well, without relying on a toolbox routine, I guess my initial thought is that your name is base 10, so in order to isolate each value, you divide by 10. This is purely brute force, I know this could be done more elegantly. But it's a start.

The below deals with _positive_ numbers only. We'd have to keep track of the sign value, otherwise (not hard, just left as an exercise for the reader (grin)).

Code:
```char   asciiVals[] = {'0','1','2','3','4','5','6','7','8','9'};
char   asciiEquivalent[12];      /* big enough for any reasonable number */

void num2str(int);           /* prototype */

void num2str(int a)
{
int     scratch;
int     index;
int     len;

asciiEquivalent[0] = 0x00;       /* init string */

/* this would also be where we would keep track of sign value */

if(!a)
{
asciiEquivalent[0] = '0';        /* it's a zero, bail early */
asciiEquivalent[1] = 0x00;

return;
};

len = 0;
scratch = a                  /* determine # of digits */
while(scratch)
{
scratch /= 10;
len++;
};

asciiEquivalent[len] = 0x00;  put terminator byte into string */
len--;

scratch = a;                 /* make a copy of original value */
while(scratch)              /* divide and conquer until zero */
{
/* you could modulo the next, but this shows the work */

scratch /= 10;           /* knock of least significant digit */
index = (a-scratch);  /* determine what got knocked off */

asciiEquivalent[len] = asciiVals[index];  /* build the string */
len--;
};
}```
enjoy.

5. sorry,
I use ^ to be "raised to the power of"...thanks for the replies....but it is more of a mathematical problem than a programming problem....

I'm trying to write a recursive function which does not eat up stack space...

I'm basically trying to reverse the operation I detailed in my initial post...

if: ^=raised to the power of

9*256^0 = 9
4*256^1 = 256
73*256^2= 458752 when we add the results we get 459785...

now I want to get 9,4 and 73 back from this above number 459785..
I've been trying to find some way of relating the length of the result to the highest power and then use mod 256^of that power...but I cant find any pattern with that....

i've been at this all day but with no success...If any of you have any ideas for me I would be grateful...
thanks

6. OK - try this (I'm not in a code-writing mood)
i = number (4589785)
i/256 = one answer
i%256 = new i
Repeat until new i == old i

(Forgive errors - I'm really tired)

7. Thanks...
but I tried that...4589785/256=17928.8.... but that is not one of the numbers...

8. >I'm trying to write a recursive function which does not eat up >stack space...

Hmmm, is that possible?

The suggestion I gave can be implemented with recursion. Recursion consists of an end-step and a recursion-step, I'm not sure if these are the correct English terms.

The end-step is N == 0. And the recursion step is N = M. So the iterative algorithm could be rewritten like:

Code:
```void printbase (int B, int N)
{
int M, R;

if (N != 0)
{
M = N / B;
R = N % B;

printf ("N %d M %d R %d\n", N, M, R);

printbase (B, M);
}
}```

9. yeah it is possible...i'm trying to implement RSA through the Glasgow Haskell Interpreter...

I know this is not the place for Haskell but it is a pure maths problem which I'm sure some of you have come across before...if any you could give me some advice on how to find how to relate the highest power within the number to the length of it I would be thankful....

10. > but I tried that...4589785/256=17928.8.... but that is not one of the numbers...

Oh - right... Stupid cheez... I told you I was tired

11. Govtcheez has the correct idea. Also Instead of dividing by 256, I guess you could instead do a shift right eight.

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
long a = 4785161;
long b, digit[10];
int num_of_digits, i;

b = a;
num_of_digits = 0;
while (b > 0)
{
digit[num_of_digits++] = b % 256;
b /= 256;
}
for (i=num_of_digits-1; i>=0; i--)
printf("digit[%d]=%d\n",i,digit[i]);
return 0;
}

12. By the way,

(1,2,3) (base 256) = 66051 decimal (as Pappy explained above)
(4,0) (base 256) = 1024 decimal

13. I should point out that Shiro and Govtcheez have the same idea.

14. Hi again,

Below is code that will give the "digits" base 256 of a base 10 number. Digits is in quotes because it really gives an int from 0 to 255. Since it woulb be difficult to come up with 256 meaningful symbols the int is the best way to go.

#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
unsigned long int numin;
double x,y;
int digts;

printf("\nEnter Number ");
scanf("%lu",&numin);
printf("\n");
for(x = 4 ; x >= 0; --x )
{
y = pow(256,x);
digts = (int)(numin /y);
numin = numin -(digts * y);
printf("%d ",digts);
}

getch();// Holds program open if using Borland
return 0;

}

BTW No Error or value checking is included. The 4th power is about as high as we can go in 16 bit compilers.
Hope this helps.

15. ## Create your own stack

> I'm trying to write a recursive function which does not eat up stack space...

Why didn't you say so in the first place-- that's your real problem. Well, if you don't want to use up system stackspace, then create your _own_ stack. It's very easy to do. All you require is two pointers and a ramblock.

If security is an issue, you can even encrypt your stack and decrypt it on the fly.l

This allows you recursion without using the system stack.

-----

Now, if you know how many times the iteration will be done (if it's a specific number of times), you could use an array, or change the access to be more effecient.

enjoy

Popular pages Recent additions