How to define a constant string in c++ using HEX values.

Hi I have the following rotten code.

      std::string delim;
      delim += 0xE1;
      delim += 0x00;
      delim += 0x3E;

What's a better way to do this only using one line?
thx!
sean-keysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi sean-keys,

allthough it's generally possible to use hex values within string literals it's not possible to write

> std::string delim = "\xE1\x00\x3E"

since the '\x00' always terminates the string literal.

The only possibility I found to create a string which is equal to that which results from your code is this:

> std::string delim = "\xE1" + std::string( 1, '\0' ) + "\x3E";
or
> std::string delim = (char)0xE1 + std::string( 1, '\0' ) + (char)0x3E;

Hope that helps,

ZOPPO

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sean-keysAuthor Commented:
thx
0
AxterCommented:
FYI:
In C/C++ you can concatenate string literals without using a string object.
So the following ....
std::string delim = "\xE1" + std::string( 1, '\0' ) + "\x3E";
std::string delim = (char)0xE1 + std::string( 1, '\0' ) + (char)0x3E;

Can be replaced with this:
std::string delim = "\xE1" "\0" "\x3E";

Concatenating with string literals is more efficient than using intermediary std::string object, because it's done at compile time, instead of at run time.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

ZoppoCommented:
Hi Axter,

I'm not sure if this is correct - at least I tested this too but the result was different (used compiler is from Microsoft VS 2010).

I agree that the declared string literal you wrote is correct, but assigning it to a std::string doesn't assign it completeley.

Please test this code:

	std::string strTest1;
	strTest1 += (char)0xE1;
	strTest1 += (char)0x00;
	strTest1 += (char)0x3E;
	std::string strTest2 ="\xE1" + std::string( 1, '\0' ) + "\x3E";
	std::string strTest3 = "\xE1" "\0" "\x3E";

	std::cout << "'" << strTest1 << "' Len:" << strTest1.length() << std::endl;
	std::cout << "'" << strTest2 << "' Len:" << strTest2.length() << std::endl;
	std::cout << "'" << strTest3 << "' Len:" << strTest3.length() << std::endl;

Open in new window


Here strTest1 is filled using the original method from the question, strTest2 is filled using my proposed method and strTest3 uses what you suggested.

Here's the result from my test app:

'ß >' Len:3
'ß >' Len:3
'ß' Len:1

Open in new window


ZOPPO
0
AxterCommented:
Looks like your right Zoppo.  When passing string literal with a zero, the string constructor is stopping and the first 0 terminator it finds.

Here's a more appropriate example, which will force the construct to read pass the zero terminator:
std::string strTest4("\xE1" "\0" "\x3E", 3);

This method doesn't require intermediary object, and string is concatenated at compile time instead of run time.

std::string strTest4("\xE1" "\0" "\x3E", 3);
	std::string strTest5 = std::string("\xE1" "\0" "\x3E", 3);
	std::cout << "'" << strTest4 << "' Len:" << strTest4.length() << std::endl;
	std::cout << "'" << strTest5 << "' Len:" << strTest5.length() << std::endl;

Open in new window

0
AxterCommented:
I think a method which is more maintainable and still efficient, would be to declare the object on a constant C style string.
Example
      const char MyData[] = "\xE1" "\0" "\x3E";
      std::string strTest6(MyData, sizeof(MyData) - 1);
      std::cout << "'" << strTest6 << "' Len:" << strTest6.length() << std::endl;

Now when ever you add not character to the string literal, you don't have to modify the length being pass to the C++ string.
0
ZoppoCommented:
That's for sure a better maintainable and more elegant solution. Nice ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.