f



Handling Strings

Some third-party libraries I use require byte arrays, some char arrays
some std::strings. Normally, when taking user defined input that is
suitable as a string, I use std::string, but find myself casting a lot
to make what I have work with various libraries. I use old-fashioned c-
style cast and it works for the most part, but I'm uncomfortable with
all the casting (it feels wrong).

So I wanted to ask others, how do you handle these situations when you
have libraries that want a certain character type (that could just as
easily be a string) and all you have is std::strings?

Thanks
0
byte8bits (134)
6/28/2010 3:11:32 PM
comp.lang.c++ 49366 articles. 1 followers. Post Follow

8 Replies
504 Views

Similar Articles

[PageSpeed] 59

Brad <byte8bits@gmail.com>, on 28/06/2010 08:11:32, wrote:

> Some third-party libraries I use require byte arrays, some char arrays
> some std::strings. Normally, when taking user defined input that is
> suitable as a string, I use std::string, but find myself casting a lot
> to make what I have work with various libraries. I use old-fashioned c-
> style cast and it works for the most part, but I'm uncomfortable with
> all the casting (it feels wrong).
>
> So I wanted to ask others, how do you handle these situations when you
> have libraries that want a certain character type (that could just as
> easily be a string) and all you have is std::strings?

For converting std::string to C-style strings you use c_str(), for the 
reverse operation you build std::string from C-style strings - I have no 
idea of what you mean with "byte arrays" since C++ has no "byte" type.

I don't see any need for casting, and about C style casts in particular, 
you can (you should) always replace them with the equivalent C++ 
explicit casts - if casting is really needed.

You could take advantage of posting some example and of mentioning the 
libraries you're using, if this answer didn't satisfy your question.

-- 
FSC
http://userscripts.org/scripts/show/59948
0
Francesco
6/28/2010 3:18:43 PM
On Jun 28, 11:18=A0am, "Francesco S. Carta" <entul...@gmail.com> wrote:

> For converting std::string to C-style strings you use c_str(), for the
> reverse operation you build std::string from C-style strings - I have no
> idea of what you mean with "byte arrays" since C++ has no "byte" type.

Yes, I use c_str(). Some libraries define byte (an unsigned char).

> I don't see any need for casting, and about C style casts in particular,
> you can (you should) always replace them with the equivalent C++
> explicit casts - if casting is really needed.
>
> You could take advantage of posting some example and of mentioning the
> libraries you're using, if this answer didn't satisfy your question.

Here is an example from Crypto++:

CryptoPP::Weak::MD4 hash;
byte digest[ 16 ];

// Here is one example where I cast to make the std::string I have
work
hash.Update((const byte*)generated_string.c_str(),
generated_string.length());
hash.Final( digest );

0
Brad
6/28/2010 4:47:53 PM
Brad <byte8bits@gmail.com>, on 28/06/2010 09:47:53, wrote:

> On Jun 28, 11:18 am, "Francesco S. Carta"<entul...@gmail.com>  wrote:
>
>> For converting std::string to C-style strings you use c_str(), for the
>> reverse operation you build std::string from C-style strings - I have no
>> idea of what you mean with "byte arrays" since C++ has no "byte" type.
>
> Yes, I use c_str(). Some libraries define byte (an unsigned char).
>
>> I don't see any need for casting, and about C style casts in particular,
>> you can (you should) always replace them with the equivalent C++
>> explicit casts - if casting is really needed.
>>
>> You could take advantage of posting some example and of mentioning the
>> libraries you're using, if this answer didn't satisfy your question.
>
> Here is an example from Crypto++:
>
> CryptoPP::Weak::MD4 hash;
> byte digest[ 16 ];
>
> // Here is one example where I cast to make the std::string I have
> work
> hash.Update((const byte*)generated_string.c_str(),
> generated_string.length());

I see, in such a case you really need the cast, and a static_cast should 
be fine.

I you don't like to scatter the casts over all of your code, consider 
making a function that takes a std::string and outputs the casted const 
byte* that you need.

Still, regular C-style strings need no casting, and I'm sure you'll take 
care that the cast is correct for any typedef of "byte" that you'll have 
to use - wrt passing the size() of the std::string.

-- 
FSC
http://userscripts.org/scripts/show/59948
0
Francesco
6/28/2010 5:03:19 PM

"Francesco S. Carta" <entuland@gmail.com> wrote in message 
news:4c28d5d6$0$6839$5fc30a8@news.tiscali.it...
> Brad <byte8bits@gmail.com>, on 28/06/2010 09:47:53, wrote:
>
>> On Jun 28, 11:18 am, "Francesco S. Carta"<entul...@gmail.com>  wrote:
>>
>>> For converting std::string to C-style strings you use c_str(), for the
>>> reverse operation you build std::string from C-style strings - I have no
>>> idea of what you mean with "byte arrays" since C++ has no "byte" type.
>>
>> Yes, I use c_str(). Some libraries define byte (an unsigned char).
>>
>>> I don't see any need for casting, and about C style casts in particular,
>>> you can (you should) always replace them with the equivalent C++
>>> explicit casts - if casting is really needed.
>>>
>>> You could take advantage of posting some example and of mentioning the
>>> libraries you're using, if this answer didn't satisfy your question.
>>
>> Here is an example from Crypto++:
>>
>> CryptoPP::Weak::MD4 hash;
>> byte digest[ 16 ];
>>
>> // Here is one example where I cast to make the std::string I have
>> work
>> hash.Update((const byte*)generated_string.c_str(),
>> generated_string.length());
>
> I see, in such a case you really need the cast, and a static_cast should 
> be fine.
>

