Hi,
I'm having some frustrating issues with char/strings.
I have a function which returns an unsigned short integer, the unsigned short integer is ALWAYS 1 char long. So if I try this code:

number[2] = function();

So, number[2] should give me the number I want, however, it gives me weird characters.
When I try this with a string it's the same result.
I also tried this:

cha[2] = (char)1;
MessageBox(hwnd, &cha[2], "Title", MB_OK);

Result: ÿÿ, or other jibberish.

Little help here please : - )
Thanks

>So, number[2] should give me the number I want
Nope, it should give you the character that the value returned by function represents. Characters are represented internally by integer values. For example, in ASCII you can expect the letter 'A' to be represented internally by the value 65.

If function returns an integer that's only one "character" long, that means it's a single digit value. Most character sets reserve such low values for control characters, which means you're trying to print a character that doesn't have a glyph.

What you want to do is convert the value returned by function to the equivalent character value of the digit:

number[2] = function() + '0';

By adding the integer value of '0' to the digit, you effectively shift the value of the digit into one of the representable values for the characters '0' - '9'.

...unsigned short integer is ALWAYS 1 char long

That's wrong statement. The "length" of unsigned short value is sizeof(unsigned short) . The length of char value is ALWAYS 1 by the language definition. Now try to print sizeof(unsigned char) and see what happens.

1. What's array called number type? Show me its declaration.
2. The second snippet is wrong (too ;) ):

cha[2] = (char)1;

Now cha is an array of two chars with element values: { 1, 0 }. But 1 (one) IS NOT a code of one as a printable char! It's '1' - char constant literal. The value of '1' is 49.

Probably, you had never seen char literals. It's strange. I think you can solve the 1st problem as soon as you understand a difference between 1 as integer and '1' as char values...

I'm sorry but I still don't quite get how I can put integers as text into a char and then print those :(

char string[2];
string[0] = function() + '0';
string[1] = '\0';

This seems to work, why? I've googled up a bit but can't figure it out.

I don't know where and what are you googling.
That's a result of my 5-second googling (1st reference in the reply list):
http://irc.essex.ac.uk/www.iota-six.co.uk/c/b1_the_char_data_type.asp
;)

Yes but what about this 0 I have to add after each line, I thought it was only the null character at the end you had to add.
I don't understand that. If you have something that outputs 1 (integer) and you add '0', doesn't that make 10?

I don't understand that. If you have something that outputs 1 (integer) and you add '0', doesn't that make 10?

Read what Narue already told you:

By adding the integer value of '0' to the digit, you effectively shift the value of the digit into one of the representable values for the characters '0' - '9'.[/code]

I'll try to further clarify this.
First open this ascii-table

Let's assume that your function is returning the unsigned short 1. You want this to be represented as the character '1' (note the quotes when I'm talking about chars). Now look at the asciitable and see what happens when you try [icode]foo[0] = char(1)[/icode].
The decimal value 1 represents the character "[i]start of heading[/i]", but that wasn't what you wanted right? You wanted the character '1' which has a decimal value of 49! So you need to add the number 48 to your output to "convert" it from int to char. Now we're almost there. Look up the character for the decimalvalue of 48 in the ascii-table. Suprise: it's the character '0'. So: if you have an number (int) between 0-9 and you want to "convert" it to it's character representation, just add '0' (character) (or int 48) to it!

The other thing mentioned is the '\0'. That's the end of string character which should always be added to an array of characters to end the string!

Oh now I get it, so if you would for some reason want your 1 to be a then you would go like this:

char string[2];
string[0] = 1 + '@';
string[1] = '\0';

I'm still having trouble trying to find how to put two char arrays or strings into one though, if I try this:

string str;
str = "this " + "is " + "a " + "string";

It gives me the following error:
Invalid operands of types const char[] and const char[] to binary 'operator +'

I need this because I'm trying to get a date, time and a number into a string, combined as a filename.

>Invalid operands of types const char[] and const char[] to binary 'operator +'
If you want string concatenation with the + operator, one of the two operands has to have a type of std::string. If it's a chain, then you can make sure that one of the first two operands is a std::string and all will be well because the result of the first concatenation will return a std::string which will then meet the requirement for all other concatenations in the chain:

string str;
str = string ( "this " ) + "is " + "a " + "string";

I understand, yet it seems so silly why it would be like that, atleast in my point of view. I hope you're not tired of helping me because I have two more questions:
1. Is it necessary to convert the integers I want to add to a string

string str;
str = string("this") + " is" + " number " + 1;

2. This code does not seem to work with the win32 equivalent of string LPSTR, any idea how I could do this because LPSTR is char*.

Or perhaps it's better to work with strings than LPSTRs, I have no idea. Are there any downsides to using LPSTR above strings? Please enlighten me.

Thanks everyone for all the help I've gotten so far by the way!

OK. let's start from the beginning. A type is a sum of two things: set of values and set of operators. Consider std::string type. It has two operator + (avoid possible const modifiers):

string + string   // concatenate two strings
string + (char*)  // append a contents referred by 
                  // right pointer operand 
                  // (upto zero byte occured).

No such beast as string::operator + with int type right operand!

Consider char* type operator +. It's the only (avoid all unsigned/short modifiers):

(char*) + int // add right operand value to 
              // the left operand pointer
              // (move pointer forward or backward)

Let's consider the code:

char* p;
p = ....     // p points to char array
p = p + 1;   // p points to the next element
p = p + '1'; // most interesting: '1' converted to int,
             // it's 49 - move p 49 positions forward
p = p + "1"; // error: no operator (char*) + (const char*)

There are (at least) two common ways to get a text (char) representation of int value in your program:

1. Use C library functions (it's a part of C++ library). No string type in C so you must use char arrays only.

char numtxt[32]; // large enough to place any int value
int num = 12345;

sprintf(numtxt,"%d",num); // Ooh, we have "12345" in numtxt
...
string s("Number is ");
s = s + numtxt; // string + (char*) operator used
// or s += numtxt;
// Now s contains "Number is 12345"

2. Use stringstream class (type) which allows stream output (with operator<<) to string type variables. It's the other story. Learn stringstream yourself...

2. Use stringstream class (type) which allows stream output (with operator<<) to string type variables. It's the other story. Learn stringstream yourself...

I'll go for the stringstream. It seems easier. Thanks.
Every time I get a bit confused I'll start reading this thread before asking any questions ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.