Convert nsString to wstring

Hi all ! I have a problem with a convert function in my XPCOM
component :

wstring nsTimmy::ConvertToWString(nsString * source)
{
	wstring dest;

	const PRUnichar *begin, *end;
	wchar_t wideChar;

	begin = (*source).BeginReading();
	end = (*source).EndReading();

	while(begin != end)
	{
		wideChar = (wchar_t)*begin;
		dest.append(&wideChar);
		++begin;
	}

	return dest;
}

This function works with Windows and visual studio compiler but not
with linux and GCC 4.1. It builds and doesn't crash at execution but
the characters returned in dest are juste wrong.

Thanks.
0
mathieu
1/22/2008 10:39:14 AM
mozilla.dev.tech.xpcom 1345 articles. 0 followers. Post Follow

5 Replies
1257 Views

Similar Articles

[PageSpeed] 2

mathieu.bastian@gmail.com wrote:
> Hi all ! I have a problem with a convert function in my XPCOM
> component :
<snip>
> This function works with Windows and visual studio compiler but not
> with linux and GCC 4.1. It builds and doesn't crash at execution but
> the characters returned in dest are juste wrong.
> 
> Thanks.

PRUnichar are two bytes each.  Gcc by default uses 4 byte long wchar_t 
(Mozilla builds with -fshort-wchar to make wchar_t two bytes long).  I'm 
guessing your XPCOM component is using -fshort-wchar but whoever is 
reading the string on the other end isn't?

-- 
Mook
mook dot moz plus bleah
0
Mook
1/22/2008 4:19:57 PM
> PRUnichar are two bytes each.  Gcc by default uses 4 byte long wchar_t
> (Mozilla builds with -fshort-wchar to make wchar_t two bytes long).  I'm
> guessing your XPCOM component is using -fshort-wchar but whoever is
> reading the string on the other end isn't?
>

I tried print sizeof(wchar_t) in my xpcom component execution and it
is both 2 on windows and linux so I guess it is not that

0
mathieu
1/22/2008 9:45:57 PM
mathieu.bastian@gmail.com wrote:
> 		dest.append(&wideChar);

Is |dest| correct after the first append call?

> 	return dest;

Is |dest| correct before that return statement?

-Boris

0
Boris
1/22/2008 10:37:49 PM
> Is |dest| correct after the first append call?

Indeed the problem seems to be here. The first append call is correct
but size returned by dest is not.

For one characted appended dest.size() return :
- 1 for windows
- 8 for linux

So under linux my wstring contains 1 correct character and then 7
uncorrect character and then 1 correct character...
0
mathieu
1/23/2008 8:45:39 PM
mathieu.bastian@gmail.com wrote:
>> Is |dest| correct after the first append call?
> 
> Indeed the problem seems to be here. The first append call is correct
> but size returned by dest is not.
> 
> For one characted appended dest.size() return :
> - 1 for windows
> - 8 for linux

So your call is:

   dest.append(&wideChar);


But the append() that takes a wchar* expects a null-terminated sequence 
of wchars.  It'll append everything starting at the pointer you pass it, 
up to the first null it finds (which will depend on the exact bytes on 
the stack).

Seems to me you either want to use some method that takes wchar_t 
instead of wchar_t*, or call

   dest.append(&wideChar, 1);

to have it only append a single char.

-Boris
0
Boris
1/23/2008 11:10:32 PM
Reply:

Similar Artilces:

Purpose of dev-tech-xpcom
What's the purpose of this list? I used to think it was about XPCOM itself, basically for questions about the code in xpcom/**. However, that doesn't seem to be how everyone else uses it. In fact, it seems to be about all coding questions that are somewhat Firefox- or XULRunner-related. Basically what the platform or extension lists are supposed to be. Should we, therefore, delete this list/newsgroup? As it is used, it is redundant. -christian -- All the world's a stage, And all the men and women merely players: They have their exits and their entrances...

Merging dev-tech-layout into dev-platform
Today I found out that I had missed a post from David Baron on dev-tech-layout, because I had no idea that this list exists. I think dev-platform is a better place to have the conversation related to the layout module (people are already having discussions about other modules over there). Does anybody have any objections? Cheers, Ehsan ...

Merging dev-tech-layout into dev-platform
Today I found out that I had missed a post from David Baron on dev-tech-layout, because I had no idea that this list exists. I think dev-platform is a better place to have the conversation related to the layout module (people are already having discussions about other modules over there). Does anybody have any objections? Cheers, Ehsan ...

WString Convert
Hi! what ist the best methode to convert wstrings to wlong/wfloat and back to wstring. Thanks to all who reply... Thomas Lauer wrote: > > Hi! > > what ist the best methode to convert wstrings to wlong/wfloat and back > to wstring. > > Thanks to all who reply... WString class has methods to convert to long\floats. The other way around,I guess, you'll have to use sprintf to populate your string ... Regards, Hari Stirbet Sybase, Inc. Tomas, I like to use the Sprintf method of WString to convert longs and floats to strings. The WS...

com to xpcom convertion
Hi, Is there a way fo conversion ms COM application to be ised as XPCPM in Firefox? or you need to rewrite the whole thing. thanks ...

UI dev tools/tech
Hello,  I am newbie to .NET. I would like to know is there any tools or technologies in .NET world that will help me to develop a thin client (browser based web application) rapidly. I am looking something like drag n drop for UI components or some tags where I no need to worry about developing these components. I want to just use these tags or drag n drop them and concentrate on my business logic of getting data.  Thanks for your help.--Vijay Have you checked out Visual Studio? It is an IDE that helps you drag n drop controls on your page?RegardsBilal Hadiar, MCP, MCTS, MCPD, MC...

