Convert to Static Char

Discussion in 'C++' started by IamZadok@hotmail.com, Feb 28, 2005.

  1. Guest

    Hi

    I was wondering if anyone knew how to convert a string or an integer
    into a Static Char.



    Thx
     
    , Feb 28, 2005
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > Hi
    >
    > I was wondering if anyone knew how to convert a string or an integer
    > into a Static Char.


    What exactly do you mean by "Static Char"?
     
    Rolf Magnus, Feb 28, 2005
    #2
    1. Advertising

  3. wrote:
    > Hi
    >
    > I was wondering if anyone knew how to convert a string or an integer
    > into a Static Char.
    >
    >
    >
    > Thx
    >


    Try this:
    static char c;

    int main(void)
    {
    c = 'A'; // Converts 'A' into a static char.
    c = '1'; // Converts a literal numeric into a char.
    return 0;
    }

    If this doesn't satisfy you, please be more descriptive
    as to what you are seeking.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Feb 28, 2005
    #3
  4. Howard Guest

    "Thomas Matthews" <> wrote in
    message news:...
    > wrote:
    >> Hi
    >>
    >> I was wondering if anyone knew how to convert a string or an integer
    >> into a Static Char.

    >
    > Try this:
    > static char c;
    >
    > int main(void)
    > {
    > c = 'A'; // Converts 'A' into a static char.
    > c = '1'; // Converts a literal numeric into a char.
    > return 0;
    > }
    >
    > If this doesn't satisfy you, please be more descriptive
    > as to what you are seeking.
    >
    > --
    > Thomas Matthews


    Well, considering he asked about converting a string or an integer, and
    since 'A' is not a string and '1' is not an integer, I suspect it won't
    satisfy.

    Like you, however, I await clarification as to exactly what the heck he
    *does* want! :)

    To the OP:

    When you say you want to "convert ... to a static char", that doesn't really
    make sense. Do you mean you want to know how to put the contents of a
    string (i.e., std::string) into an array of char (a C-style string), and how
    to convert an integer into an C-style string as well? Or...???

    A char (watch your capitalization, it's important in C++) is a single
    character, and is not going to be able to hold an integer (if it's outside
    the range of 0..9) or a string (if the string is longer than 1 character).

    With the keyword "static" in front of it, it means different things,
    depending upon where it's used. A static member variable is different from
    a non-member variable declared as static. (I hate that those different
    concepts use the same keyword, but what're you gonna do, eh? :))

    Research the keyword "static". Also research "array of char", or C-style
    strings. And if by "string" you meant the class std::string, look that up,
    too.

    -Howard
     
    Howard, Feb 28, 2005
    #4
  5. Howard wrote:

    > A char (watch your capitalization, it's important in C++) is a single
    > character, and is not going to be able to hold an integer (if it's outside
    > the range of 0..9)



    I am sure you did not mean what it sounds that you said, but we have to
    be technically correct. :)

    A char can hold integer values from numeric_limits<char>::min() to
    numeric_limits<char>::max() the latest being always at least 127.


    The following code displays these ranges in a system:


    #include <iostream>
    #include <limits>

    int main()
    {
    using namespace std;

    cout<<static_cast<int>( numeric_limits<char>::min() )<<"\n"
    <<static_cast<int>( numeric_limits<char>::max() )<<"\n";
    }


    In my system it outputs:

    C:\c>temp
    -128
    127

    C:\c>



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Mar 1, 2005
    #5
  6. Howard Guest

    "Ioannis Vranos" <> wrote in message
    news:1109640191.100742@athnrd02...
    > Howard wrote:
    >
    >> A char (watch your capitalization, it's important in C++) is a single
    >> character, and is not going to be able to hold an integer (if it's
    >> outside the range of 0..9)

    >
    >
    > I am sure you did not mean what it sounds that you said, but we have to be
    > technically correct. :)


    Right, sorry. What I meant was that if you are using an array of char to
    display an integer that has been "converted" to a string, such as with itoa,
    sprintf, or a stringstream, then a single char in that array will only hold
    one decimal digit of that integer. So the number 127 would need an array of
    at least three chars, i.e., ['1','2','7'] (plus one more for the
    null-terminator if used as a C-style string). And since the OP asked about
    converting an integer to a {static} char, I wanted to point out that he
    probably meant an _array_ of char, not just a single char. Assuming, of
    course, I understood what he meant by "convert"! :)

    Thanks,
    Howard
     
    Howard, Mar 1, 2005
    #6
    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. wwj
    Replies:
    7
    Views:
    574
  2. wwj
    Replies:
    24
    Views:
    2,544
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    490
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    605
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
Loading...

Share This Page