sprintf confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arnuld

    sprintf confusion

    For my program , I want to convert an integer into array of characters. I
    see C has an atoi function o convert from an array of characters to
    integers but no standard function to convert from integer to an array opf
    chars. Why so ? Does that conversion not come into practice ?


    we have sprintf but that riddles with the buffer-overflow problem :( and
    snprintf is not in ANSI but in C99.


    I have a network program in C ( which is not topical here, hence I did
    not post the code). The client sends a number to the server and server
    squares the number and sends it back to the client. Now any communication
    between them happens using characters. So client sends an array of
    characters to the server:

    arr[] = { '3', '\0' };


    and the server reads and square the number and sends this array back to
    the client:

    arr_new[] = { '9', '\0' };


    Right now I solved the problem using sprintf and by using the number whose
    squares are of one digit and I have also limited the size of array to 2
    only.

    but is this the C way of doing things ?




    --

    my email ID is @ the above blog.
    just check the "About Myself" page :)

  • Bart

    #2
    Re: sprintf confusion

    On May 15, 6:21 pm, arnuld <sunr...@see.si gs.invalidwrote :
    For my program , I want to convert an integer into array of characters. I
    see C has an atoi  function o convert from an array of characters to
    integers but no standard function to convert from integer to an array opf
    chars. Why so ?  Does that conversion not come into practice ?
    >
    we have sprintf but that riddles with the buffer-overflow problem :( and
    snprintf is not in ANSI but in C99.
    I have itoa() and ltoa() on my C system. But I suspect these may not
    be standard [why not?].

    Then sprintf seems a good alternative. Just give it a big enough
    buffer size (certainly more than 2 characters!). After all how big can
    an integer be? Give it 100 characters and be done with it.

    Or use it to create your own itoa().

    --
    Bartc

    Comment

    • pete

      #3
      Re: sprintf confusion

      Bart wrote:
      On May 15, 6:21 pm, arnuld <sunr...@see.si gs.invalidwrote :
      >For my program , I want to convert an integer into array of characters. I
      >see C has an atoi function o convert from an array of characters to
      >integers but no standard function to convert from integer to an array opf
      >chars. Why so ? Does that conversion not come into practice ?
      >>
      >we have sprintf but that riddles with the buffer-overflow problem :( and
      >snprintf is not in ANSI but in C99.
      >
      I have itoa() and ltoa() on my C system. But I suspect these may not
      be standard [why not?].
      >
      Then sprintf seems a good alternative. Just give it a big enough
      buffer size (certainly more than 2 characters!). After all how big can
      an integer be? Give it 100 characters and be done with it.
      >
      I'm familiar with itoa
      as a code example and an exercise from K&R, sec 3.6.
      You still need a buffer with that.

      --
      pete

      Comment

      • Richard Heathfield

        #4
        Re: sprintf confusion

        Bart said:

        <snip>
        After all how big can an integer be?
        You can find out with sizeof.

        If you want a buffer big enough to support an integer parameter n, you can
        get one like this (given appropriate furniture, e.g. <limits.h>) - the
        #defines are merely for explanatory purposes and I don't propose that you
        use them in Real Code:

        #define BYTES_PER_INT (sizeof n)
        #define BITS_PER_BYTE (CHAR_BIT)
        #define BITS_PER_INT (BYTES_PER_INT * BITS_PER_BYTE)
        #define BIT_CAPACITY 3 /* see below */
        #define ALLOW_FOR_MINUS 1
        #define ALLOW_FOR_TERM 1
        #define DIGIT_COUNT ((BITS_PER_INT + BIT_CAPACITY - 1) / BIT_CAPACITY)

        #define BUFFER_SIZE (ALLOW_FOR_MINU S + DIGIT_COUNT + ALLOW_FOR_TERM)

        Bit capacity: since decimal representation gives us ten digits ('0' through
        '9'), each digit can represent log(2)10 = 3.32+ bits, so it can certainly
        represent 3 bits.

        Note that for decimal representation the calculated buffer size is always
        sufficient, but it can err a little - always on the generous side.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Mark McIntyre

          #5
          Re: sprintf confusion

          arnuld wrote:
          For my program , I want to convert an integer into array of characters. I
          see C has an atoi function o convert from an array of characters to
          integers but no standard function to convert from integer to an array opf
          chars. Why so ? Does that conversion not come into practice ?
          Its commonplace to need to read input and convert it to numbers, but the
          reverse is less common.
          we have sprintf but that riddles with the buffer-overflow problem :( and
          snprintf is not in ANSI but in C99.
          sprintf() can be used effectively if you're careful.
          Right now I solved the problem using sprintf and by using the number whose
          squares are of one digit and I have also limited the size of array to 2
          only.
          >
          but is this the C way of doing things ?
          yes.

          --
          Mark McIntyre

          CLC FAQ <http://c-faq.com/>
          CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

          Comment

          Working...