conversion between char* and Descriptors

Login to reply to this topic.
Wed, 2005-08-10 10:04
Joined: 2005-02-18
Forum posts: 16
hi all
   I use s60 0.9 for 7650!Now how i conver between char* and  Descriptors?Huh

and  char* is for C;

who can help me ??

thx!!!!

Wed, 2005-08-10 12:28
Joined: 2004-12-03
Forum posts: 192
Re: conversion between char* and Descriptors
Code:
TPtrC8 buf(pString, User::StringLength(pString));
Thu, 2005-08-11 04:43
Joined: 2005-02-18
Forum posts: 16
Re: conversion between char* and Descriptors
thanks for your help!!

but how to conve form TPtrC to char*.


thanks very much!!!

Thu, 2005-08-11 06:44
Joined: 2004-12-03
Forum posts: 192
Re: conversion between char* and Descriptors
easy Smiley just read documentation
Code:
TInt len = ptrc.Length();

Mem::Copy(pString, ptrc.Ptr(), len);
pString[len] = 0;

and Ptr() function is:
Code:
const TUint8* Ptr() const


Thu, 2005-08-25 00:53
Joined: 2005-06-13
Forum posts: 68
Re: conversion between char* and Descriptors
I was wondering...

Why isn't there a default constructor for TPtr?

What if you wanted to do something like this:

Code:
void method(TPtr& aPtr)
{
   aPtr = _L("blob") ;
}

TPtr ptr ;
method(ptr) ;
// now ptr points to "blob"

The only I can see of doing something similar is like this:

Code:
void method(HBufC*& aBuf)
{
   aBuf = HBufC16::NewL(4) ;
   aBuf->Des() = _L("blob") ;
}

HBufC* buf ;
method(buf) ;
// now buf points to "blob"

Anyone has any better ideas of how do this?

Anyone has a good explanation of why TPtr has no default contructor?

Cheers,

Nikolas.

If we fall down it's so we can learn to pick ourselves up.

Wed, 2005-10-12 22:53
Joined: 2005-07-15
Forum posts: 21
Re: conversion between char* and Descriptors
Hi
    I tried the above method, but it dint work for me.
here is the code snippet

"buffer" is TBuf8 type and "temp" is unsigned char*

               counter = buffer.Length();
   Mem::Copy(temp, buffer.Ptr(), counter);    //Crashes here


Any help is highly appreciated.
   
   
Tue, 2005-11-08 09:10
Joined: 2005-08-08
Forum posts: 1
Re: conversion between char* and Descriptors
Quote from: fastsymbian
Hi
    I tried the above method, but it dint work for me.
here is the code snippet   

I test it. It work for me.

Tue, 2005-11-08 12:44
Joined: 2005-07-29
Forum posts: 12
Re: conversion between char* and Descriptors
Quote from: fastsymbian
    I tried the above method, but it dint work for me.
here is the code snippet

"buffer" is TBuf8 type and "temp" is unsigned char*

               counter = buffer.Length();
   Mem::Copy(temp, buffer.Ptr(), counter);    //Crashes here

Probably you aren't allocate space for 'temp'. You need some code like:

temp = new(ELeave) unsigned char[counter + 1];

before Mem::Copy()
(counter + 1 at minimum, if you copy strings (there must be enought space for data + terminating zero byte!) If this is the case, after Mem::Copy() you must add also:

temp[counter] = 0;

Fri, 2005-12-09 07:39
Joined: 2005-11-30
Forum posts: 11
Re: conversion between char* and Descriptors
Well my code is working fine with -

char *chStr  = new char[20];

TBufC8<50> buf8((unsigned char*)chStr);
         
TBuf16<50> buf16;
buf16.Copy(buf8);

iAppContainer->SetTextL(buf16);

Simple !!!! Roll Eyes
Fri, 2005-12-16 13:20
Joined: 2005-12-16
Forum posts: 3
Re: conversion between char* and Descriptors
say--
funC(const TDesC& ades)
{
char* des= reinterpret_cast<char* > (ades.Ptr());
}

Ptr() converts descriptor to TUint . then u can type cast...
U can avoid reinterpret casting and use c style casting but prefer reinterpret casting...
Fri, 2005-12-16 13:21
Joined: 2005-12-16
Forum posts: 3
Re: conversion between char* and Descriptors
say--
funC(const TDesC& ades)
{
char* des= reinterpret_cast<char* > (ades.Ptr());
}

Ptr() converts descriptor to TUint . then u can type cast...
U can avoid reinterpret casting and use c style casting but prefer reinterpret casting...
Sun, 2005-12-18 08:23
Joined: 2005-02-12
Forum posts: 98
Re: conversion between char* and Descriptors
Why not we discuss in real terms...
i think that would give the best understanding.

for example:
char str[5]; 
// Above statement allocates five continuous bytes (say memory address 100 to 104 if each char takes one byte)

char * ptr;
 // ptr is pointer variable say it's address is 1000 but content is yet to set
ptr=str; // now ptr  has the content 100
using ptr we can access and modify array str as following
for(TInt i=0;i<5;i++)
ptr[i]='a';

Now We should think like above for symbian descriptors
TBuf<5>buf; 
//Above should also allocate cont. memory of 10 bytes(So object size is 10 .Right or Wrong?)
also buf[i] gives ith character in buffer;

so &buf[0] will be base address;
so Is following right
char *ptr;
ptr=(char *)&buf[0];

Plz have the disscussion to understand the concepts not just how to use them.
 

Thu, 2006-01-26 16:10
Joined: 2006-01-26
Forum posts: 4
Re: conversion between char* and Descriptors
Quote from: Core[RUS]
easy Smiley just read documentation
Code:
TInt len = ptrc.Length();

Mem::Copy(pString, ptrc.Ptr(), len);
pString[len] = 0;
//               should this be '\0'?
and Ptr() function is:
Code:
const TUint8* Ptr() const



Tue, 2006-02-07 14:23
Joined: 2005-02-24
Forum posts: 276
Re: conversion between char* and Descriptors
This  thread was becoming interesting, why di it stop?  Huh

Now that I have ptr = "aaaa" and buf is an empty 5-chars descriptor, how do I put ptr contents inside buf?

-- JumpJack --

Wed, 2006-03-01 06:06
Joined: 2005-02-12
Forum posts: 98
Re: conversion between char* and Descriptors
I think following is most safe way


char *str="vikas"; //C Style constant string
char string[6];
TBuf<5>buffer;

strcpy(string,str);   // can be used beacuse of estdlib.lib

for(int i=0;i<strlen(str);i++)
buffer.Append(str[i]);

//other option may be buffer.Copy(TUInt16 *(str));


Sat, 2006-03-25 17:08
Error (not verified)
Forum posts: 2018
Re: conversion between char* and Descriptors
am i doing correct?Huh?


TPtrC& pBody = _L("test source data");         
const char* srcData = (const char*)pBody.Ptr();

did try this way too:
const char* srcData= reinterpret_cast<const char*>(pBody.Ptr());

only "t"  is being copied into srcData Sad



How can i convert :  char dstData[32]; --->  TDesC
  • Login to reply to this topic.