Log in

View Full Version : Oh No... Convert from Long to String, need help... again...


PPCdev
02-16-2003, 07:31 PM
I know you good hearted people are getting tired of me by now. I tried to bug the folks at codeproject.com for a while. Nice folks there, the best place to be if your writing X86 code, but Im getting help that doesnt apply and thus gettimg more confused since Im writing for WinCE. I now understand Im working with a Unicode (?) environment, but much of the advice is for ANSI (I know what that is..) environment. Since I am too much a newbie to know the difference, Im trying to absorb all the information and apply it to no avail. I still cannot convert a Long variable to a string variable.

The sting var is defined as CString, as best I know that shouldnt be a problem.

The reason for this is so that the sum of two numbers can be used in a text file output, message box, or whatever... The main reason for this is because this is my newbie project which I am learning with.

Ive been working on this specific item for 2 days now with no success. Now I know why VB is so appealing to beginner programmers. :-(

I would appericiate some code example, please bare in mind Im 90% ignorant of structure at this point.

Thanks for any help,
Andrew

smashcasi
02-16-2003, 08:07 PM
Unicode and ANSI are basically both definitions for encoding characters. The problem with ANSI is that its character set only adequately handles the English language. The Unicode (http://www.unicode.org/) standard is designed to work around this problem by providing "a unique number for every character, no matter what the platform, no matter what the program, no matter what the language".

Ok, great you say. So why does this make your life difficult? All characters in the ANSI character set can be defined using only 8 bits (1 byte). Unicode on the other hand has to support Kanji and all the other special characters out there, so it requires 16 bits (2 bytes) to represent each character. This is why Unicode characters are also called "wide characters".

So, keeping in mind that Unicode strings use twice as many bytes as ANSI strings, it makes sense that the standard string conversion functions won't work. Thankfully, the standard library provides us with alternatives:


// The simplest way to accomplish your goal is to use the 'wcstol' function.
// (wide character string to long)

// convert a CString to a long using base 10, with NULL as the terminating character
CString textValue = "12345";
long longVal = wcstol(textValue, NULL, 10);


There are a number of other wide character conversion functions available, a little time spent poking around eVC++'s help file should prove enlightening.

PPCdev
02-16-2003, 11:22 PM
That makes a lot of sense, thanks for the info, and the code.

// The simplest way to accomplish your goal is to use the 'wcstol' function.
// (wide character string to long)

// convert a CString to a long using base 10, with NULL as the terminating character
CString textValue = "12345";
long longVal = wcstol(textValue, NULL, 10);


Now I understand what "wcstol" stands for! Not so cryptic now, all I have to do is figure out the other 999 commands and functions! :-)

Thanks again,
Andrew