Again About HBuf, TDesC, 8bits, 16bits...
| Fri, 2003-10-31 10:41 | |
|
Hello everybody,
to explain my problem: in a function i get a HBufC16* result and this function can only return this type, the following line (theorically), i need to give a TDesC8& argument.. Does it exist any mean to "convert" HBufC16 to TDesC8 knowing that i cannot change these types in the two functions. edit: if somebody could give me a sample code converting 16-bits coded data to 8-bits coded data.. i hadn't succeeded using the class provided in the SDK Thanx in advance, Carlos. |
|






Forum posts: 5
Good luck!
Forum posts: 17
Forum posts: 723
Here is a code snippet from my code. Enjoy!
{
HBufC8* buf = HBufC8::NewLC( aString.Length() );
buf->Des().Copy( aString );
return buf;
}
tOtE
Ps.: anyway, your compiler was right ==> this method is defined in TDes8 and not in its base class (i.e. TDesC8).
Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/
Forum posts: 17
For the moment i'm cheating using some TBuf<> and CnvUtfConverter::ConvertFromUnicodeToUTF8, i'm not sure that's the right way but it seems to work.
But i'll try your solution too
So if i've well understood (i'm a beginner
thanx
Forum posts: 1918
A HBufC is a TDesC so you can use one whenever a TDesC is required.
(and obviously the reciprocal is not true: you cannot use a TDesC when a HBufC is required since HBufC is a richer class).
I also use generally the CnvUtfConverter stuff to do this kind of conversion.
Cheers,
Eric
Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant
Forum posts: 17
So i create a
knowing that Name16 is a HBufC16*.
I've noticed that only the two first characters are well converted,
if Name16 contains "Carlos" this code will return Ca followed by 4 odd characters.
Anyone got an idea??
thanx in advance ..
Forum posts: 723
So i create a
knowing that Name16 is a HBufC16*.
I've noticed that only the two first characters are well converted,
if Name16 contains "Carlos" this code will return Ca followed by 4 odd characters.
Anyone got an idea??
thanx in advance ..
Hi,
I don't why the above symptom exists, but your code should look like as follows :
TInt err = CnvUtfConverter::ConvertFromUnicodeToUtf8( Name8, *Name16 );
if ( err != KErrNone )
{
// do some error handling
}
Note : the SDK doc says that the above method is withdrawn. I don't know why this happened, but you should also consider this fact.
Cheers,
tOtE
Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/
Forum posts: 17
I don't why the above symptom exists, but your code should look like as follows :
TInt err = CnvUtfConverter::ConvertFromUnicodeToUtf8( Name8, *Name16 );
if ( err != KErrNone )
{
// do some error handling
}
Note : the SDK doc says that the above method is withdrawn. I don't know why this happened, but you should also consider this fact.
Cheers,
tOtE
i forgot to mention it but obviously i've tried to check out the value which results of the conversion function and i've made a handler like i found on a website:
{
User::Leave(KErrCorrupt);
}
else if (err != 0)
{
User::Leave(KErrGeneral);
}
but i always have 0 with 2-chars data, 3-chars data... and in all cases the only 2 first chars are well-converted.
>tOtE
i've not read in my devlib that this method is withdrawn, are you sure of this? if yes, do you have a up-to-date method, because i've tried a lot of solutions and this one was the first to give some results!
Thanx to all,
Forum posts: 17
I'm sure you know the solution
Forum posts: 17
I'm dealing again with descritptors and after a big period of reflexion, i've the same problem:
In a function i get back a HBufC16* and i need to send the datas to a socket.
The problem is that the socket could only accept 8bits data, so i'm searching the best (and also the working) way to do it...
I need to convert HBufC16 in TDesc8...
Some people have already replied but none of their solutions worked well enough
Thanx to all!!!
Forum posts: 11
Some people have already replied but none of their solutions worked well enough
Thanx to all!!!
take a look at the "Sending string via BlueTooth" thread. That's what i do, to deal with the conversion.
Forum posts: 17
The problem is that only the first char were successfully converted, knowing that there were only letters and numbers, i think it's odd, but i will continue to search...
Thanx
Forum posts: 1918
A+
Eric
Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant
Forum posts: 17
i wanna convert aHBufC16 into TDesC8
the cast weren't a good idea?
TInt aRet1; //return code
aRet1=CnvUtfConverter::ConvertFromUnicodeToUtf8(aUtf8,*aHBufC16);
if (aRet1 == CnvUtfConverter::EErrorIllFormedInput) //here i'm testing the result of the conv
{
User::Leave(KErrCorrupt);
}
else if (aRet1 != 0)
{
User::Leave(KErrGeneral);
}
Otherwise, i was thinking to acces directly the memory, byte by byte to fetch the bytes i need to send through the socket but i didn't find anything about how doing it...
thanx to all!!!
Forum posts: 1918
Eric
Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant