Newbie hexadecimal question

J

Jim Lake

I'm creating a string of hex characters and I have an unsigned char
value of 00 assigned to the name IS.
I try m_strResp += IS to append the hex 00 to the hex characters
already in m_strResp and I get a C2593 compile error (error
C2593: 'operator +=' is ambiguous). I can see where adding 0 to
something is ambiguous but I'm not trying to do arithmetic. I'm
trying to create a string of hex characters.
Any suggestions?
Thanks!
 
A

ajk

I'm creating a string of hex characters and I have an unsigned char
value of 00 assigned to the name IS.
I try m_strResp += IS to append the hex 00 to the hex characters
already in m_strResp and I get a C2593 compile error (error
C2593: 'operator +=' is ambiguous). I can see where adding 0 to
something is ambiguous but I'm not trying to do arithmetic. I'm
trying to create a string of hex characters.
Any suggestions?
Thanks!

you could try using stringstream instead:

#include <sstream>
#include <iomanip>

....

stringstream ss;
int somevalue = 0;
ss << hex << somevalue;

cout << ss.str() << endl;

/ajk
 
A

Alf P. Steinbach

* Jim Lake:
I'm creating a string of hex characters and I have an unsigned char
value of 00

No, you don't.
assigned to the name IS.

No, you don't. That is at best a macro definition. Don't use
macros.

I try m_strResp += IS

Don't use mangled names, use meaningful names.

to append the hex 00 to the hex characters
already in m_strResp and I get a C2593 compile error (error
C2593: 'operator +=' is ambiguous).

The following code reproduces your problem:

#include <string>

int main()
{
std::string s;

s += 0;
}

Always post code that reproduces the problem. Copy and paste
the code. Don't type it in.

I can see where adding 0 to
something is ambiguous but I'm not trying to do arithmetic. I'm
trying to create a string of hex characters.
Any suggestions?

As already stated: don't use macros. A macro does not have a type.
And that's the direct cause of your problem.
 
J

Jim Lake

#include <string>

int main()
{
std::string s;

s += 0;
}

c:\documents and settings\administrator.the_domain\my documents\my
projects\test.cpp(6) : error C2593: 'operator +=' is ambiguous

c:\documents and settings\administrator.the_domain\my documents\my
projects\test.cpp(7) : warning C4508: 'main' : function should return
a value; 'void' return type assumed

How is this adding a hex 00 to a string (group) of hex characters?

I'm looking to add a hex 00 to this: FF FA

I want this-->FF FA to become this--> FF FA 00
 
A

Alf P. Steinbach

* Jim Lake:
[top-posting]

Don't top post. Read the FAQ. I have corrected the top-posting
here, but for best chance of useful answers: following usual posting
guidelines such as not top-posting, including relevant code, etc.


* Jim Lake:
#include <string>

int main()
{
std::string s;

s += 0;
}

c:\documents and settings\administrator.the_domain\my documents\my
projects\test.cpp(6) : error C2593: 'operator +=' is ambiguous

This is the error message you claimed, so the error seems to have
been successfully reproduced.

c:\documents and settings\administrator.the_domain\my documents\my
projects\test.cpp(7) : warning C4508: 'main' : function should return
a value; 'void' return type assumed

This is an incorrect warning, which means you're using an old compiler.

Try g++.

It's free.

How is this adding a hex 00 to a string (group) of hex characters?

It isn't, it's what you most probably did.

I'm looking to add a hex 00 to this: FF FA

I want this-->FF FA to become this--> FF FA 00

To add a string to the end of a std::string you can use the "+="
operator, like this:


std::string s = "FF FA";

s += " 00";
 
H

Howard

Alf P. Steinbach said:
This is the error message you claimed, so the error seems to have
been successfully reproduced.



This is an incorrect warning, which means you're using an old compiler.

Try g++.

It's free.



It isn't, it's what you most probably did.



To add a string to the end of a std::string you can use the "+="
operator, like this:


std::string s = "FF FA";

s += " 00";

This is assuming, of course, that Jim, meant that he had a string that
contains "FF FA" when he said that.

It *may* be that what he had was a string that contained two unsigned char
values where the first unsigned char had the hex value FF and the second had
the hex value FA. In that case, appending the hex value 00 (which is just
the single unsigned char with value zero) would be a different task from
appending the string " 00" (which is three unsigned char's: a space
character, followed by two "0" characters).

Jim, can you please clarify which of the above two meanings you had in mind
when you said you had a "string of hex characters"?

-Howard
 
O

Old Wolf

Howard said:
This is assuming, of course, that Jim, meant that he had a string that
contains "FF FA" when he said that.

It *may* be that what he had was a string that contained two
unsigned char values where the first unsigned char had the hex
value FF and the second had the hex value FA.

Unlikely - on most systems, char is 8-bit and signed,
so a std::string (or a char array) can't contain FF and FA.

Of course, it's hard to know when the guy can't be bothered
posting any code.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top