Again About HBuf, TDesC, 8bits, 16bits...

Login to reply to this topic.
Fri, 2003-10-31 10:41
Joined: 2003-09-26
Forum posts: 17
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.

Fri, 2003-10-31 21:10
Joined: 2003-10-27
Forum posts: 5
Again About HBuf, TDesC, 8bits, 16bits...
Use TDesC8::Copy(const TDesC16& aDes);

Good luck! Smiley
Mon, 2003-11-03 10:39
Joined: 2003-09-26
Forum posts: 17
Again About HBuf, TDesC, 8bits, 16bits...
i've tried this solution but borland told me that Copy weren't a member of TDesC8...
Mon, 2003-11-03 16:47
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Here is a snippet
Quote from: wintermelaun
i've tried this solution but borland told me that Copy weren't a member of TDesC8...

Here is a code snippet from my code. Enjoy!

Code:
HBufC8* CSomething::UnicodeToAsciiLC( const TDesC16& aString ) const
{
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/

Tue, 2003-11-04 10:22
Joined: 2003-09-26
Forum posts: 17
Again About HBuf, TDesC, 8bits, 16bits...
So Tina's solution is wrong?

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  Smiley

So if i've well understood (i'm a beginner  Embarassed ) when you must use a HBuf you can use a TDesC and the contrary ?



thanx
Tue, 2003-11-04 10:57
NewLC AdministratorSymbian AccreditedForum Nokia Champion
Joined: 2003-01-14
Forum posts: 1918
Again About HBuf, TDesC, 8bits, 16bits...
You have the descriptors inheritance graph there : http://www.newlc.com/article.php3?id_article=12

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

Tue, 2003-11-04 12:10
Joined: 2003-09-26
Forum posts: 17
Again About HBuf, TDesC, 8bits, 16bits...
Eric, thanx for these precisions.

So i create a
Code:
TBuf<20> Name8
and then i use
Code:
CnvUtfConverter::ConvertFromUnicodeToUtf8(Name8,const_cast<HBufC16>(*Name16));
,
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 ..
Wed, 2003-11-05 14:40
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Correction
Quote from: wintermelaun
Eric, thanx for these precisions.

So i create a
Code:
TBuf<20> Name8
and then i use
Code:
CnvUtfConverter::ConvertFromUnicodeToUtf8(Name8,const_cast<HBufC16>(*Name16));
,
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 :

Code:
TBuf8<20> Name8;
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/

Wed, 2003-11-05 18:49
Joined: 2003-09-26
Forum posts: 17
Re: Correction
Quote from: tote
Hi,

I don't why the above symptom exists, but your code should look like as follows :

Code:
TBuf8<20> Name8;
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:

Code:
if (err == CnvUtfConverter::EErrorIllFormedInput)
{
  User::Leave(KErrCorrupt);
}
else if (err != 0)
{
  User::Leave(KErrGeneral);
}
(edit: knowing that err is defined as above in tOtE's code)


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,
Mon, 2003-11-17 13:54
Joined: 2003-09-26
Forum posts: 17
Again About HBuf, TDesC, 8bits, 16bits...
Nobody can help me?
I'm sure you know the solution  Cheezy
Mon, 2003-12-08 15:34
Joined: 2003-09-26
Forum posts: 17
Again About HBuf, TDesC, 8bits, 16bits...
Hi everybody!!

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  Cry

Thanx to all!!!
Mon, 2003-12-08 18:54
Joined: 2003-11-26
Forum posts: 11
Again About HBuf, TDesC, 8bits, 16bits...
Quote from: wintermelaun
I need to convert HBufC16 in TDesc8...

Some people have already replied but none of their solutions worked well enough  Cry
Thanx to all!!!

take a look at the "Sending string via BlueTooth" thread. That's what i do, to deal with the conversion.
Tue, 2003-12-09 14:17
Joined: 2003-09-26
Forum posts: 17
Again About HBuf, TDesC, 8bits, 16bits...
i agree with you but i've ever tried this solution.
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
Tue, 2003-12-09 21:15
NewLC AdministratorSymbian AccreditedForum Nokia Champion
Joined: 2003-01-14
Forum posts: 1918
Again About HBuf, TDesC, 8bits, 16bits...
You should re-post the code you are using, most of the method described here are working. Note also that you should not have to cast anyhing to achieve the conversion (that's the case in one of the code snippet you posted before).

A+
Eric

Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant

Wed, 2003-12-10 10:29
Joined: 2003-09-26
Forum posts: 17
Again About HBuf, TDesC, 8bits, 16bits...
here's a snippet from my code:
i wanna convert aHBufC16 into TDesC8
the cast weren't a good idea?

Code:
TBuf8<12> aUtf8; //
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!!!
Wed, 2003-12-10 13:27
NewLC AdministratorSymbian AccreditedForum Nokia Champion
Joined: 2003-01-14
Forum posts: 1918
Again About HBuf, TDesC, 8bits, 16bits...
This code is working perfectly for me (assuming that aHBufC16 is of type HBufC*).

Eric

Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant

  • Login to reply to this topic.