On 8/13/2010 3:49 AM, Alex yu wrote:
> Is it legal to convert char* type to unsigned char* type?

Sure!  I do that all the time.


> does the conversion from char* to unsigned char* cause any loss of data,
> including length?

No.


> I've got a function named build which takes a unsigned char* and its length.
> when I read raw data directly from disk like :
>      fseek(fp, 0, SEEK_END);
>      long lSize = ftell(fp);
>      rewind(fp);
>      BYTE * buf = (BYTE*) malloc(sizeof(unsigned char)*lSize);
> then pass buf and lSize to build , it totally works.
>
> However, when I hard code the content in the source like :
>      string origin= "this is a text";
>      long lSize = origin.size();
>      unsigned char *buf = (unsigned char*) malloc(sizeof(unsigned
> char)*lSize);
 >      char *strs = const_cast<char*>(origin.data());
 >      buf = reinterpret_cast<unsigned char*>(strs);
 > it failed .


I'm definitely not a fan of mixing C and C++ these days.  You should 
look at using new and delete.  Results in cleaner code instead of being 
forced to typecast all over the place.

-- 
Thomas Hruska
CubicleSoft President

Barebones CMS is a high-performance, open source content management 
system for web developers operating in a team environment.

An open source CubicleSoft initiative.
Your choice of a MIT or LGPL license.

http://barebonescms.com/

Reply via email to