CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    Convert char in CString to ASCII

    I have this code:

    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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Convert char in CString to ASCII

    Quote Originally Posted by Icyculyr
    I have this code:

    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
    1) 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

  3. #3
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    Re: Convert char in CString to ASCII

    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

  4. #4
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    Re: Convert char in CString to ASCII

    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

  5. #5
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Convert char in CString to ASCII

    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
    Code:
    for (int i = 0; i < csBaseChars.GetLength(); i++) {
        int iInt = csBaseChars[i];
    Kurt

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Convert char in CString to ASCII

    Quote Originally Posted by Icyculyr
    Two, that code is the only problem.. it happens on the line that sets iInt to (int)csBaseChars[i]...
    Really?
    Code:
    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 code
    The 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

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Convert char in CString to ASCII

    Quote Originally Posted by Icyculyr
    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 them
    What 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured