one c question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nayak Abinash XN (AS/EAB)

    one c question

    how to use itoa() with example

  • Ramasubramanian XR (AS/EAB)

    #2
    Re: one c question

    On 2005-04-03 19:49, Nayak Abinash XN (AS/EAB) wrote:[color=blue]
    > how to use itoa() with example
    >[/color]
    perform well abinash

    Comment

    • Ramasubramanian XR (AS/EAB)

      #3
      Re: one c question

      On 2005-04-03 19:49, Nayak Abinash XN (AS/EAB) wrote:[color=blue]
      > how to use itoa() with example
      >
      > #include <stdio.h>
      > #include <stdlib.h>
      >
      > int main ()
      > {
      > int i;
      > char buffer [33];
      > printf ("Enter a number: ");
      > scanf ("%d",&i);
      > itoa (i,buffer,10);
      > printf ("decimal: %s\n",buffer);
      > itoa (i,buffer,16);
      > printf ("hexadecima l: %s\n",buffer);
      > itoa (i,buffer,2);
      > printf ("binary: %s\n",buffer);
      > return 0;
      > }[/color]


      Comment

      • Ramasubramanian XR (AS/EAB)

        #4
        Re: one c question

        On 2005-04-03 19:49, Nayak Abinash XN (AS/EAB) wrote:[color=blue]
        > how to use itoa() with example
        >[/color]


        #include <crtdll/errno.h>
        #include <crtdll/stdlib.h>
        #include <crtdll/internal/file.h>

        char *
        itoa(int value, char *string, int radix)
        {
        char tmp[33];
        char *tp = tmp;
        int i;
        unsigned v;
        int sign;
        char *sp;

        if (radix > 36 || radix <= 1)
        {
        __set_errno(EDO M);
        return 0;
        }

        sign = (radix == 10 && value < 0);
        if (sign)
        v = -value;
        else
        v = (unsigned)value ;
        while (v || tp == tmp)
        {
        i = v % radix;
        v = v / radix;
        if (i < 10)
        *tp++ = i+'0';
        else
        *tp++ = i + 'a' - 10;
        }

        if (string == 0)
        string = (char *)malloc((tp-tmp)+sign+1);
        sp = string;

        if (sign)
        *sp++ = '-';
        while (tp > tmp)
        *sp++ = *--tp;
        *sp = 0;
        return string;
        }


        char *
        ltoa(long value, char *string, int radix)
        {
        char tmp[33];
        char *tp = tmp;
        long i;
        unsigned long v;
        int sign;
        char *sp;

        if (radix > 36 || radix <= 1)
        {
        __set_errno(EDO M);
        return 0;
        }

        sign = (radix == 10 && value < 0);
        if (sign)
        v = -value;
        else
        v = (unsigned long)value;
        while (v || tp == tmp)
        {
        i = v % radix;
        v = v / radix;
        if (i < 10)
        *tp++ = i+'0';
        else
        *tp++ = i + 'a' - 10;
        }

        if (string == 0)
        string = (char *)malloc((tp-tmp)+sign+1);
        sp = string;

        if (sign)
        *sp++ = '-';
        while (tp > tmp)
        *sp++ = *--tp;
        *sp = 0;
        return string;
        }

        char *
        _ultoa(unsigned long value, char *string, int radix)
        {
        char tmp[33];
        char *tp = tmp;
        long i;
        unsigned long v = value;
        char *sp;

        if (radix > 36 || radix <= 1)
        {
        __set_errno(EDO M);
        return 0;
        }


        while (v || tp == tmp)
        {
        i = v % radix;
        v = v / radix;
        if (i < 10)
        *tp++ = i+'0';
        else
        *tp++ = i + 'a' - 10;
        }

        if (string == 0)
        string = (char *)malloc((tp-tmp)+1);
        sp = string;


        while (tp > tmp)
        *sp++ = *--tp;
        *sp = 0;
        return string;
        }


        Comment

        • pete

          #5
          Re: one c question

          Nayak Abinash XN (AS/EAB) wrote:[color=blue]
          >
          > how to use itoa() with example[/color]

          /* BEGIN new.c */

          #include <stdio.h>
          #include <limits.h>

          void itoa(int n, char *s);
          int fsput_d(int integer);

          int main(void)
          {
          fputs("INT_MIN is ", stdout);
          fsput_d(INT_MIN );
          putchar('\n');

          return 0;
          }

          int fsput_d(int integer)
          {
          char string[CHAR_BIT / 2 * sizeof integer];

          itoa(integer, string);
          return fputs(string, stdout);
          }

          void itoa(int n, char *s)
          {
          int tenth, min_offset;
          char swap, *p;

          min_offset = 0;
          if (0 > n) {
          if (-INT_MAX > n) {
          ++n;
          ++min_offset;
          }
          n = -n;
          *s++ = '-';
          }
          p = s;
          tenth = n;
          do {
          tenth /= 10;
          *p++ = (char)(n - 10 * tenth + '0');
          n = tenth;
          } while (tenth != 0);
          *s = (char)(*s + min_offset);
          *p-- = '\0';
          while (p > s) {
          swap = *s;
          *s++ = *p;
          *p-- = swap;
          }
          }

          /* END new.c */

          --
          pete

          Comment

          • Lew Pitcher

            #6
            Re: one c question

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            Nayak Abinash XN (AS/EAB) wrote:[color=blue]
            > how to use itoa() with example
            >[/color]

            IIRC, itoa() is not an ISO C function. As such, it is hard to show you how to
            use itoa(), as it takes many forms and can be used in many ways.

            We've had a large number of discussions around itoa(); see

            for one of them. That thread includes my attempt at an itoa() function :-)

            FWIW, I've seen three variations on the itoa() parameter list on three different
            platforms. Depending on your platform you may have one of those three
            variations, or no itoa() at all. Thus, it's hard to answer your question,
            because we have no idea which of the many non-standard variations of the
            non-standard function you want to see an example of the use of.


            - --
            Lew Pitcher
            IT Specialist, Enterprise Data Systems,
            Enterprise Technology Solutions, TD Bank Financial Group

            (Opinions expressed are my own, not my employers')
            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.2.4 (MingW32)

            iD8DBQFCUVczagV FX4UWr64RAub0AJ 9bqkbgV8opu30wR w5jjAzS4CmuXwCg lJWW
            DUtL6vc2hXJLyWl xi1ekxTM=
            =N3JB
            -----END PGP SIGNATURE-----

            Comment

            • osmium

              #7
              Re: one c question

              "Nayak Abinash XN (AS/EAB)" writes:
              [color=blue]
              > how to use itoa() with example[/color]

              That is not a standard function, but there is an atoi() function. If you
              are not discussing well-known functions, it is best to describe what it is
              you want to do, rather than assigning it a name of your choosing. sprintf()
              in <stdio.h> does about what is suggested by the acronym you have assigned.


              Comment

              • Erik de Castro Lopo

                #8
                Re: one c question

                "Ramasubramania n XR (AS/EAB)" wrote:[color=blue]
                >
                > On 2005-04-03 19:49, Nayak Abinash XN (AS/EAB) wrote:[color=green]
                > > how to use itoa() with example
                > >[/color]
                >
                > #include <crtdll/errno.h>
                > #include <crtdll/stdlib.h>
                > #include <crtdll/internal/file.h>[/color]

                Those are not standard C header files. This newsgroups limits itself
                to standard C.
                [color=blue]
                > char *
                > itoa(int value, char *string, int radix)[/color]

                ????????

                Why go to all the trouble of writing and maintaining an itoa
                function when snprintf (which is standard C) is available in
                the standard library.


                char buffer [32] ;
                snprintf (buffer, sizeof (buffer), "%d", value_tp_conver t);

                Erik
                --
                +-----------------------------------------------------------+
                Erik de Castro Lopo nospam@mega-nerd.com (Yes it's valid)
                +-----------------------------------------------------------+
                "There is no reason why anyone would want a computer in their home"
                Ken Olson, DEC, 1977

                Comment

                Working...