const int to cost char

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

Recommended Posts

void Chandler::msgParse(char *charbuffer) { char CR = 13; char LF = 10; strcat(charbuffer, CR) } what im trying to do is add 13 as a char to the end of a message. so charbuffer in this case could be hello world, but it must be terminated with "hello world1310\0" the 13 and 10 are chars ascii values carriage return and line feed. how do i add chars such as this to the end of my message? the error from the code above is in the title thanks

Share on other sites
strcat(charbuffer,"\r\n");

Share on other sites
cheers mike but i must explicitly add 13 and 10 to the end of a sent message.

Share on other sites
strcat(charbuffer,"\x0d\x0a");

0d == 13
0a == 10

Share on other sites
That will only work if you can be certain that charbuffer contains sufficient space for two extra characters. Since you appear to be working in C++ I'd advise just using a std::string:
void Chandler::msgParse(std::string& message){	message += char(13);	message += char(10);}

Enigma

Share on other sites
Your original code does not work because strcat expects both parameters to be NULL-TERMINATED strings.

You could also write this I suppose:
void Chandler::msgParse(char *charbuffer)
{
char EOL[]={13, 10, 0};
strcat(charbuffer, EOL)
}
However, I think mike25025's first answer is really the best as it does the same thing, and everyone understands what it means. The thing you're sending this to is probably coded to look exactly for "\r\n" anyway, even though the spec may say 13,10 or whatever.

Also, be sure that the buffer 'charbuffer' that is passed in, has room for two extra characters.

Share on other sites
The emphasis here, though, is on 'string' rather than 'null-terminated' (although calling that awful thing that C provides a "string" offends my moral sensibilities).

The full error message is probably something along the lines of "cannot convert from const int to const char *"; I don't see what's so hard about copying and pasting that and making sure it is complete and correct. The * here is quite important; conversion between an integer and a single character is quite possible, because both are numeric types. However, conversion between either of those to a pointer type is a no-no.

If you'd prefer to do things 'directly', you could write this:
void Chandler::msgParse(char *charbuffer) {  const char CR = 13;  const char LF = 10;  char* end = charbuffer + strlen(charbuffer);  *end = CR; // overwrite the null terminator  *end+1 = LF;  *end+2 = '\0'; // restore the null terminator}

However, you must own the space "after" the "string" to which you are writing, no matter how you wish to do it. E.g.:

char stuff[13] = "Hello World!"; // 12 bytes plus null terminatorChandler c;c.msgParse(stuff); // "No room! No room!" There's plenty of room -// in Segmentation Fault Land!

You really, really, really should be using std::string; char * "strings" are a low-level abomination that should only be exposed where really needed (or where the effort of creating a std::string wrapper is completely useless).

void Chandler::msgParse(std::string& message) {  message += '\r'; // CR, i.e. ascii 13  message += '\n'; // LF, i.e. ascii 10  // It's so easy, happy go lucky!}

Note single quotes denote single characters; double quotes denote "string literals".

Now all you need to do is give the class and method decent names, and you're good to go ;)

(Yeah... I'd have to say "Chandler" is about the worst name for a class I have ever seen!

1) It says absolutely nothing about the class' purpose or what it represents, beyond that it "handles" - i.e. "does" something...
2) It emphasizes the perfectly obvious fact that it's a class, for no good reason.
3) As an unfortunate accident, it appears to refer to a character from Friends (I think that was the one... I never used to watch those sitcoms)...

)

Share on other sites
Quote:
 Original post by ZahlmanThe emphasis here, though, is on 'string' rather than 'null-terminated' (although calling that awful thing that C provides a "string" offends my moral sensibilities).You really, really, really should be using std::string; char * "strings" are a low-level abomination that should only be exposed where really needed (or where the effort of creating a std::string wrapper is completely useless).3) As an unfortunate accident, it appears to refer to a character from Friends (I think that was the one... I never used to watch those sitcoms)...)
Yeah I noticed that the class was named after a Friends character.

Perpare to be offended, but a null-terminated array of characters was called a string long before a std::string was. I don't see what else you'd call it.
A mere "character array" doesn't imply null-termination (which it might not depending on what you're doing with it), and that's a bit of a mouthful too.

Share on other sites
the help on this site is purly amazing...the quality of the resposes are excellent. thanks alot guys. ATM im trying to understand basic errors so that i can at least understand what i have to do to sort it out

thanks

Share on other sites
Quote:
 Original post by iMalcPerpare to be offended, but a null-terminated array of characters was called a string long before a std::string was. I don't see what else you'd call it.A mere "character array" doesn't imply null-termination (which it might not depending on what you're doing with it), and that's a bit of a mouthful too.

But the term 'string' applied to c-style "string"s is actually shorthand for 'zero-terminated string' (hence the Hungarian Wart strz). A "mere" character array can be just as much a 'string' as a zero-terminated one. And I'm running out of quotation characters, so I'll stop now [wink].

Enigma

Share on other sites
Quote:
Original post by Enigma
Quote:
 Original post by iMalcPerpare to be offended, but a null-terminated array of characters was called a string long before a std::string was. I don't see what else you'd call it.A mere "character array" doesn't imply null-termination (which it might not depending on what you're doing with it), and that's a bit of a mouthful too.

But the term 'string' applied to c-style "string"s is actually shorthand for 'zero-terminated string' (hence the Hungarian Wart strz). A "mere" character array can be just as much a 'string' as a zero-terminated one. And I'm running out of quotation characters, so I'll stop now [wink].

Enigma
Okay, point taken.

Share on other sites
I'm not offended, just my moral sensibilities are. ;) But yeah, the field of computer programming is no place to uphold tradition for tradition's sake.

Put bluntly: OMFG std::string ROXORS TEH BIG ONE111!

Share on other sites

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

Create an account

Register a new account

• Forum Statistics

• Total Topics
628726
• Total Posts
2984410

• 25
• 11
• 10
• 16
• 14