static_cast is not fine, reinterpret_cast is.

/Leigh 

0
Leigh
6/28/2010 5:14:18 PM
Leigh Johnston <leigh@i42.co.uk>, on 28/06/2010 18:14:18, wrote:

> "Francesco S. Carta" <entuland@gmail.com> wrote in message
> news:4c28d5d6$0$6839$5fc30a8@news.tiscali.it...
>> Brad <byte8bits@gmail.com>, on 28/06/2010 09:47:53, wrote:
>>> hash.Update((const byte*)generated_string.c_str(),
>>> generated_string.length());
>>
>> I see, in such a case you really need the cast, and a static_cast
>> should be fine.
>>
>
> static_cast is not fine, reinterpret_cast is.

Whoops...

-- 
FSC
http://userscripts.org/scripts/show/59948
0
entuland (631)
6/28/2010 5:24:08 PM
On Jun 28, 11:47=A0am, Brad <byte8b...@gmail.com> wrote:
> On Jun 28, 11:18=A0am, "Francesco S. Carta" <entul...@gmail.com> wrote:
>
> > For converting std::string to C-style strings you use c_str(), for the
> > reverse operation you build std::string from C-style strings - I have n=
o
> > idea of what you mean with "byte arrays" since C++ has no "byte" type.
>
> Yes, I use c_str(). Some libraries define byte (an unsigned char).
>
> > I don't see any need for casting, and about C style casts in particular=
,
> > you can (you should) always replace them with the equivalent C++
> > explicit casts - if casting is really needed.
>
> > You could take advantage of posting some example and of mentioning the
> > libraries you're using, if this answer didn't satisfy your question.
>
> Here is an example from Crypto++:
>
> CryptoPP::Weak::MD4 hash;
> byte digest[ 16 ];
>
> // Here is one example where I cast to make the std::string I have
> work
> hash.Update((const byte*)generated_string.c_str(),
> generated_string.length());
> hash.Final( digest );

How about:

template<>
struct std::char_traits<byte>
{
    // define the typedefs and static methods as appropriate
};

typedef std::basic_string<byte> ByteString;
0
Sousuke
6/28/2010 5:49:01 PM
On 28 juuni, 20:49, Sousuke <s0s...@gmail.com> wrote:
> On Jun 28, 11:47=A0am, Brad <byte8b...@gmail.com> wrote:
>
>
>
> > On Jun 28, 11:18=A0am, "Francesco S. Carta" <entul...@gmail.com> wrote:
>
> > > For converting std::string to C-style strings you use c_str(), for th=
e
> > > reverse operation you build std::string from C-style strings - I have=
 no
> > > idea of what you mean with "byte arrays" since C++ has no "byte" type=
..
>
> > Yes, I use c_str(). Some libraries define byte (an unsigned char).
>
> > > I don't see any need for casting, and about C style casts in particul=
ar,
> > > you can (you should) always replace them with the equivalent C++
> > > explicit casts - if casting is really needed.
>
> > > You could take advantage of posting some example and of mentioning th=
e
> > > libraries you're using, if this answer didn't satisfy your question.
>
> > Here is an example from Crypto++:
>
> > CryptoPP::Weak::MD4 hash;
> > byte digest[ 16 ];
>
> > // Here is one example where I cast to make the std::string I have
> > work
> > hash.Update((const byte*)generated_string.c_str(),
> > generated_string.length());
> > hash.Final( digest );
>
> How about:
>
> template<>
> struct std::char_traits<byte>
> {
> =A0 =A0 // define the typedefs and static methods as appropriate
>
> };
>
> typedef std::basic_string<byte> ByteString;

When some library/framework/platform interface needs something as
input then best idea is provide that to it with conversion functions.
It is not so good idea to use the types of library in your internal
application data and logic.

Few years pass and you realize that you need to switch to different
library. Then it is hard because some "myte" of "cbangbang" library
has spread all over your code and now you depend on it. Whereas with
conversion/translation layers you may even support usage of multiple
competing libraries in parallel when need arises.
0
ISO
6/28/2010 9:10:56 PM
On Jun 28, 12:47=A0pm, Brad <byte8b...@gmail.com> wrote:
> Here is an example from Crypto++:
>
> CryptoPP::Weak::MD4 hash;
> byte digest[ 16 ];
>
> // Here is one example where I cast to make the std::string I have
> work
> hash.Update((const byte*)generated_string.c_str(),
> generated_string.length());
> hash.Final( digest );

In addition to what others have said, you might want to
provide a global operator<< so that you could write

  hash << generated_string;

Or something similar.

--Jonathan
0
Jonathan
6/28/2010 9:52:25 PM
Reply: