Jump to content
  • Advertisement
Sign in to follow this  
Bru

char + char = love!

This topic is 3440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey there. i am programming in c++ and i've come to a point that i want to send a packet from my server to the client. but not just a packet, it has to have an idea aswell as some data since i want the client to know what data i sent(like coordinates or any other variables). so for this i guess i have to join two char arrays(strings?) to be able to connect an id and the information(like 01555 where 01 is the id of the packet,and 555 is the data). but well, c++ doesnt love char + char. and so i went on my long journy to google to find the lost html page with the tutorial on how to join two chars! but well that didnt go well. so i just thought about going here and ask, how can i join two char arrays :)?

Share this post


Link to post
Share on other sites
Advertisement
It doesn't look like you want to join two chars. Rather, you want to join two char arrays. To do this in C++ you need to know the lengths of both char arrays. Say that your char arrays are ca1 and ca2 respectively. Let the length of ca1 be la1 and the length of ca2 be la2. Then you can simply use a memcpy.



char* ca = new char[la1+la2];
memcpy( ca, ca1, la1 );
memcpy( &ca[la1], ca2, la2 );

// do whatever you need to send your packet here

delete [] ca;




Note that here you probably wouldn't want to dynamically allocate the array ca all the time. It would be better if, at compile time, you knew the size of the packet, or at least had an upper bound on the size of the packet. Then you could either allocate ca statically, or you could allocate it dynamically once at the beginning of the program and then just use it repeatedly every time you needed to send a packet. Anyway, those are all details. The real answer is that to join two char arrays, you'll likely want to use memcpy.

Cheers,
Rob

Share this post


Link to post
Share on other sites
You can use strcat to concat two strings. I'll let you look it up.

You might want to write a class that deals with all this sorta stuff for you, similar to Raknet's Bitstream, or, well, the iostream stuff [smile]

Share this post


Link to post
Share on other sites
strcat seems nice and simple, but what's memcpy? didnt fully understand what it does.

Share this post


Link to post
Share on other sites
@Richy2k : He might not want to use strcat. The two separate char*'s he wants to concatenate might not have null terminators. In fact, from the example he provided, that doesn't seem to be the case. Try the following code:


#include <cstdio>
#include <cstring>
#include <iostream>

using std::cout;
using std::endl;


int main(){

char* ca1 = new char[5];
char* ca2 = new char[8];

ca1[0] = 01;
ca2[1] = 5;
ca2[2] = 5;
ca2[3] = 5;

char* ca = new char[5+8];

strcat( ca, ca1 );
strcat( ca, ca2 );

puts( ca );

cout << strlen( ca ) << endl;
return 0;
}





That likely won't be what you want or what Bru expects.

@Bru The strcat function concatenates two null terminated strings. The memcpy function is more generic. Basically it looks like this:


memcpy( void* dest, void* src, size_t len );





It's really rather straight forward. It copies "len" bytes from "src" into "dest". The problem with strcat, as I mentioned to Richy2k, is that it expects the char arrays your concatenating to be null terminated. Since you'll be sending things over the network and you might want to use chars as bytes rather than parts of a string, this may not be true for you. Take a look at the example program I posted if you want to see what I mean. Let me know if you have any other questions about memcpy.

--Rob

Share this post


Link to post
Share on other sites
There are multiple ways listed above, but one you should seriously consider is using std::stringstream. The STL is really very powerful.

Here's a quick snippet to show you how easy it is to combine two char arrays into one stringstream.


#include <iostream>
#include <sstream>

using namespace std;

int main()
{
// Create a couple of buffers.
char* buffer1 = new char[11];
buffer1 = "firstBuffer";

char* buffer2 = new char[12];
buffer2 = "secondBuffer";


// Create a stringstream and feed in our char buffers using the input operator (<<)
stringstream stream;
stream << buffer1 << buffer2;


// Print out the results using the .str() function of stringstream.
cout << stream.str() << '\n';

return 0;
}



Hope you figure out a way that works!

Share this post


Link to post
Share on other sites
Original post by ArcPrime

// Create a couple of buffers.
char* buffer1 = new char[11];
buffer1 = "firstBuffer";

char* buffer2 = new char[12];
buffer2 = "secondBuffer";


[/quote]

What's that?

You know you are leaking memory and for no reason at all?

Why not just write:

std::string buffer1 = "firstBuffer";
std::string buffer2 = "secondBuffer";


Or for this particular case:

const char *buffer1 = "firstBuffer";
const char *buffer2 = "secondBuffer";

Share this post


Link to post
Share on other sites
Quote:

What's that?

You know you are leaking memory and for no reason at all?

Why not just write:


std::string buffer1 = "firstBuffer";
std::string buffer2 = "secondBuffer";



Or for this particular case:


const char *buffer1 = "firstBuffer";
const char *buffer2 = "secondBuffer";


Yes, you are correct. I wasn't so concerned about the tiny memory leak; the intention was simply to display the use of stringstream.

No offense intended, and thanks for giving everyone else the heads-up. :)

FYI, I personally prefer the first suggestion you made (using std::string).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!