1

Very basic question. I am having problems in converting a char to a char* that points to the actual character. Googling yielded information on strcpy, string, sprintf, etc. After many, many attempts at trying to understand the problem, I still can't get it to work.

I have a character array recv_msg which contains the ASCII values of the characters I need. I need to store the characters (not the values) in a vector msg.

What I have:

std::vector<char> msg;
char recv_msg[max_buffer_length];
...
int i;
for (i=0;i<bytes_transferred;i++) {
    msg.push_back(recv_msg[i]);
}
// Problem: when I process my msg I see 49 instead of '1'.

If I do the following as a sanity check, I do get the behavior I need. That is, I see a '5' and not a 53 (ASCII code for 5).

std::vector<char*> msg;
...
int i;
char *val;
for (i=0;i<bytes_transferred;i++) {
    msg.push_back(val);
}
// No problem: when I process my msg I see '5' instead of 53.

So I need to turn the ASCII code values (char) into their character representation (char *). How on earth do I do this?

I want to solve the problem at this stage, and not have to do the ASCII -> character conversion when processing the message.

4
  • 1
    Use std::string or would that be too easy? Commented Jul 31, 2013 at 19:44
  • Of course you are going to see a 53, a char is just a 1 byte number. Can you clarify how are you "processing" your data? Printing? Commented Jul 31, 2013 at 19:48
  • @MrLister The std::string seemed a bit wasteful, so I wanted to understand what was going on. Commented Jul 31, 2013 at 20:06
  • @LarryPel It was a bit of a convoluted explanation since this is part of an NPAPI plugin... Somehow user P0W recognized my problem (from my poor explanation), and recommended something that worked. Commented Jul 31, 2013 at 20:08

3 Answers 3

2

A char is just a number, so if you're seeing it as 53 instead of '5' it's because that's how you're displaying it.

You could change to store the incoming messages as strings, e.g.:

std::vector<std::string> msg;
char recv_msg[max_buffer_length];
...
int i;
for (i=0;i<bytes_transferred;i++) {
    msg.push_back(std::string(&recv_msg[i], 1));
}

But this seems inefficient if all you really have is single characters. An alternative would be to convert to a string when you "do something" with the character, e.g.:

std::string str(&msg.back(), 1);

Or even:

char str[2];
str2[0] = msg.back();
str2[1] = '\0';
Sign up to request clarification or add additional context in comments.

1 Comment

Yup, I tried the string approach at some point but it seemed wasteful as you point out. I understand I could change the way I display the number. My problem was how to store the characters inside the vector in a similar way than the string literal "5" gets stored. P0W's method of msg.push_back(recv_msg[i] - '0') achieved this. Thanks for the detailed explanation though!
1

Based on your

"I need to store the characters (not the values) in a vector msg"

Try following, btw ASCII 53 is '5' only, may be your recv_msg stores ascii value only.

std::vector<char> msg;
char recv_msg[max_buffer_length];
...
int i;
for (i=0;i<bytes_transferred;i++) {
    msg.push_back(recv_msg[i] - '0'); //Subtract 48 from numerals, 
}

1 Comment

Thank you! This is exactly what I needed. I now understand what my problem was.
1

The difference between char and char * has absolutely nothing to do with the representation of a character, and its numeric representation.

With ASCII:

printf("%c\n", '5');

gives you 5, and:

printf("%d\n", '5');

gives you 53. It's always going to be stored as 53, turning it into a printable 5 character is just a question of how you elect to output it - you can't "store the characters (not the values)", because the value always is what you store. Even when you write '5' in your source code, your compiler just turns it into 53, i.e.:

if ( '5' == 53 ) {
    puts("This will always print.");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.