Handling Strings

B

Brad

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
 
F

Francesco S. Carta

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.
 
B

Brad

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 );
 
F

Francesco S. Carta

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.
 
S

Sousuke

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 );

How about:

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

typedef std::basic_string<byte> ByteString;
 
Ö

Öö Tiib

Yes, I use c_str(). Some libraries define byte (an unsigned char).
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;

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.
 
J

Jonathan Lee

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top