Copying the contents of a wxString

Michiel.Salters at tomtom.com Michiel.Salters at tomtom.com
Fri Sep 8 05:44:11 PDT 2006


Peter Gordon wrote:
> I need to copy the contents of a wxString to a separate, non-wxString
> buffer.

Do you need to add a \0? wxString is a C++ class and doesn't need one,
but C "strings" do need them.

> The pointer is wxString::c_str(), but what is the length that needs to
> be copied assuming that I am using Unicode? Do I need to take
> wxString::Len(),
> wxString::Len() * sizeof(w_char),
> wxString::Len() * sizeof(w_char) + 1,
> or what?

The first two could be correct. ::Len gives you the number of wchar_t's
(in the unicode case). ::Len() * sizeof(w_char) gives you the number of
bytes. However, it's more portable to use * sizeof(wxString[0]).

The third looks like you copy to a C-string buffer, except that you
need to
add sizeof(wchar_t), not 1 as a wchar_t[] string is terminated with
L'\0'.
You could also write (wxString::Len() +1) * sizeof(wchar_t).

HTH,
Michiel Salters







More information about the wx-users mailing list