Thread: Integer to String - C

  1. #1
    Unregistered
    Guest

    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. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    27

    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
    Pappy
    You learn something new everyday.

  4. #4
    Sayeh
    Guest
    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. #5
    Unregistered
    Guest
    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. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    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. #7
    Unregistered
    Guest
    Thanks...
    but I tried that...4589785/256=17928.8.... but that is not one of the numbers...

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >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. #9
    Unregistered
    Guest
    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. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > 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. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    By the way,

    (1,2,3) (base 256) = 66051 decimal (as Pappy explained above)
    (4,0) (base 256) = 1024 decimal
    Last edited by swoopy; 02-08-2002 at 04:50 PM.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I should point out that Shiro and Govtcheez have the same idea.

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    27
    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.
    Pappy
    You learn something new everyday.

  15. #15
    Sayeh
    Guest

    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 subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Integer to string?
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 06:13 AM
  3. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM