Click to See Complete Forum and Search --> : Convert char in CString to ASCII


Icyculyr
August 22nd, 2008, 03:11 AM
I have this code:


for (int i = 0; i < iBaseCharsLength; i++)
{
//int iInt = (int)csBaseChars[i];
//iIndex[i] = iInt;
//int iInt2 = (int)csEndChars[i];
//iEnd[i] = iInt2;
}


I commented out the lines because they cause an "ASSERTION FAILED" error,

that's probably because I'm treating csBaseChars as an array, but what I really want to do is get the ASCII value for each char in csBaseChars, and csEndChars, I thought of using csBaseChars.Mid(0, 1), but it won't let me cast it as an int.

Can anyone tell me how to do this?

Cheers
Icyculyr

Paul McKenzie
August 22nd, 2008, 03:18 AM
I have this code:


for (int i = 0; i < iBaseCharsLength; i++)
{
//int iInt = (int)csBaseChars[i];
//iIndex[i] = iInt;
//int iInt2 = (int)csEndChars[i];
//iEnd[i] = iInt2;
}


I commented out the lines because they cause an "ASSERTION FAILED" error,

that's probably because I'm treating csBaseChars as an array, but what I really want to do is get the ASCII value for each char in csBaseChars, and csEndChars, I thought of using csBaseChars.Mid(0, 1), but it won't let me cast it as an int.

Can anyone tell me how to do this?

Cheers
Icyculyr1) CString is MFC. You should have posted in the Visual C++ forum, not the WinAPI forum.

2) There is nothing wrong with the code you posted. It is perfectly valid to use [] on a CString type and cast the return to an int. There is something wrong with the code you didn't post that causes the failure.

Next time, post a small, but complete example demonstrating the error. Just posting lines of uncommented code that is perfectly valid standing by itself is not going to help solve your problem.

Regards,

Paul McKenzie

Icyculyr
August 23rd, 2008, 02:47 AM
Firstly, I was told CString can also be used in WIN32, which is what I am doing, I was told to use it, instead of Raw WCHAR's or whatever they are...

My project is not MFC...

Two, that code is the only problem.. it happens on the line that sets iInt to (int)csBaseChars[i]...

I don't see any other code that would lead to that problem... just those uncommented lines, and my compiler won't let me cast csBaseChars[i] as an int...

I don't know why.

Cheers
Icyculr

Icyculyr
August 23rd, 2008, 03:11 AM
When I've debugged, the value in csBaseChars is AAAA

it says ASSERTION FAILED ERROR, Expression (iChar >= 0) && (iChar <= GetLength()) as soon as I try and debug past this line:

WCHAR wcChar = (WCHAR)csBaseChars[2];

Does anyone know why?

if you want I can post the three functions I am using, but I don't think there's anything wrong with them (involving this error anyway lol)

Cheers
Icyculyr

ZuK
August 23rd, 2008, 04:05 AM
From that assertion I would understand that iBaseCharsLength is not the length of the string csBaseChars.
show the code where you initialize both variables or use
for (int i = 0; i < csBaseChars.GetLength(); i++) {
int iInt = csBaseChars[i];
Kurt

Paul McKenzie
August 23rd, 2008, 04:44 AM
Two, that code is the only problem.. it happens on the line that sets iInt to (int)csBaseChars[i]...Really?

for (int i = 0; i < iBaseCharsLength; i++)

What is iBaseCharsLength? It could be 1, 324324, 8327, 325, ... how would we know without you showing us the lines of code that sets up this loop?

That's why I asked you to provide a complete example. No one knows what value your variables are, and you're using this unknown value in your example. I could easily take your code, put it in a function, and it works perfectly because I dummied up some good values for the looping.

That's why we need to see the entire context of where, when, and how you're using that code. Just posting a few lines of code of valid code with variables that we don't know the values of the variables doesn't help.
I don't see any other codeThe issue is that we can't see the code either, because you didn't post it.
and my compiler won't let me cast csBaseChars[ i ] as an int...The compiler does allow you to do this. If it didn't, the compiler would not have compiled your code without errors. The issue is not compiling, it is at runtime. We don't know if "i" is an index that is correct.

Regards,

Paul McKenzie

Paul McKenzie
August 23rd, 2008, 04:49 AM
When I've debugged, the value in csBaseChars is AAAA

it says ASSERTION FAILED ERROR, Expression (iChar >= 0) && (iChar <= GetLength()) as soon as I try and debug past this line:

WCHAR wcChar = (WCHAR)csBaseChars[2];

Does anyone know why?
The error is obvious.

Your string can only hold less than 2 characters, but you're accessing the third element in the string.
if you want I can post the three functions I am using, but I don't think there's anything wrong with themWhat you should be showing us is the code that initializes csBaseChars and sets its size.

See what I mean now? If you really expected csBaseChars to hold more than two characters, the problem wasn't that one line of code you posted. The problem is the code you didn't post, i.e. the code that initializes and sets the value of csBaseChars before that line is invoked.

Regards,

Paul McKenzie