remove escape characters from a char
I've been working with this for about 2 days now. I'm stuck, with a rather simple annoyance, but I'm not capable of solving it.
My programs basicly recieves a TCP connection from a PHP script. And the message which is send is stored in char buffer[1024];
.
Okay this buffer variable contains an unique key, which is being compared to a char key[1024] = "supersecretkey123";
The problem itself is that these two does not equal - no matter what I do. I've been printing the buffer and key variable out just above eachother and by the look they are 100% identical. However my equalisation test still fails.
if(key == buffer) { // do some thing here etc }
So then I started searching the internet for some information on what c开发者_运维问答ould be wrong. I later realized that it might be some escape characters annoying me. But I'm not capable of printing them, removing them or even making sure they are there. So that's why I'm stuck - out of ideas on how to make these equal when the buffer variable matches the key variable. Well the key does not chance, unless the declaration of the key is modified manually. The program itself is recieving the information and sending back information "correctly".
Thanks.
If you're using null terminated strings use proper api - strcmp
and its variants.
Additionally size in declaration char key[1024] = "supersecretkey123";
is not needed - either compiler will reduced it or stack/heap memory will be wasted.
If you are using C++ use std::string
instead of char []
. You cannot compare two char []
in way you try to do this (they are pointers to memory), but it's possible with std::string
.
If it's somehow mandatory to use char[]
in your case, use strcmp
.
Try with if(!strncmp(key,buffer,1024))
. See this reference on strncmp
.
精彩评论