Convert to Static Char

I

IamZadok

Hi

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



Thx
 
T

Thomas Matthews

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
 
H

Howard

Thomas Matthews said:
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.

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
 
I

Ioannis Vranos

Howard said:
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>
 
H

Howard

Ioannis Vranos said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top