How to convert INTEGER to STRING without using library functions??



HI ,


Please post the source code for converting int to string without using library functions!!


Rajesh

Comments

  • : Please post the source code for converting int to string without using library functions!!

    Blimey, aren't you allowed to use libraries??
    Convert the decimals to chars one by one...

    Good luck,
    bilderbikkel

  • :
    :
    : HI ,
    :
    :
    : Please post the source code for converting int to string without using library functions!!
    :
    :
    : Rajesh
    :
    [code]
    char revertedstr[32];
    char str[32];
    int length=0;

    while (number > 0)
    {
    int a = number % 10;
    str[length++] = a | '0';
    number /= 10;
    }

    length--;
    int rev = 0;
    while (length >= 0)
    {
    str[rev++] = revertedstr[length--];
    }

    str[rev] = '';
    [/code]

    All done by heart, in less than two minutes.
    Did you try anything yourself?



    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • : :
    : [code]
    : char revertedstr[32] ;
    : char str[32];
    : int length=0;
    :
    : while (number > 0)
    : {
    : int a = number % 10;
    [red] revertedstr[/red][length++] = a | '0';
    : number /= 10;
    : }
    :
    : length--;
    : int rev = 0;
    : while (length >= 0)
    : {
    : str[rev++] = revertedstr[length--];
    : }
    :
    : str[rev] = '';
    : [/code]
    :


    just one bug :)
  • : : :
    : : [code]
    : : char revertedstr[32] ;
    : : char str[32];
    : : int length=0;
    : :
    : : while (number > 0)
    : : {
    : : int a = number % 10;
    : [red] revertedstr[/red][length++] = a | '0';
    : : number /= 10;
    : : }
    : :
    : : length--;
    : : int rev = 0;
    : : while (length >= 0)
    : : {
    : : str[rev++] = revertedstr[length--];
    : : }
    : :
    : : str[rev] = '';
    : : [/code]
    : :
    :
    :
    : just one bug :)

    Thanx. I wonder if the original poster would have found that one.


    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • :
    :
    : HI ,
    :
    :
    : Please post the source code for converting int to string without using library functions!!
    :
    :
    : Rajesh
    :
    [code]
    #include
    #include

    void PrintBin(long);
    void PrintHex(long);
    void PrintOct(long);

    int main()
    {

    long x=0xf0f00f0f;
    PrintBin(x);
    PrintHex(x);
    PrintOct(x);



    system("PAUSE");
    return 0;
    }

    void PrintBin(long x)
    {
    static char binary_strings[16][5]={{"0000"},{"0001"},{"0010"},{"0011"},{"0100"},{"0101"},{"0110"},{"0111"},{"1000"},{"1001"},{"1010"},{"1011"},{"1100"},{"1101"},{"1110"},{"1111"}};

    short int index, offset, count=28;

    long nybble=0x0f;

    printf("%i or %u in binary

    ", x, x);

    while(count!=-4)
    {
    index=nybble & (x>>count);
    for(offset=0; offset<4;offset++)
    printf("%c", binary_strings[index][offset]);
    printf(" ");
    count-=4;
    }
    printf("

    ");
    }

    void PrintHex(long x)
    {
    static char hex_string[]="0123456789ABCDEF";

    short int offset, count=28;
    long nybble=0x0f;
    printf("%i or %u in hexadecimal

    ", x, x);
    while(count!=-4)
    {
    offset=nybble &(x>>count);

    printf("%c", hex_string[offset]);
    if(count%16==0 & count!=0)
    printf(":");
    count-=4;
    }
    printf("

    ");
    }

    void PrintOct(long x)
    {
    static char octal_string[]="01234567";

    short int octal=0x07, count=30, offset;
    printf("%i or %u in octal is

    ", x, x);

    while(count!=-3)
    {
    if (count==30)
    offset=octal & ((x>>count) & 3);
    else
    offset=octal & (x>>count);
    printf("%c", octal_string[offset]);

    count-=3;
    }
    printf("

    ");
    }
    [/code]
    [green]
    Decimal conversions should be easy to do yourself.
    [/green]

  • : : Please post the source code for converting int to string without using library functions!!
    :
    : Blimey, aren't you allowed to use libraries??
    : Convert the decimals to chars one by one...
    :
    : Good luck,
    : bilderbikkel
    :
    :

    Somewhat old reply, but there are circumstances where libraries aren't an option. For example, writing a shell or operating system kernel. Neither of the above is always able to use libraries...

    Besides, it's good to know how your library works. Even if you never plan to use anything but that library, and you know what it does by heart, if you know how it works, you can better assess how to use it more effectively :)
    Sincerely,
    Jakykong (Jack Mudge)
    [email protected]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories