Board index » cppbuilder » Signed unsigned conversion

Signed unsigned conversion


2005-11-08 04:30:29 AM
cppbuilder66
I've got a long number that I'm trying to selectively convert to signed or
unsigned values. The problem I'm having is that casting doesn't seem to
work. I've got my code listed below, any help would be appreciated.
char charTemp;
long longRtn;
long ConvertNum( String strType, long longValue)
{
strcpy(&charTemp, strType.SubString(0, 1).c_str()); //Get the first
character
switch (UpCase(charTemp))
{
case 'U': //Unsigned desired
longRtn = static_cast<unsigned long>(longValue);
break;
case 'S': //Signed desired
longRtn = static_cast<signed long>(longValue);
break;
default:
longRtn = -1;
break;
}//end switch
return longRtn;
}
 
 

Re:Signed unsigned conversion

On Mon, 7 Nov 2005 13:30:29 -0700, Jeff wrote:
Quote
long ConvertNum( String strType, long longValue)
{
strcpy(&charTemp, strType.SubString(0, 1).c_str()); //Get the first
character
switch (UpCase(charTemp))
{
case 'U': //Unsigned desired
longRtn = static_cast<unsigned long>(longValue);
break;

case 'S': //Signed desired
longRtn = static_cast<signed long>(longValue);
break;

default:
longRtn = -1;
break;
}//end switch
return longRtn;
}
There are a few problems with your code, one of which is that you're
/always/ returning the default implementation (signed or unsigned,
which differs for different compilers) for long. That is, even though
longRtn is /always/ signed if your implementation defaults to signed,
or unsigned if your implementation defaults to unsigned. With BCB the
default is signed, so you'll always get the signed value.
something more like this might be better:
template <typename T, typename U>T aconvert(U value)
{
T ret;
ret = static_cast<T>( value);
return ret;
}
but even with your code it's unnecessary to do the UpperCase call,
switch(strType[1])
{
case 'u' : case 'U' : // whatever
break;
case 'l' : case 'L': // whatever
break;
default: break;
}
--
liz
 

Re:Signed unsigned conversion

Your function returns a signed long. No matter what the cast is the result
is placed into that value (the value of the variable 'longRtn').
Note that the bit pattern for a long and an unsigned long are identical.
The code you show is confusing as it seems to have no function. What are
you really trying to achieve with it? Show something in the source code
which calls the function, something which is sensitive to if it is a signed
vs an unsigned long.
. Ed
Quote
Jeff wrote in message
news: XXXX@XXXXX.COM ...

I've got a long number that I'm trying to selectively convert to signed or
unsigned values. The problem I'm having is that casting doesn't seem to
work. I've got my code listed below, any help would be appreciated.

char charTemp;
long longRtn;

long ConvertNum( String strType, long longValue)
{
strcpy(&charTemp, strType.SubString(0, 1).c_str()); //Get the first
character
switch (UpCase(charTemp))
{
case 'U': //Unsigned desired
longRtn = static_cast<unsigned long>(longValue);
break;

case 'S': //Signed desired
longRtn = static_cast<signed long>(longValue);
break;

default:
longRtn = -1;
break;
}//end switch
return longRtn;
}
 

{smallsort}

Re:Signed unsigned conversion

I'm trying to have a function which will selectively return a signed or
unsigned representation of the number.
That is, I'd like to pass in 'S' and 65535 and have it return -1. Or pass
in U and -1 and have it return 65535.
Thanks,
Jeff
"Ed Mulroy" < XXXX@XXXXX.COM >wrote in message
Quote
Your function returns a signed long. No matter what the cast is the
result is placed into that value (the value of the variable 'longRtn').

Note that the bit pattern for a long and an unsigned long are identical.

The code you show is confusing as it seems to have no function. What are
you really trying to achieve with it? Show something in the source code
which calls the function, something which is sensitive to if it is a
signed vs an unsigned long.

. Ed

