Convert int to short? 
Author Message
 Convert int to short?

Hi there!
Is it possible to convert an int to an short int? In that case how?

/Tobbe



Mon, 29 Apr 2002 03:00:00 GMT  
 Convert int to short?

Quote:

> Hi there!
> Is it possible to convert an int to an short int? In that case how?

Try something like this:

 int i = 5;
 short s;

 s = i;

Quote:
> /Tobbe

Gr.,

Ruben.

--
http://www.arm-uit-de.com



Mon, 29 Apr 2002 03:00:00 GMT  
 Convert int to short?
This will cause a warning if you don't specify the cast directly. You should
use an explicit cast:

int i = 5;
short s;
s = (short)i;

Torsten



Mon, 29 Apr 2002 03:00:00 GMT  
 Convert int to short?

Quote:

> This will cause a warning if you don't specify the cast directly. You should
> use an explicit cast:

> int i = 5;
> short s;
> s = (short)i;

It works fine without the type cast.

Gr.,

Ruben.

Quote:

> Torsten

--
http://www.arm-uit-de.com


Mon, 29 Apr 2002 03:00:00 GMT  
 Convert int to short?
Indeed, I'm shocked.

In warning level 3 the following conversions do not cause warnings!

int i=5;
short s=i;

long l=5L;
int i=l;

long l=5L;
short s=l;            // long to short !

double d=5.0;
int s=d;               // double to int !

Now I wonder about the following:

unsigned int a=5U;
int b=6;
bool c=(a<b);    // without any warning
if (a<b) {...}        // signed/unsigned mismatch warning

Very obscure!

Torsten



Mon, 29 Apr 2002 03:00:00 GMT  
 Convert int to short?

Quote:
> Is it possible to convert an int to an short int? In that case how?

Have you tried a cast?

int x;
short y;

y = (short) x;    // in C

y = static_cast<short>(x);    // in C++

Regards,
Will



Mon, 29 Apr 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. convert char to short int via pointer?...

2. Converting to short int

3. different between short int and int

4. cast long int to short int

5. short int vs int wrt to execution time

6. Int,short int and portability of code

7. converting unsigned int to signed int

8. Convert (int) to (unsigned int)

9. short vs int

10. short to int conversion

11. DWORD, int, short, and long

12. Finding the respective upper and lower bytes in memory of a short unsigned int

 

 
Powered by phpBB® Forum Software