By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,035 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,035 IT Pros & Developers. It's quick & easy.

Convert to Static Char

P: n/a
Hi

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

Thx

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ia******@hotmail.com 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"?

Jul 23 '05 #2

P: n/a
Ia******@hotmail.com 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

Jul 23 '05 #3

P: n/a

"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:42************@sbcglobal.net...
Ia******@hotmail.com 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

Jul 23 '05 #4

P: n/a
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
Jul 23 '05 #5

P: n/a

"Ioannis Vranos" <iv*@remove.this.grad.com> 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


Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.