string & bytes

V

vertigo

Hello
I have:
struct packet{
byte type; /*my byte is unsigned char*/
int len;
}
struct packet *p = new struct packet;
p->type=10;
p->len=0;

std::string str="";
for (int i=0; i<5; i++)
str=*((byte*)p+i);

i want to create std::string object which has bytes from p.
(because that string object is later used for Sending() that packet).
My problem is that: when before sending i displayed str some character
values were less than 0 (thru to char instead of unsigned char).
How can i prevent it ?
Second problem is strange. When i displayes str i received:
10, -25, 115, 0, 0.
Why there are -25 and 115 insted of 0, 0 ?

Thanx
Michal
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello
I have:
struct packet{
byte type; /*my byte is unsigned char*/
int len;
}
struct packet *p = new struct packet;
p->type=10;
p->len=0;

std::string str="";

OK, this is the kicker.

Either you've got a C program that won't compile, or your question is
not about the C language. I think your problem is the latter, and you
/really/ need to talk to the people in comp.lang.c++ instead of us.

[snip]


- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFBSE/CagVFX4UWr64RAm8PAKCEvEjSjU3uxrb1aoo/2d/ecwoYLACeIhyW
4iCdsd3Ig9BBrL8WJZrUdd0=
=N3VT
-----END PGP SIGNATURE-----
 
S

SM Ryan

# struct packet{
# byte type; /*my byte is unsigned char*/
# int len;
# }

# Second problem is strange. When i displayes str i received:
# 10, -25, 115, 0, 0.
# Why there are -25 and 115 insted of 0, 0 ?

There's no reason to assume the bytes of the struct fields are packed tightly
with no padding bytes between.
 
J

John Bode

vertigo said:
Hello
I have:
struct packet{
byte type; /*my byte is unsigned char*/
int len;
}
struct packet *p = new struct packet;
p->type=10;
p->len=0;

std::string str="";
for (int i=0; i<5; i++)
str=*((byte*)p+i);

i want to create std::string object which has bytes from p.
(because that string object is later used for Sending() that packet).
My problem is that: when before sending i displayed str some character
values were less than 0 (thru to char instead of unsigned char).
How can i prevent it ?
Second problem is strange. When i displayes str i received:
10, -25, 115, 0, 0.
Why there are -25 and 115 insted of 0, 0 ?

Thanx
Michal



You have several pad bytes between the type and len members. Most
architectures have a restriction that multi-byte objects must be
aligned on even addresses, so structs may have some pad bytes between
members.

Read and convert each member individually, instead of just blindly
copying bytes from the struct.
 
Z

Zhou Wei

John Bode said:
vertigo <[email protected]> wrote in message
Hello
I have:
struct packet{
byte type; /*my byte is unsigned char*/
int len;
}
struct packet *p = new struct packet;
p->type=10;
p->len=0;

std::string str="";
for (int i=0; i<5; i++)
str=*((byte*)p+i);

i want to create std::string object which has bytes from p.
(because that string object is later used for Sending() that packet).
My problem is that: when before sending i displayed str some character
values were less than 0 (thru to char instead of unsigned char).
How can i prevent it ?
Second problem is strange. When i displayes str i received:
10, -25, 115, 0, 0.
Why there are -25 and 115 insted of 0, 0 ?

Thanx
Michal



You have several pad bytes between the type and len members. Most
architectures have a restriction that multi-byte objects must be
aligned on even addresses, so structs may have some pad bytes between
members.

Read and convert each member individually, instead of just blindly
copying bytes from the struct.

But how can i solve this problem?
Now my solution is to append a preprocess word "__attribute__ ((packed))"
(under Linux) after the struct, just like below:
struct packet{
byte type; /*my byte is unsigned char*/
int len;
}__attribute__ ((packed));
 
R

Richard Bos

Zhou Wei said:
But how can i solve this problem?

By reading and converting the members individually. As John said two
lines earlier.
Now my solution is to append a preprocess word "__attribute__ ((packed))"

That _may_ work, but it's not ISO C, and not portable. It's also
possible that it leads to less efficient code.

Richard
 
J

John Bode

Zhou Wei said:
John Bode said:
vertigo <[email protected]> wrote in message
Hello
I have:
struct packet{
byte type; /*my byte is unsigned char*/
int len;
}
struct packet *p = new struct packet;
p->type=10;
p->len=0;

std::string str="";
for (int i=0; i<5; i++)
str=*((byte*)p+i);

i want to create std::string object which has bytes from p.
(because that string object is later used for Sending() that packet).
My problem is that: when before sending i displayed str some character
values were less than 0 (thru to char instead of unsigned char).
How can i prevent it ?
Second problem is strange. When i displayes str i received:
10, -25, 115, 0, 0.
Why there are -25 and 115 insted of 0, 0 ?

Thanx
Michal



You have several pad bytes between the type and len members. Most
architectures have a restriction that multi-byte objects must be
aligned on even addresses, so structs may have some pad bytes between
members.

Read and convert each member individually, instead of just blindly
copying bytes from the struct.

But how can i solve this problem?


By reading and converting each member individually, instead of just
blindly copying bytes from the struct.

str[0] = static_cast<char>(p->type);
char *p = reinterpret_cast<char *> (&p->len);
for (size_t i = 0; i < sizeof p->len; i++)
str[i+1] = *p++;

I won't guarantee that this is the right way to do it, but it
illustrates what I was talking about.
 
C

CBFalconer

John said:
.... snip ...

str[0] = static_cast<char>(p->type);
char *p = reinterpret_cast<char *> (&p->len);
for (size_t i = 0; i < sizeof p->len; i++)
str[i+1] = *p++;

I won't guarantee that this is the right way to do it, but it
illustrates what I was talking about.

It isn't the right way. It heaves up multiple errors on any C
compiler known to me. Results might be different with a C++
compiler, but those are off-topic here. :)
 
J

John Bode

CBFalconer said:
John said:
... snip ...

str[0] = static_cast<char>(p->type);
char *p = reinterpret_cast<char *> (&p->len);
for (size_t i = 0; i < sizeof p->len; i++)
str[i+1] = *p++;

I won't guarantee that this is the right way to do it, but it
illustrates what I was talking about.

It isn't the right way. It heaves up multiple errors on any C
compiler known to me. Results might be different with a C++
compiler, but those are off-topic here. :)

Well, since the OP was trying to write to a std::string, I was
assuming C++ and wrote the code accordingly, but the actual question
(what's this garbage in the middle of my struct) is applicable to both
groups.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top