Discussion:
Decimal Ascii-Value of a single Char
(too old to reply)
Alan Bellingham
2008-05-25 18:21:02 UTC
Permalink
"Your Name" <your-***@anywhere.net> wrote:

You may want to fill your name in, at least!
i have a typecast question, i need to get the decimal ascii-value
of a single char (0-127, in this case "E" should return 69)!
void __fastcall TForm1::Button1Click(TObject *Sender)
{
unsigned int x2;
AnsiString out = "E";
x2 = (unsigned char)out.c_str();
Avoid using that type of C cast in C++. It's almost never necessary, and
it's often not safe. In this case, you're trying to convert a pointer to
an unsigned char - and that makes no sense to the compiler. You had to
hit it over the head with the sledgehammer of a C-style cast to get it
to agree to do what you wanted.

Interestingly, 27338816 (decimal) is 0x1A12840. You've cast your pointer
to an unsigned char, which is one byte rather than 4, so it truncates
that value down to 0x40. Which, in decimal, is the 64 you're seeing.
Edit2->Text = AnsiString(x2); // returns 64 however the input is
As just explained.
x2 = (unsigned int)out.c_str();
Edit2->Text = AnsiString(x2); // returns 27338816
That's the full 0x1A12840 value.
}
How can i get the Ascii-Decimal(using BCB6 under Vista)?
You don't want the pointer, you want *what the pointer is pointing to*.
So,

x2 = static_cast<unsigned char>(out[0]);

should do it (bearing in mind I never use AnsiStrings, so there might be
a typo in there.) The cast is there only because of the possible
signed/unsigned difference (you may have signed chars turned on).

Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced
Elmar Baumann
2008-05-25 18:45:14 UTC
Permalink
with Subject: Re: Decimal Ascii-Value of a single Char, on 25.05.2008 20:21:02
You may want to fill your name in, at least!
okay, done ;-)
x2 = static_cast<unsigned char>(out[0]);
should do it (bearing in mind I never use AnsiStrings, so there might be
a typo in there.) The cast is there only because of the possible
signed/unsigned difference (you may have signed chars turned on).
Thank you for this Informations - it works great!
Alan Bellingham
2008-05-26 09:07:06 UTC
Permalink
Post by Elmar Baumann
Post by Alan Bellingham
x2 = static_cast<unsigned char>(out[0]);
should do it (bearing in mind I never use AnsiStrings, so there might be
a typo in there.) The cast is there only because of the possible
signed/unsigned difference (you may have signed chars turned on).
Thank you for this Informations - it works great!
Ah, note Duane's comment. Or use

x2 = static_cast<unsigned char>(out.c_str()[0]);

which really does use the c_str() pointer and then get the first char
that that points to.

Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced
Elmar Baumann
2008-05-26 20:02:20 UTC
Permalink
with Subject: Re: Decimal Ascii-Value of a single Char, on 26.05.2008 11:07:06
Ah, note Duane's comment. Or use
x2 = static_cast<unsigned char>(out.c_str()[0]);
Ohh - sure i did so^^, but much thanks for helping . . .


regards

Elmar Baumann
Duane Hebert
2008-05-25 21:02:12 UTC
Permalink
Post by Alan Bellingham
x2 = static_cast<unsigned char>(out[0]);
should do it (bearing in mind I never use AnsiStrings, so there might be
a typo in there.) The cast is there only because of the possible
signed/unsigned difference (you may have signed chars turned on).
AnsiString is one based so it should be out[1]
if out is an AnsiString. There used to be an overloaded
[] that returned [1] for [0] though I thought they
got rid of that.
Alan Bellingham
2008-05-25 22:45:33 UTC
Permalink
Post by Duane Hebert
AnsiString is one based so it should be out[1]
if out is an AnsiString. There used to be an overloaded
[] that returned [1] for [0] though I thought they
got rid of that.
Ah, thanks.

(And the overload sounds ... interesting. Just imagine indexing from 0
to size-1. You'd get the first item twice, and the last never.)

Alan Bellingham
--
Team Browns
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html> netiquette
Duane Hebert
2008-05-25 23:55:58 UTC
Permalink
Post by Alan Bellingham
Post by Duane Hebert
AnsiString is one based so it should be out[1]
if out is an AnsiString. There used to be an overloaded
[] that returned [1] for [0] though I thought they
got rid of that.
Ah, thanks.
(And the overload sounds ... interesting. Just imagine indexing from 0
to size-1. You'd get the first item twice, and the last never.)
It was especially interesting refactoring to std::string.

Anyway, with 2006 Explorer version, I tried:
AnsiString s("Hello");
ShowMessage(s[0]);

and get an access violation so I expect that the overload is
gone.
Remy Lebeau (TeamB)
2008-05-27 20:35:22 UTC
Permalink
Post by Alan Bellingham
x2 = static_cast<unsigned char>(out[0]);
AnsiString is 1-indexed, not 0-indexed. And you don't need the cast at all:

x2 = out[1];


Gambit

Loading...