#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    76
    Rep Power
    12

    ASCII(Visual C++)


    I hav a question here:
    CString *pData=NULL;
    pData = new CString("ABCD");
    char nChar[2];

    sprintf(nChar, "%d", pData->Mid(0,1));

    how can i get the asc for character "A"?is my sprintf func right?thanks...
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    Try using the single quatation marks

    int i='a';

    or

    char sBuffer[3]={"A"};
    int i=(int)sBuffer[0];//cast may not be needed
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    76
    Rep Power
    12
    thanks...TechNoFear
    but now if i have:
    char* p = "ABCD";

    how can i get the asc for "C" character if i used the sprintf() func..thanks..
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    What are you trying to do?


    Print the ASCII value of each character in a string?

    Try this:

    in C:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
        char chrPtr[] = "somestring goes here";
        int i, len;
    
        len = strlen(chrPtr);
        for (i=0; i<len; i++){
            printf("%d\n", chrPtr[i]);
        }
    
        return 0;
    }
    in C++:

    Code:
    #include <iostream>
    #include <string>
    
    int main(){
        std::string str;
        int val;
        
        str = "some string goes here";
    
        for (int i=0; i<str.size(); i++){
            val = str.c_str()[i];
            std::cout << val << std::endl;
        }
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    if you are using CString class, then I suggest if the CString is ascii to use GetBuffer method to get the value in the CString to a character before you try retreving the value of the character

IMN logo majestic logo threadwatch logo seochat tools logo