Board index » cppbuilder » Re: Signed unsigned conversion

Re: Signed unsigned conversion


2005-11-09 06:18:05 AM
cppbuilder53
Jeff wrote:
Quote
*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.

Have you tried declaring a signed variable (possibly of larger size) and
assigning to it a value returned by the HW? You need a larger-sized
variable because the maximum signed value that can be represented in a
16 bit value is 32767.
You probably need to apply the usnigned cast to the original value, so
it's assigned properly.
hth,
.a
 
 

Re:Re: Signed unsigned conversion

The value from the script is a signed double equal to 65535, instead of -1.
"Alex Bakaev [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
Jeff wrote:
>*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.
>
Have you tried declaring a signed variable (possibly of larger size) and
assigning to it a value returned by the HW? You need a larger-sized
variable because the maximum signed value that can be represented in a 16
bit value is 32767.

You probably need to apply the usnigned cast to the original value, so
it's assigned properly.

hth,
.a
 

Re:Re: Signed unsigned conversion

"Jeff" < XXXX@XXXXX.COM >wrote:
Quote
The value from the script is a signed double equal to 65535, instead of -1.
Ah, how stupid.
And you want it back as a signed value in the range -32768 to 32767?
That would probably require multiple casts:
#include <iostream>
int getval(double dblval, bool Signed)
{
int result;
if (Signed)
{
unsigned short usval = static_cast<unsigned short>(dblval);
short sval = static_cast<short>(usval);
result = static_cast<int>(sval);
}
else
{
result = static_cast<int>(dblval);
}
return result;
}
int main(int, char* [])
{
std::cout << getval(65535.0, true) << " " << getval(65535.0, false);
}
C:\>test
-1 65535
C:\>
On the Signed branch, the first cast narrows it, the second does the
unsigned/signed flip, and the third widens it back again. The third cast
can be done implicitly, of course.
Alan Bellingham
--
Team Thai Kingdom
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

{smallsort}

Re:Re: Signed unsigned conversion

Jeff wrote:
Quote
The value from the script is a signed double equal to 65535, instead of -1.

>>*The custom hardware that the script is talking to sends back unsigned
>>numbers which *are in two's complement representative unsigned form. I'm
Somewhat confusing, methinks. Especially mentioning a floating point bit
only now.
.a
 

Re:Re: Signed unsigned conversion

"Alex Bakaev [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
Somewhat confusing, methinks. Especially mentioning a floating point bit
only now.
Pretty much my conclusion, but I trust my example helps Jeff.
Why, oh why, the script decides that it wants to return a 16-bit value
in a _double_ is beyond me.
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
 

Re:Re: Signed unsigned conversion

Alan,
Thank you very much, that's exactly what I was looking for; it's working
now.
Thanks,
Jeff
"Alan Bellingham" < XXXX@XXXXX.COM >wrote in message
Quote
"Jeff" < XXXX@XXXXX.COM >wrote:

>The value from the script is a signed double equal to 65535, instead
>of -1.

Ah, how stupid.

And you want it back as a signed value in the range -32768 to 32767?

That would probably require multiple casts:

#include <iostream>

int getval(double dblval, bool Signed)
{
int result;

if (Signed)
{
unsigned short usval = static_cast<unsigned short>(dblval);
short sval = static_cast<short>(usval);
result = static_cast<int>(sval);
}
else
{
result = static_cast<int>(dblval);
}
return result;
}

int main(int, char* [])
{
std::cout << getval(65535.0, true) << " " << getval(65535.0, false);
}

C:\>test
-1 65535
C:\>

On the Signed branch, the first cast narrows it, the second does the
unsigned/signed flip, and the third widens it back again. The third cast
can be done implicitly, of course.

Alan Bellingham
--
Team Thai Kingdom
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

Re:Re: Signed unsigned conversion

Quote
char charTemp;
long longRtn;---------------this a long variable
longRtn = static_cast<unsigned long>(longValue);
case 'S': //Signed desired
longRtn = static_cast<signed long>(longValue);
unsigned long aa=static_cast<signed long>(longValue);
Tony yu
 

Re:Re: Signed unsigned conversion

"tony yu" < XXXX@XXXXX.COM >wrote:
Quote
unsigned long aa=static_cast<signed long>(longValue);
Yes?
What is this introduction of an otherwise unused variable for?
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK