integer to characters

Discussion in 'C Programming' started by Alan, Oct 22, 2003.

  1. Alan

    Alan Guest

    I want to change a 3 digits integer to characters, how can i do that?

    the 3 digits integer maybe 123, 23 or 3
    I want to change the integer to "123", " 23" or " 3"

    thx
     
    Alan, Oct 22, 2003
    #1
    1. Advertising

  2. Alan wrote:
    >
    > I want to change a 3 digits integer to characters, how can i do that?
    >
    > the 3 digits integer maybe 123, 23 or 3
    > I want to change the integer to "123", " 23" or " 3"
    >


    You don't want to change anything.
    You want to convert an integer into its textual representation.

    char Buffer[20];
    int Number = 123;
    sprintf( Buffer, "%d", Number );

    Also: When posting to alt.comp.lang.learn.c-c++ always
    indicate which language you need a solution for. There
    we discuss C and C++. The C++ solution would be completely
    different.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 22, 2003
    #2
    1. Advertising

  3. Alan

    Alan Guest

    "Karl Heinz Buchegger" <> ???
    news: ???...
    >
    > Alan wrote:
    > >
    > > I want to change a 3 digits integer to characters, how can i do that?
    > >
    > > the 3 digits integer maybe 123, 23 or 3
    > > I want to change the integer to "123", " 23" or " 3"
    > >

    >
    > You don't want to change anything.
    > You want to convert an integer into its textual representation.
    >
    > char Buffer[20];
    > int Number = 123;
    > sprintf( Buffer, "%d", Number );


    Is there any function same as sprintf() but write to file?
    because I can't find a function called "fsprintf()"
    thx


    > Also: When posting to alt.comp.lang.learn.c-c++ always
    > indicate which language you need a solution for. There
    > we discuss C and C++. The C++ solution would be completely
    > different.


    sorry, next time I will do so
     
    Alan, Oct 22, 2003
    #3
  4. Alan

    Rick Guest

    Alan wrote:

    > I want to change a 3 digits integer to characters, how can i do that?
    >
    > the 3 digits integer maybe 123, 23 or 3
    > I want to change the integer to "123", " 23" or " 3"
    >
    > thx


    char arr[4];
    int val = 123;

    arr[0] = '0' + val/100;
    arr[1] = '0' + (val%100)/10;
    arr[2] = '0' + val%10;
    arr[3] = '\0';

    printf("%s", arr);


    Rick
     
    Rick, Oct 22, 2003
    #4
  5. Alan wrote:
    >
    > "Karl Heinz Buchegger" <> ???
    > news: ???...
    > >
    > > Alan wrote:
    > > >
    > > > I want to change a 3 digits integer to characters, how can i do that?
    > > >
    > > > the 3 digits integer maybe 123, 23 or 3
    > > > I want to change the integer to "123", " 23" or " 3"
    > > >

    > >
    > > You don't want to change anything.
    > > You want to convert an integer into its textual representation.
    > >
    > > char Buffer[20];
    > > int Number = 123;
    > > sprintf( Buffer, "%d", Number );

    >
    > Is there any function same as sprintf() but write to file?
    > because I can't find a function called "fsprintf()"
    > thx


    What for?
    Either fprintf() does what you need
    or you first print to a character buffer and then
    write that character buffer to the file.

    In any case: there is no need for fsprintf

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 22, 2003
    #5
  6. On Wed, 22 Oct 2003 19:12:21 +1000, Rick <rrquick@nospam-com> wrote:

    >Alan wrote:
    >
    >> I want to change a 3 digits integer to characters, how can i do that?
    >>
    >> the 3 digits integer maybe 123, 23 or 3
    >> I want to change the integer to "123", " 23" or " 3"
    >>
    >> thx

    >
    >char arr[4];
    >int val = 123;
    >
    >arr[0] = '0' + val/100;
    >arr[1] = '0' + (val%100)/10;
    >arr[2] = '0' + val%10;
    >arr[3] = '\0';
    >
    >printf("%s", arr);


    What is the plan if the three-digit integer is negative?

    Best wishes,

    Bob
     
    Robert W Hand, Oct 22, 2003
    #6
  7. Hi,


    "Rick" <rrquick@nospam-com> wrote in message
    news:3f9647af$...
    > Alan wrote:
    >
    > > I want to change a 3 digits integer to characters, how can i do that?
    > >
    > > the 3 digits integer maybe 123, 23 or 3
    > > I want to change the integer to "123", " 23" or " 3"
    > >
    > > thx

    >
    > char arr[4];
    > int val = 123;
    >
    > arr[0] = '0' + val/100;
    > arr[1] = '0' + (val%100)/10;
    > arr[2] = '0' + val%10;
    > arr[3] = '\0';
    >
    > printf("%s", arr);


    this fails the OPs spec.

    Why not use the standard library for things it was
    made for?

    Regards,
    Stefan.
     
    Stefan Höhne, Oct 22, 2003
    #7
  8. Alan

    Alan Guest

    "Stefan Höhne" <do_not_use_this_adress@127.0.0.1> ¦b¶l¥ó
    news:bn5jog$2vk$-ag.de ¤¤¼¶¼g...
    > Hi,
    >
    > "Rick" <rrquick@nospam-com> wrote in message
    > news:3f9647af$...
    > > Alan wrote:
    > >
    > > > I want to change a 3 digits integer to characters, how can i do that?
    > > >
    > > > the 3 digits integer maybe 123, 23 or 3
    > > > I want to change the integer to "123", " 23" or " 3"
    > > >
    > > > thx

    > >
    > > char arr[4];
    > > int val = 123;
    > >
    > > arr[0] = '0' + val/100;
    > > arr[1] = '0' + (val%100)/10;
    > > arr[2] = '0' + val%10;
    > > arr[3] = '\0';
    > >
    > > printf("%s", arr);

    >
    > this fails the OPs spec.


    I don't understand, in what situation will it fail ?


    > Why not use the standard library for things it was
    > made for?


    what is the standard library and how to do that ?
    thx
     
    Alan, Oct 22, 2003
    #8
  9. Hi,

    "Alan" <> wrote in message
    news:bn5lpq$qpq$...
    >
    > "Stefan Höhne" <do_not_use_this_adress@127.0.0.1> ¦b¶l¥ó
    > news:bn5jog$2vk$-ag.de ¤¤¼¶¼g...
    > > Hi,
    > >
    > > "Rick" <rrquick@nospam-com> wrote in message
    > > news:3f9647af$...
    > > > Alan wrote:
    > > >
    > > > > I want to change a 3 digits integer to characters, how can i do

    that?
    > > > >
    > > > > the 3 digits integer maybe 123, 23 or 3
    > > > > I want to change the integer to "123", " 23" or " 3"
    > > > >
    > > > > thx
    > > >
    > > > char arr[4];
    > > > int val = 123;
    > > >
    > > > arr[0] = '0' + val/100;
    > > > arr[1] = '0' + (val%100)/10;
    > > > arr[2] = '0' + val%10;
    > > > arr[3] = '\0';
    > > >
    > > > printf("%s", arr);

    > >
    > > this fails the OPs spec.

    >
    > I don't understand, in what situation will it fail ?


    for val=3. This solution would compute "003", the OP
    (=you) explicitely wanted " 3". Of course this is easily
    adopted, but needs extra logic.

    > > Why not use the standard library for things it was
    > > made for?

    >
    > what is the standard library and how to do that ?
    > thx


    The standard library consists of all those basic
    functionality you use everyday: malloc(), printf(),
    rand(), sin(), is_alpha(), ...

    In this case, sprintf() would be the tool to use, as
    Karl suggested.

    HTH,
    Stefan.
     
    Stefan Höhne, Oct 22, 2003
    #9
  10. Alan

    Jirka Klaue Guest

    Stefan Höhne wrote:
    ....
    > The standard library consists of all those basic
    > functionality you use everyday: malloc(), printf(),
    > rand(), sin(), is_alpha(), ...


    I never heard of or used is_alpha().
    Well, I never used isalpha as well. :)

    Jirka
     
    Jirka Klaue, Oct 22, 2003
    #10
  11. "Stefan Höhne" wrote:
    >
    > In this case, sprintf() would be the tool to use, as
    > Karl suggested.


    Of course not in the way I suggested, the format specifiers
    would need some adjustment. But that's any easy thing to do
    with sprintf (and there should be some work left for the OP too :)

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 22, 2003
    #11
  12. Alan

    Robby C.H.S. Guest

    Karl Heinz Buchegger <> wrote in message news:<>...
    > Alan wrote:
    > >
    > > I want to change a 3 digits integer to characters, how can i do that?
    > >
    > > the 3 digits integer maybe 123, 23 or 3
    > > I want to change the integer to "123", " 23" or " 3"
    > >

    >
    > You don't want to change anything.
    > You want to convert an integer into its textual representation.
    >
    > char Buffer[20];
    > int Number = 123;
    > sprintf( Buffer, "%d", Number );
    >
    > Also: When posting to alt.comp.lang.learn.c-c++ always
    > indicate which language you need a solution for. There
    > we discuss C and C++. The C++ solution would be completely
    > different.


    Sorry for interrupting.
    What is the C++ solution?
     
    Robby C.H.S., Oct 22, 2003
    #12
  13. Alan

    Aggro Guest

    Aggro, Oct 22, 2003
    #13
  14. "Robby C.H.S." wrote:
    >
    > Karl Heinz Buchegger <> wrote in message news:<>...
    > > Alan wrote:
    > > >
    > > > I want to change a 3 digits integer to characters, how can i do that?
    > > >
    > > > the 3 digits integer maybe 123, 23 or 3
    > > > I want to change the integer to "123", " 23" or " 3"
    > > >

    > >
    > > You don't want to change anything.
    > > You want to convert an integer into its textual representation.
    > >
    > > char Buffer[20];
    > > int Number = 123;
    > > sprintf( Buffer, "%d", Number );
    > >
    > > Also: When posting to alt.comp.lang.learn.c-c++ always
    > > indicate which language you need a solution for. There
    > > we discuss C and C++. The C++ solution would be completely
    > > different.

    >
    > Sorry for interrupting.
    > What is the C++ solution?


    search the group (eg. on google) :)
    This question is asked at least twice a day.
    Hint: stringstreams

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 22, 2003
    #14
  15. Robby C.H.S. wrote:

    > Karl Heinz Buchegger <> wrote in message
    > news:<>...
    >> Alan wrote:
    >> >
    >> > I want to change a 3 digits integer to characters, how can i do that?
    >> >
    >> > the 3 digits integer maybe 123, 23 or 3
    >> > I want to change the integer to "123", " 23" or " 3"
    >> >

    >>
    >> You don't want to change anything.
    >> You want to convert an integer into its textual representation.
    >>
    >> char Buffer[20];
    >> int Number = 123;
    >> sprintf( Buffer, "%d", Number );
    >>
    >> Also: When posting to alt.comp.lang.learn.c-c++ always
    >> indicate which language you need a solution for. There
    >> we discuss C and C++. The C++ solution would be completely
    >> different.

    >
    > Sorry for interrupting.
    > What is the C++ solution?


    int number = 123;
    std::eek:stringstream oss;
    oss << std::setw(3) << number;
    std::string buffer = oss.str();

    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.snurse-l.org/acllc-c /faq.html
    a.c.l.l.c-c++ FAQ mirror: http://nullptr.merseine.nu:8080/acllcc .html
    c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    c.l.c++ FAQ: http://www.parashift.com/c -faq-lite/
     
    B. v Ingen Schenau, Oct 22, 2003
    #15
  16. Alan wrote:

    > I want to change a 3 digits integer to characters, how can i do that?
    >
    > the 3 digits integer maybe 123, 23 or 3
    > I want to change the integer to "123", " 23" or " 3"


    #include <stdio.h>

    char *package(unsigned int num, char *s)
    {
    if (num > 999)
    return 0;
    if (sprintf(s, "%3d", num))
    return s;
    else
    return 0;
    }

    int main(void)
    {
    int x[3] = { 123, 23, 3 };
    char buf[BUFSIZ], *p;
    size_t i;
    for (i = 0; i < sizeof x / sizeof *x; i++) {
    if ((p = package(x, buf)))
    printf("%d -> \"%s\"\n", x, p);
    else
    printf("something went wrong with %d\n", x);
    }
    return 0;
    }


    [output]
    123 -> "123"
    23 -> " 23"
    3 -> " 3"

    Now give us an exercise from the *second* chapter of your Introduction to C
    text.

    --
    Martin Ambuhl
     
    Martin Ambuhl, Oct 22, 2003
    #16
  17. Martin Ambuhl wrote:
    > char *package(unsigned int num, char *s)

    [...]
    > if (sprintf(s, "%3d", num))


    if (sprintf(s, "%3u", num) > 0)
     
    Jeremy Yallop, Oct 22, 2003
    #17
  18. On Wed, 22 Oct 2003 22:38:24 GMT, Martin Ambuhl
    <> wrote:

    [snip]

    >int main(void)
    >{
    > int x[3] = { 123, 23, 3 };
    > char buf[BUFSIZ], *p;
    > size_t i;
    > for (i = 0; i < sizeof x / sizeof *x; i++) {

    ^^^^^^^^^
    What does this mean? I can not help but read it as the size of
    an int * *. I would have written "sizeof(int)".

    Please explain as I am missing something if this is real code.

    [snip]

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Oct 22, 2003
    #18
  19. Alan

    Mike Wahler Guest

    "Gene Wirchenko" <> wrote in message
    news:...
    > On Wed, 22 Oct 2003 22:38:24 GMT, Martin Ambuhl
    > <> wrote:
    >
    > [snip]
    >
    > >int main(void)
    > >{
    > > int x[3] = { 123, 23, 3 };
    > > char buf[BUFSIZ], *p;
    > > size_t i;
    > > for (i = 0; i < sizeof x / sizeof *x; i++) {

    > ^^^^^^^^^
    > What does this mean?


    It evaluates to the size of an array element, and
    is independent of the array's element type.

    > I can not help but read it as the size of
    > an int * *.


    No, it's equivalent to 'sizeof(int)'. The type of
    '*x' is 'int'.

    >I would have written "sizeof(int)".


    And thus tied it to type 'int'. Martin's code will
    work whatever the type of the array element.

    -Mike
     
    Mike Wahler, Oct 23, 2003
    #19
  20. Alan

    Default User Guest

    Gene Wirchenko wrote:
    >
    > On Wed, 22 Oct 2003 22:38:24 GMT, Martin Ambuhl
    > <> wrote:
    >
    > [snip]
    >
    > >int main(void)
    > >{
    > > int x[3] = { 123, 23, 3 };
    > > char buf[BUFSIZ], *p;
    > > size_t i;
    > > for (i = 0; i < sizeof x / sizeof *x; i++) {

    > ^^^^^^^^^
    > What does this mean? I can not help but read it as the size of
    > an int * *. I would have written "sizeof(int)".


    How would you get a pointer to pointer from applying the dereferencing
    operator to a pointer?

    > Please explain as I am missing something if this is real code.


    Yes, that divides the size of an entire array but the size of one
    element, giving the number of elements in the array. Only works with
    real arrays.



    Brian Rodenborn
     
    Default User, Oct 23, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,993
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    543
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    536
    Yu SONG
    Oct 15, 2004
  4. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    45,963
    JavaBean2010
    Apr 7, 2010
  5. Johannes Zellner
    Replies:
    22
    Views:
    3,771
Loading...

Share This Page