locked
Convert CString to just Char not Char* RRS feed

  • Question

  • Hi,

    i'v been wanting to convert a hexadecimal number 78 in CString to a char variable bt i can only find references to  convert CString into char*.

    i am using Microsoft visual studio 6.

    Plz suggest me a way asap!!

    Monday, July 2, 2012 3:19 PM

Answers

  • Hi,

    i'v been wanting to convert a hexadecimal number 78 in CString to a char variable bt i can only find references to  convert CString into char*.

    i am using Microsoft visual studio 6.

    Plz suggest me a way asap!!

    Do you want array of chars? you can't make "78" as only char. 

                           CString str = _T("78");
    			wchar_t a[2];
    
    			for(int i=0;i<str.GetLength();i++)
    			{
    				a[i] = str.GetAt(i);
    			}
    
    			printf(("%S"),a);



    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/


    • Edited by Selvam Monday, July 2, 2012 3:28 PM
    • Proposed as answer by Selvam Monday, July 2, 2012 3:29 PM
    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:21 AM
    Monday, July 2, 2012 3:25 PM
  • A CString represents multiple characters, which is why you're finding the code which converts it to a char* (basically, a pointer to the start of the array of characters).  You should be able to use results[0] if you only want the first (single) character.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:22 AM
    Monday, July 2, 2012 3:25 PM
  • Assuming you are not using a unicode build, so CString contains chars, then...

    CString s((char)0x78);

    char c = s[0];

    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:22 AM
    Monday, July 2, 2012 3:25 PM
  • Use GetBuffer() and ReleaseBuffer() API's of CString

    Thanks, Renjith V R

    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:22 AM
    Monday, July 2, 2012 3:54 PM
  • If you have the string "78" and you whant this string converted to the value 0x78 than the easiest way would beusing

    strtol: http://msdn.microsoft.com/en-us/library/w4z2wdyc%28v=vs.100%29.aspx

    strtoul: http://msdn.microsoft.com/en-us/library/5k9xb7x1.aspx

    Than you only need to cast you value from long / unsigned long to char.



    • Edited by Bordon Monday, July 2, 2012 5:49 PM
    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:22 AM
    Monday, July 2, 2012 5:46 PM

All replies

  • Hi,

    i'v been wanting to convert a hexadecimal number 78 in CString to a char variable bt i can only find references to  convert CString into char*.

    i am using Microsoft visual studio 6.

    Plz suggest me a way asap!!

    Do you want array of chars? you can't make "78" as only char. 

                           CString str = _T("78");
    			wchar_t a[2];
    
    			for(int i=0;i<str.GetLength();i++)
    			{
    				a[i] = str.GetAt(i);
    			}
    
    			printf(("%S"),a);



    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/


    • Edited by Selvam Monday, July 2, 2012 3:28 PM
    • Proposed as answer by Selvam Monday, July 2, 2012 3:29 PM
    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:21 AM
    Monday, July 2, 2012 3:25 PM
  • A CString represents multiple characters, which is why you're finding the code which converts it to a char* (basically, a pointer to the start of the array of characters).  You should be able to use results[0] if you only want the first (single) character.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:22 AM
    Monday, July 2, 2012 3:25 PM
  • Assuming you are not using a unicode build, so CString contains chars, then...

    CString s((char)0x78);

    char c = s[0];

    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:22 AM
    Monday, July 2, 2012 3:25 PM
  • Use GetBuffer() and ReleaseBuffer() API's of CString

    Thanks, Renjith V R

    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:22 AM
    Monday, July 2, 2012 3:54 PM
  • If you have the string "78" and you whant this string converted to the value 0x78 than the easiest way would beusing

    strtol: http://msdn.microsoft.com/en-us/library/w4z2wdyc%28v=vs.100%29.aspx

    strtoul: http://msdn.microsoft.com/en-us/library/5k9xb7x1.aspx

    Than you only need to cast you value from long / unsigned long to char.



    • Edited by Bordon Monday, July 2, 2012 5:49 PM
    • Marked as answer by Helen Zhao Monday, July 9, 2012 2:22 AM
    Monday, July 2, 2012 5:46 PM