You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by pundog <pu...@gmail.com> on 2007/12/03 10:03:11 UTC
Re: Converting XMLCh* to std::string with encoding
Alberto Massari wrote:
>
> As you can read here
> http://xerces.apache.org/xerces-c/apiDocs/classXMLTranscoder.html#b9d5409d562aa54f99dc01617091c457,
> the signature is
>
> virtual unsigned int XMLTranscoder::transcodeFrom( const XMLByte
> <http://xerces.apache.org/xerces-c/apiDocs/XercesDefs_8hpp.html#7470c7a32c59355685ebcd878a33f126>
> *const srcData, const unsigned int srcCount, XMLCh *const toFill, const
> unsigned int /maxChars/, unsigned int & /bytesEaten/, unsigned char
> *const /charSizes)/
> //
> //It takes XMLByte* (i.e. unsigned char*, that you should get from
> std::string.c_str() ) and fills an XMLCh buffer of maxChars.
>
> Alberto
>
Hi, first of all, thanks for your reply
I tried to use the "transcodeFrom" function using the following code:
XMLByte* xmlBytes = (unsigned char*)MY_STRING.c_str();
size_t stringLength = str.length();
XMLCh* xmlChars = new XMLCh[stringLength];
XMLTranscoder* utf8Transcoder ;
XMLTransService::Codes failReason;
utf8Transcoder =
XMLPlatformUtils::fgTransService->makeNewTranscoderFor("UTF-8", failReason,
16*1024);
unsigned int eaten;
unsigned char* charSizes = new unsigned char[stringLength];
unsigned int xmlCharsLength = utf8Transcoder->transcodeFrom(xmlBytes,
stringLength, xmlChars, stringLength, eaten, charSize);
delete[] charSizes;
delete utf8Transcoder;
...
XMLString::release(xmlChars);
The problem with this code is that for each string that i transcode i get
some weird chars from the right side. For example, if i transcode the string
"Hello", then my XMLCh* will be "Hello$$$$".
I tried the place a null ('\0') at the end of the XMLCh but it causes an
exception to be thrown when i try to call XMLString::release, to deallocate
it's memory.
How can i solve this?
again, thanks for your help :)
--
View this message in context: http://www.nabble.com/Converting-XMLCh*-to-std%3A%3Astring-with-encoding-tf4807661.html#a14125854
Sent from the Xerces - C - Users mailing list archive at Nabble.com.
Re: Converting XMLCh* to std::string with encoding
Posted by Alberto Massari <am...@datadirect.com>.
Hi,
you need to allocate at least one extra character (i.e. stringLength+1)
to store the NULL character.
Alberto
pundog wrote:
>
> Alberto Massari wrote:
>
>> As you can read here
>> http://xerces.apache.org/xerces-c/apiDocs/classXMLTranscoder.html#b9d5409d562aa54f99dc01617091c457,
>> the signature is
>>
>> virtual unsigned int XMLTranscoder::transcodeFrom( const XMLByte
>> <http://xerces.apache.org/xerces-c/apiDocs/XercesDefs_8hpp.html#7470c7a32c59355685ebcd878a33f126>
>> *const srcData, const unsigned int srcCount, XMLCh *const toFill, const
>> unsigned int /maxChars/, unsigned int & /bytesEaten/, unsigned char
>> *const /charSizes)/
>> //
>> //It takes XMLByte* (i.e. unsigned char*, that you should get from
>> std::string.c_str() ) and fills an XMLCh buffer of maxChars.
>>
>> Alberto
>>
>>
>
> Hi, first of all, thanks for your reply
> I tried to use the "transcodeFrom" function using the following code:
>
> XMLByte* xmlBytes = (unsigned char*)MY_STRING.c_str();
> size_t stringLength = str.length();
> XMLCh* xmlChars = new XMLCh[stringLength];
>
> XMLTranscoder* utf8Transcoder ;
> XMLTransService::Codes failReason;
> utf8Transcoder =
> XMLPlatformUtils::fgTransService->makeNewTranscoderFor("UTF-8", failReason,
> 16*1024);
>
> unsigned int eaten;
> unsigned char* charSizes = new unsigned char[stringLength];
> unsigned int xmlCharsLength = utf8Transcoder->transcodeFrom(xmlBytes,
> stringLength, xmlChars, stringLength, eaten, charSize);
>
> delete[] charSizes;
> delete utf8Transcoder;
> ...
> XMLString::release(xmlChars);
>
> The problem with this code is that for each string that i transcode i get
> some weird chars from the right side. For example, if i transcode the string
> "Hello", then my XMLCh* will be "Hello$$$$".
> I tried the place a null ('\0') at the end of the XMLCh but it causes an
> exception to be thrown when i try to call XMLString::release, to deallocate
> it's memory.
>
> How can i solve this?
> again, thanks for your help :)
>