I need to convert string type to unsigned char..
My intension is to assign a token that is read as a string to
unsigned char variable.
hope somebody can help me in this..
thanks in advance
Sreeraj
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
You need some kind of table where each string is assigned an unsigned char
value. How do you want your strings translated into the character?
smn
sreeraj wrote:
> Hello there..
>
> I need to convert string type to unsigned char..
>
> My intension is to assign a token that is read as a string to
> unsigned char variable.
Depending on what you mean by "assign" and "cnvert":
1)
std::string token;
if ( cin >> token ) {
const char* charToken = token.c_str();
// here you can read from charToken (can not modify it though)
// as long as token is not modified.
}
2)
std::string token;
if ( cin >> token ) {
char* charToken = strdup( token.c_str() );
// here charToken holds a dynamically allocated copy of token
// contents. Do whatever you want with it, do not forget to
// deallocate
const unsigned char* pch = (unsigned char*)myString.c_str();
If you need a copy of the string, do something like:
unsigned char* theCopy = (unsigned char*)strdup( myString.c_str());
// use it
free( theCopy); // don't forget to free the memory.
In the second example, I didn't check for the validity of the pointer
returned by c_str(). You should. String operations on NULL pointers always
crash the application...
Catalin Pitis
"sreeraj" <sr...@biztelone.com> wrote in message
news:7d35db59.02071...@posting.google.com...
> Hello there..
>
> I need to convert string type to unsigned char..
>
> My intension is to assign a token that is read as a string to
> unsigned char variable.
>
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
> If you need a read-only string do something like:
>
> const unsigned char* pch = (unsigned char*)myString.c_str();
>
>
> If you need a copy of the string, do something like:
>
> unsigned char* theCopy = (unsigned char*)strdup( myString.c_str());
>
> // use it
>
> free( theCopy); // don't forget to free the memory.
>
> In the second example, I didn't check for the validity of the pointer
> returned by c_str(). You should. String operations on NULL pointers always
> crash the application...
>
>
> Catalin Pitis
>
>
> "sreeraj" <sr...@biztelone.com> wrote in message
> news:7d35db59.02071...@posting.google.com...
> > Hello there..
> >
> > I need to convert string type to unsigned char..
> >
> > My intension is to assign a token that is read as a string to
> > unsigned char variable.
> >
>
>
for a copy,a vector<unsigned char> is safer than a char array created
by malloc()[via strdup()]
std::vector<unsigned char> uc(myString.begin(),myString.end());
uc.push_back('\0');
unsigned char *p = &uc.front();
is much safer than strdup().
There are two problems with this:
1) strdup is not part of the Standard C++ library even though it is
frequently provided on Unix systems.
2) strdup usually uses some variation of malloc for its implemention and
leaves the programmer responsible for the memory clean-up. What makes
this worse is that someone writing a home-rolled version might use new[]
and thereby provide the trap of calling free for something that was
created with new[].
Writing correct, robust and portable code may be a little more tedious,
but it has the advantage of being all those things and so is the
profession solution.
If you need to do this often, it is possibly worth writing an
appropriate function which returns some form of smart pointer. If you
just need to use unsigned char * as an argument for a function call the
value returned by c_str() will be valid long enough for your purpose.
However you would need to watch for problems with aliasing and
multi-threading.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
> If you need a read-only string do something like:
But, please, do it the C++ way! Not:
> const unsigned char* pch = (unsigned char*)myString.c_str();
but:
const unsigned char *pch
= reinterpret_cast<const unsigned char*>(MyString.c_str());
and not:
> unsigned char* theCopy = (unsigned char*)strdup( myString.c_str());
>
> // use it
>
> free( theCopy); // don't forget to free the memory.
but:
unsigned char *const theCopy = new unsigned char[myString.size()+1];
strcpy(theCopy,myString.c_str());
// use it
delete[] theCopy;
or use a smart pointer so you won't have to call delete[] yourself.
(Note that strdup isn't a standard function, neither in C++ nor in C.)
> String operations on NULL pointers always crash the application...
I wish they would. They rather give you undefined behaviour which
can do anything (including, perhaps, a crash).
Regards
W. Roesler