Trouble with dev-tech-crypto
I'm having trouble posting to this list. I'm trying to get an announcement posted, but the messages simply disappear without errors. If you end up seeing my messages multiple times, please apologize. This issue is being tracked in bugzilla at mozilla dot org number 839245. (Not including a link, just in case links are the reason why mails are being filtered.) Kai ...

convert nsString to char * in VC6
Hi I want to return nsIDOMNode->->GetNodeName(name); (where name is an nsString), in a char* form. How can I get the char * content from an nsString. Thank's for your responses philippe philippe wrote: > Hi > > I want to return nsIDOMNode->->GetNodeName(name); (where name is an > nsString), in a char* form. > > How can I get the char * content from an nsString. > > > Thank's for your responses > > philippe > Hi Philippe, You'll probably have better luck in a developer newsgroup, or an ext...

Merging dev-gaia and dev-b2g into dev-fxos
--001a113ce93ebce35d051e4c0c73 Content-Type: text/plain; charset=UTF-8 Hello people of Firefox OS, After a discussion we have decided that the distinction between dev-gaia and dev-b2g mailing lists is not enough to warrant maintaining two lists. So we are deprecating both in favor of dev-fxos. So if you are subscribed to one of the aforementioned lists, you will be subscribed to the new dev-fxos list and we will shortly be decommissioning dev-gaia and dev-b2g. Thanks! Michael --001a113ce93ebce35d051e4c0c73 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: qu...

Removing dev.tech.gfx
I've just filed bug 685268 to remove dev.tech.gfx, with the intent being that all such discussion should be happening on dev.platform. This is how all the Mozilla graphics folk are operating already; we may as well pave that particular path. Joe ...

newbie question
i'm planning to writing an XPCOM component is javascript. 1. how does javascript know to call the NSGetModule() method when a file is loaded. it always does it? 2. if i'm writing some code in c++, i assume i have to compile it for different platforms. does this result in different platform specific extension (.xpi) files or is it one file and the loader figures out the right version (i.e. subfoler) to load based on platform. 3. is there an online source for detailed docs on all the components and interfaces. thx. dashman wrote: > i'm planning to writin...

Merging dev-gaia and dev-b2g into dev-fxos
--001a113ce93ebce35d051e4c0c73 Content-Type: text/plain; charset=UTF-8 Hello people of Firefox OS, After a discussion we have decided that the distinction between dev-gaia and dev-b2g mailing lists is not enough to warrant maintaining two lists. So we are deprecating both in favor of dev-fxos. So if you are subscribed to one of the aforementioned lists, you will be subscribed to the new dev-fxos list and we will shortly be decommissioning dev-gaia and dev-b2g. Thanks! Michael --001a113ce93ebce35d051e4c0c73 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: qu...

dev-tech-crypto moderation changes
Since its origin a few months ago, the dev-tech-crypto@lists.mozilla.org list has been moderated, in an effort to keep spam out of the list. The rules have been rather simple: mail sent by list members gets right through, the rest goes into a moderation queue where it is reviewed daily. The occasional on-topic message from a non-subscriber address gets manually passed through, and the rest is discarded. Spam filters (are intended to) recognize most spam and discard it outright, so that the moderator need not review hundreds of spams daily to find the occasional rare nuggets. Un...

I get no response in dev.tech.css
page at http://www.mrkuhn.com should forward to http://www.mrkuhn.com/default2.htm, works with IE but not firefox. if anyone would let me know how to do a forward that's compatable for both it would be greatly appreciated. I also use resizeable iframes, which work properly for fitting with htm files but leave extra blank space on my aspx pages. If anyone knows a fix for that it would also be a big help. Matthew Kuhn wrote: > page at http://www.mrkuhn.com should forward to > http://www.mrkuhn.com/default2.htm, works with IE but not firefox. if > anyone would let ...

Web resources about - Convert nsString to wstring - mozilla.dev.tech.xpcom

Apple Emoji / iterate through NSString
Apple Emoji / iterate through NSString

Beginning Objective C for iPad on the iTunes App Store
Get Beginning Objective C on the App Store. See screenshots and ratings, and read customer reviews.

Twitter Engineering: Mobile app development: Catching crashers
Thursday, May 30, 2013 Mobile app development: Catching crashers Before Twitter for iOS code reaches production, we run it through static analysis, ...

Objective-C - Wikipedia, the free encyclopedia
.h, .m, .mm Objective-C is a general-purpose , high-level , object-oriented programming language that adds Smalltalk -style messaging to the ...

0xced
skip to main - skip to sidebar [Cocoa hack]; Sunday, January 04, 2009 Using your own address book in the iPhone Simulator Jailbroken iPhone with ...

objective-c怎么将WCF的DateTime类型转换成NSDate
NSRange begin = [dateString rangeOfString:"("];NSRange end = [dateString rangeOfString:"+"];NSString* milliSecondsSince1970 = [dateString su ...

Apple pushes iOS 6.0 Beta 2 OTA update to developers
Not available yet on Apple’s Dev site, Developers are pinging us that a 299MB -iPhone4S/322MB iPhone 4 update is available for iOS 6 via OTA ...

IPC Made Easy: Introducing XPC and NSXPCConnection
Some new classes were introduced in Foundation.framework as part of Mac OS X 10.8 (Mountain Lion) to help ease the pain associated with performing ...

Apple Posts Release Notes For iOS 7 Beta 3
Apple has published the release notes for the third iOS 7 beta 3. Take a look: Introduction iOS SDK 7.0 provides support for developing iOS ...

NSCharacterSet Class Reference
Describes a set of Unicode characters used in scanning and searching operations. An NSCharacterSet object represents a set of Unicode-compliant ...

Resources last updated: 1/22/2016 4:59:30 PM