pamam:
I already fixed it with "toInt" from Smart Willies post.
Who?
pamam:
But why any way??
Because:
pamam:
I'm using this conversion
String Mode = "12";
int mode = (int)Mode;
or
int mode = int(Mode);
They are not conversions. Nothing is changed. What is happening is simply telling the compiler to re-interpret some bit-pattern in another way. This is called type-casting.
"Mode" is not a basic/fundamental C data type. It's an object. An object belonging to the class "String". If you're not familiar with object-oriented concepts, I won't attempt to explain all that here. But, really, all "Mode" is is a reference to an object stored in memory. In other words, an address in memory. If you try to use type-casting on it, all you get is that memory address, which could be anything!
What you are saying is that the string of "1234" which is stored in ascii encoded bits is not changed to the binary of 1010101000 when it is cast to an int?
No what we're saying is that you cannot "cast" an object like a String into an int or any other basic variable type. "Cast", i.e. automatic type conversion, is a term that only applies to basic C++ variable types and NOT to complex objects. To convert a String to an int you need to use a specialised object method, in this case 'toInt()'.
Welcome to C/C++. You used a very powerful construct - a cast, without understanding what it does. C (it dates back there) delights in handing you a loaded pistol and letting you have at it, assume that you know what you are doing. Does your foot hurt?
It's not very productive (or helpful) to tell you the above without a better explanation but that's a lot of typing that's been done many times already elsewhere. Look at pointers and casts. Try your example with a C string. It will be just as bad. Bring that code back if you are having trouble seeing why.
pamam:
What you are saying is that the string of "1234" which is stored in ascii encoded bits is not changed to...
Yes, that's exactly what I'm saying:
PaulRB:
Nothing is changed.
Type-casting never changes the bit-pattern, never performs a conversion. It just takes the existing bit-pattern and pretends it is something else. This is different to other languages.
What's more: often, in C, the variable is not the value, the bit-pattern itself, but only a pointer/address to that value or pattern in memory. So when you type-cast the variable, you are really type-casting the address or pointer. This is true for strings in C. The variable is just the pointer to where the actual string is stored in memory.
PaulRB:
So there must be some C rule I haven't grasped after all these years that says when a type-cast happens and when a conversion happens.
You”re getting the address of j, since j is a pointer type.
I think the rule is you can cast between any fundamental type.
An array is not a fundamental type, but a pointer to it is.
I did a little more reading too on this subject and concur with the original gotcha I made. I found that most authors inter use the term conversion and cast without knowing it. In some books the word Cast is not even in the index and Conversion of types is. Oh well it was enlightening hearing from all of you; but I think we have flogged the dead horse enough don't you and I think it ain't going to get up and run anymore. Also one of the worst thing that occur in written specifications is that they are not specific enough.
Thanks,
pamam