>Jeff wrote in message
>news: XXXX@XXXXX.COM ...
>
>I've got a long number that I'm trying to selectively convert to signed
>or unsigned values. The problem I'm having is that casting doesn't seem
>to work. I've got my code listed below, any help would be appreciated.
>
>char charTemp;
>long longRtn;
>
>long ConvertNum( String strType, long longValue)
>{
>strcpy(&charTemp, strType.SubString(0, 1).c_str()); //Get the
>first character
>switch (UpCase(charTemp))
>{
>case 'U': //Unsigned desired
>longRtn = static_cast<unsigned long>(longValue);
>break;
>
>case 'S': //Signed desired
>longRtn = static_cast<signed long>(longValue);
>break;
>
>default:
>longRtn = -1;
>break;
>}//end switch
>return longRtn;
>}


 

Re:Signed unsigned conversion

"Jeff" < XXXX@XXXXX.COM >writes:
[Just some remarks that I haven't seen in this thread.]
Quote
char charTemp;
long longRtn;

long ConvertNum( String strType, long longValue)
{
strcpy(&charTemp, strType.SubString(0, 1).c_str()); //Get the first
character
Actually, the comment doesn't match what is going on on this line. In
fact, two char objects are written, including the terminating 0. Since
charTemp can only hold one, the program has undefined behavior.
What's wrong with the obvious
Quote
switch (UpCase(charTemp))
switch (UpCase(strType[0]))
?
Quote
{
case 'U': //Unsigned desired
longRtn = static_cast<unsigned long>(longValue);
break;

case 'S': //Signed desired
longRtn = static_cast<signed long>(longValue);
break;

default:
longRtn = -1;
Using -1 to signal failure looks dangerous, since -1 is also a
perfectly valid return value for some input.
Why not
- using assert() to abort the program if strType[0] has an unexpected
value, or
- throw an exception in that case, or
- change strType's type to bool or an appropriate enumeration type
 

Re:Signed unsigned conversion

On Mon, 7 Nov 2005 14:41:55 -0700, Jeff wrote:
Quote
I'm trying to have a function which will selectively return a signed or
unsigned representation of the number.
Then just cast it as desired
--
liz
 

Re:Signed unsigned conversion

"Jeff" < XXXX@XXXXX.COM >wrote in message
Quote
I'm trying to have a function which will selectively return a signed
or unsigned representation of the number. That is, I'd like to pass
in 'S' and 65535 and have it return -1. Or pass in U and -1 and
have it return 65535.
What you are trying to implement programmably, the compiler already does for
you implicitally. So why do you need a function for it?
Also, in your example, 65535 is the same value for both 'signed long' and
'unsigned long'. Remember that a 'long' is a 32-bit value, and 65535 is a
valid 32-bit value for both signed and unsigned types. The kind of behavior
that you describe would have to be applied to a 'short' instead of a 'long',
ie:
signed short s_value = 65535; // s_value becomes -1
unsigned short u_value = -1; // u_value becomes 65535
signed short s_value2 = -1;
unsigned short u_value2 = static_cast<unsigned short>(s_value2); //
u_value2 becomes 65535
s_value2 = static_cast<signed short>(u_value2); // s_value becomes -1
Gambit
 

Re:Signed unsigned conversion

Casting is what I've done in the code I pasted, this doesn't work.
"liz" < XXXX@XXXXX.COM >wrote in message
Quote
On Mon, 7 Nov 2005 14:41:55 -0700, Jeff wrote:

>I'm trying to have a function which will selectively return a signed or
>unsigned representation of the number.

Then just cast it as desired
--
liz
 

Re:Signed unsigned conversion

On Mon, 7 Nov 2005 16:19:34 -0700, Jeff wrote:
Quote
Casting is what I've done in the code I pasted, this doesn't work.
Of course it doesn't you're always returning a signed integer. What
are you really trying to do? The bit representation will be the same
whether it's signed or unsigned.
Why do you need signed (or unsigned)?
--
liz
 

Re:Signed unsigned conversion

"Jeff" < XXXX@XXXXX.COM >wrote in message
Quote
Casting is what I've done in the code I pasted, this doesn't work.
Yes, it does. You just are not using it properly. Ed and I have explained
to you why - your function is always returning the same type of value
regardless of any casting, and your casting is using the wrong sized data
type for what you are trying to accomplish anyway.
Gambit
 

Re:Signed unsigned conversion

liz wrote:
Quote
There are a few problems with your code, one of which is that you're
/always/ returning the default implementation (signed or unsigned,
which differs for different compilers) for long.
Erm, unqualified long IS the signed type, this is not implementation
defined (7.1.5.2)
I think you might be confusing it with char, where char is a distinct
type from both signed and unsigned char, and it is
implementation-defined whether it is a signed or unsigned type.
[Similarly for wchar_t, although it is even less clear what type is
'shadows']
--
AlisdairM(TeamB)
 

Re:Signed unsigned conversion

On 8 Nov 2005 03:34:36 -0700, Alisdair Meredith [TeamB] wrote:
Quote
Erm, unqualified long IS the signed type, this is not implementation
defined (7.1.5.2)

I think you might be confusing it with char, where char is a distinct
You're right, Thanks
--
liz
 

Re:Signed unsigned conversion

The problem is I have to return a signed variable, but I need to convert a
signed negative number into it's signed positive equivalent. For example,
with a -1 pass in, it should return 65535 and vice versa; both of which are
passed back in a signed variable.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Jeff" < XXXX@XXXXX.COM >wrote in message
news:436fca0a$ XXXX@XXXXX.COM ...

>I'm trying to have a function which will selectively return a signed
>or unsigned representation of the number. That is, I'd like to pass
>in 'S' and 65535 and have it return -1. Or pass in U and -1 and
>have it return 65535.

What you are trying to implement programmably, the compiler already does
for you implicitally. So why do you need a function for it?

Also, in your example, 65535 is the same value for both 'signed long' and
'unsigned long'. Remember that a 'long' is a 32-bit value, and 65535 is a
valid 32-bit value for both signed and unsigned types. The kind of
behavior that you describe would have to be applied to a 'short' instead
of a 'long', ie:

signed short s_value = 65535; // s_value becomes -1
unsigned short u_value = -1; // u_value becomes 65535

signed short s_value2 = -1;
unsigned short u_value2 = static_cast<unsigned short>(s_value2); //
u_value2 becomes 65535
s_value2 = static_cast<signed short>(u_value2); // s_value becomes -1


Gambit


 

Re:Signed unsigned conversion

"Jeff" < XXXX@XXXXX.COM >wrote in message
Quote
The problem is I have to return a signed variable
Why?
Quote
but I need to convert a signed negative number into it's signed positive
equivalent.
As I told you earlier, 65535 in your earlier example is the same value for
both 'signed' and 'unsigned' long types. The only way to convert 65535
to -1 is if the function works with 16-bit values instead of 32-bit values.
In which case, the C++ language already handles thee conversion for you, so
you don't need a function.
Quote
For example, with a -1 pass in, it should return 65535 and vice versa;
both of which are passed back in a signed variable.
What EXACTLY are you trying to accomplish overall? Why do you need these
conversions in the first place? You've been asked several timees now to
provide more details and a real example, but you have not provided them yet.
Gambit
 

Re:Signed unsigned conversion

*See comments below:
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Jeff" < XXXX@XXXXX.COM >wrote in message
news:4370e0d3$ XXXX@XXXXX.COM ...

>The problem is I have to return a signed variable

Why?
*This is because I'm using this fuction to create a converted value for a
program which *contains a scripting engine. The script expects a signed
value, and it's code that I *cannot change. This scripting program is used
to talk to custom hardware.
Quote
>but I need to convert a signed negative number into it's signed positive
>equivalent.

As I told you earlier, 65535 in your earlier example is the same value for
both 'signed' and 'unsigned' long types. The only way to convert 65535
to -1 is if the function works with 16-bit values instead of 32-bit
values. In which case, the C++ language already handles thee conversion
for you, so you don't need a function.
*The custom hardware that the script is talking to sends back unsigned
numbers which *are in two's complement representative unsigned form. I'm
trying to find a way to *translate these two's complement representative
unsigned numbers into their signed *representative equivalents so that I can
use them in straight forward math functions.
Quote
>For example, with a -1 pass in, it should return 65535 and vice versa;
>both of which are passed back in a signed variable.

What EXACTLY are you trying to accomplish overall? Why do you need these
conversions in the first place? You've been asked several timees now to
provide more details and a real example, but you have not provided them
yet.
*The reason why I can't provide all the details is because this is for an
internal product *which contains intellectual property, and I can't divulge
every part of the code.