Newbie: string/byte concatenation

J

Jeremy

Sorry, I know this is probably a horrible question, but I have an array of
BYTEs: Address[5], and a std::string

Here is the definition for BYTE:
typedef unsigned char BYTE;

How do I append the bytes to the string? And does anyone have a suggestion
where I can read up on sting concatenation?

Thanks,
Jeremy
 
V

Victor Bazarov

Jeremy said:
Sorry, I know this is probably a horrible question, but I have an array of
BYTEs: Address[5], and a std::string

Here is the definition for BYTE:
typedef unsigned char BYTE;

How do I append the bytes to the string?

Your problem is in the fact that 'std::string' uses 'char' and your
BYTE type is 'unsigned char'. The two are different types although
there exists a conversion between them. I'd try using

std::copy(Address, Address + 5, std::back_inserter(yourstring));

Don't forget to include the necessary headers.
And does anyone have a suggestion
where I can read up on sting concatenation?

There are so many books on programming, I am unable to choose. If
you want to learn about C++ Standard Library, begin with simple C++
books ("Accelerated C++" is one of the best, or so I heard), then
move to "The C++ Standard Library" by Josuttis.

Victor
 
J

John Harrison

Jeremy said:
Sorry, I know this is probably a horrible question, but I have an array of
BYTEs: Address[5], and a std::string

Here is the definition for BYTE:
typedef unsigned char BYTE;

How do I append the bytes to the string?

Simple enough.

string s;
BYTE Address[5];
s.append(Address, Address + 5);

It's possible that will give you warning about conversion from signed to
unsigned, if so I'd just add some casts.
And does anyone have a suggestion
where I can read up on sting concatenation?

This is a good reference http://www.dinkumware.com/refxcpp.html for the
standard library.

john
 
J

Jeremy

How do I append the bytes to the string?
Your problem is in the fact that 'std::string' uses 'char' and your
BYTE type is 'unsigned char'. The two are different types although
there exists a conversion between them. I'd try using

std::copy(Address, Address + 5, std::back_inserter(yourstring));


Sorry, I should have clarified one thing: I want the hex(or decimal) values,
not the actual character conversion.

So if the bytes = 0x00, 0x0C, 0x76, 0x09, 0x01, 0x2B
The string should = "000C7609012B" (or the decimal equivalent if that's
easier)

It has to be this way because the string needs to be compatible with
standard URLs.

Thanks again,
Jeremy
 
J

John Harrison

Jeremy said:
Sorry, I should have clarified one thing: I want the hex(or decimal) values,
not the actual character conversion.

So if the bytes = 0x00, 0x0C, 0x76, 0x09, 0x01, 0x2B
The string should = "000C7609012B" (or the decimal equivalent if that's
easier)

Just a crucial little detail.

I would use sprintf to turn the bytes into C strings. And then append the C
string to the C++ string

for (int i = 0; i < 5; ++i)
{
char temp[3];
sprintf(temp, "%02X", Address);
s += temp;
}

but there are other ways as well.

john
 
J

Jeremy

So if the bytes = 0x00, 0x0C, 0x76, 0x09, 0x01, 0x2B
The string should = "000C7609012B" (or the decimal equivalent if that's
easier)

Just a crucial little detail.
:)


I would use sprintf to turn the bytes into C strings. And then append the C
string to the C++ string

for (int i = 0; i < 5; ++i)
{
char temp[3];
sprintf(temp, "%02X", Address);
s += temp;
}


That works great, thanks. I assume it is temp[3] and not [2] because
sprintf appends NUL to the end?

Thank you,
Jeremy
 
S

Siemel Naran

string s;
BYTE Address[5];
s.append(Address, Address + 5);

It's possible that will give you warning about conversion from signed to
unsigned, if so I'd just add some casts.

What cast are you thinking of? I can think of casting Address from BYTE* to
char*, or a special iterator whose operator* casts unsigned char to char, or
just making a std::basic_string<unsigned char>. There might be others too.
What do you have in mind?
 
S

Siemel Naran

for (int i = 0; i < 5; ++i)
{
char temp[3];
sprintf(temp, "%02X", Address);
s += temp;
}

but there are other ways as well.


One can also use stringstreams. (Code not tested :).

ostringstream temp;
temp << hex;
for (int i = 0; i < 5; ++i)
{
temp.clear();
temp.str("");
temp << Address;
s += temp.str();
}
 
J

John Harrison

string s;
BYTE Address[5];
s.append(Address, Address + 5);

It's possible that will give you warning about conversion from signed to
unsigned, if so I'd just add some casts.

What cast are you thinking of? I can think of casting Address from
BYTE* to
char*, or a special iterator whose operator* casts unsigned char to
char, or
just making a std::basic_string<unsigned char>. There might be others
too.
What do you have in mind?

I was thinking the OP has a compiler which doesn't support template member
functions, in which case append would be defined as append(const char*,
const char*) so the cast would be necessary.

john
 
T

tom_usenet

So if the bytes = 0x00, 0x0C, 0x76, 0x09, 0x01, 0x2B
The string should = "000C7609012B" (or the decimal equivalent if that's
easier)

Just a crucial little detail.
:)


I would use sprintf to turn the bytes into C strings. And then append the C
string to the C++ string

for (int i = 0; i < 5; ++i)
{
char temp[3];
sprintf(temp, "%02X", Address);
s += temp;
}


That works great, thanks. I assume it is temp[3] and not [2] because
sprintf appends NUL to the end?


Right.

Tom
 
O

Old Wolf

John Harrison said:
Jeremy said:
So if the bytes = 0x00, 0x0C, 0x76, 0x09, 0x01, 0x2B
The string should = "000C7609012B" (or the decimal equivalent if that's
easier)
I would use sprintf to turn the bytes into C strings. And then append the C
string to the C++ string

for (int i = 0; i < 5; ++i)
{
char temp[3];
sprintf(temp, "%02X", Address);
s += temp;
}

but there are other ways as well.


This is perhaps not the best advice, as it could be a buffer overflow
if the type of Address were anything other than 'unsigned char'. I
would prefer to either make temp[] larger, or use the stringstream method.
 
S

Siemel Naran

Old Wolf said:
sprintf(temp, "%02X", Address);

This is perhaps not the best advice, as it could be a buffer overflow
if the type of Address were anything other than 'unsigned char'. I
would prefer to either make temp[] larger, or use the stringstream method.


But we know Address has type BYTE, which is an unsigned char.

Of course, if in the future we change to larger sized bytes, the
stringstream method is better.
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top