Conversion of types

I’m using this conversion

String Mode = “12”;

int mode = (int)Mode;
or
int mode = int(Mode);

either syntax is correct since the Language Reference says:

Syntax
int(x)
(int)x (C-style type conversion)

Parameters
x: a value. Allowed data types: any type.

Why does this not work?

Do you really want to know why, or just to fix it?

int mode = Mode.toInt();

I already fixed it with "toInt" from Smart Willies post.

Yes:
But why any way??

String is a complex object type NOT a simple C++ variable.

Steve

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!

PaulRB,

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?

pamam

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()'.

Steve

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.

Thanks y'all for your great help.
pamam

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.

Type casting may do a conversion. Consider casting an int to a float.

wildbill:
Type casting may do a conversion. Consider casting an int to a float.

Hmmm... true!

  float j = 12.34;

  int k = j;

  SerialUSB.println(k);

prints "12". I would call that an implicit type conversion, rather than a type-cast.

  float j = 12.34;

  int k = (int) j;

  SerialUSB.println(k);

also prints "12". So what appears to be a type-cast operation is doing an (explicit) conversion.

  char* j = "12.34";

  int k = j;

  SerialUSB.println(k);

prints "293", at least on my Pro Micro with my exact sketch and IDE 1.8.10. That's a type-cast of the string pointer, and not a conversion.

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.

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 was bothered by it too so I did a little reading this morning. I think it's only applicable to scalar types.

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