| |

VerySource

 Forgot password?
 Register
Search
View: 671|Reply: 2

Numerical conversion

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-22 23:30:01
| Show all posts |Read mode
A 16-bit binary number consisting of two bytes, with the most significant bit being the sign bit.
pVal [1] high byte pVal [0] low byte How do I convert this 16-bit binary number?
 
short CMainFrame :: Get2RealVal (BYTE * pVal)
{
short Val = 0;

if (pVal [1]&0x80 == 0)
{
Val = pVal [1] * 256 + pVal [0];
}
else
{
Val = pVal [1] * 256 + pVal [0];
Val = Val ^ 0xffff + 1;
}
Ranch
return Val;
}

Is there anything wrong with this?
Reply

Use magic Report

0

Threads

12

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-6 17:00:01
| Show all posts
Isn't forced conversion possible?

short Val = *(short*)pVal

ps. Provided that the current system is little endian
Reply

Use magic Report

0

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-7 09:45:01
| Show all posts
short val = 0;
val = pVal[0] + pVal[1] * 256;

Or the one upstairs is OK and fast
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list