[Solved] OUString to String and back

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

[Solved] OUString to String and back

Post by saleem145 »

Hello,

Quick question -- how do I convert or cast an OUString to string and vice versa??

Thanks,

Saleem
Last edited by saleem145 on Sun Jul 15, 2012 1:54 pm, edited 1 time in total.
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: OUString to String and back

Post by Charlie Young »

saleem145 wrote:Hello,

Quick question -- how do I convert or cast an OUString to string and vice versa??

Thanks,

Saleem
Working with c++ and OpenOffice, class OUString is your friend. It's often easier to manipulate OUStrings than char *s.

To go back and forth there are a number of tricks.

To make an OUString from a literal there is a macro

Code: Select all

OUString ou = OUString(RTL_CONSTASCII_USTRINGPARAM("literal"));
from a char * do

Code: Select all

char *myName = "Saleem";

OUString OUName = OUString::createFromAscii(myName);
OUString::createFromAscii also works with literals, but the macro is a bit more efficient in that case.

To go the other way, there is an intermediary called oString

Code: Select all

OUString myOUName = OUString(RTL_CONSTASCII_USTRINGPARAM("Saleem"))

OString o = OUStringToOString( myOUName, RTL_TEXTENCODING_ASCII_US );
char *c = o.pData->buffer;
Apache OpenOffice 4.1.1
Windows XP
karsten.burger
Posts: 4
Joined: Thu Jul 19, 2012 3:40 pm

Re: [Solved] OUString to String and back

Post by karsten.burger »

But how about wide-character strings? How do I get a std::wstring from an OUString?
Ooffice 3.1.1, Linux
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: [Solved] OUString to String and back

Post by Charlie Young »

karsten.burger wrote:But how about wide-character strings? How do I get a std::wstring from an OUString?
I just did a quick test, and I'm not sure I understand it, but it appears you can just do a direct assignment:

Code: Select all

wstring wstr;
OUString ouStr;

ouStr = OUString::createFromAscii("I'm an OUString");
		
wstr = ouStr;
And the other direction:

Code: Select all

wstr = L"Hi there!";
ouStr = wstr.data();
Apache OpenOffice 4.1.1
Windows XP
Post Reply