有符号位的二进制怎么样转为十进制数?(实在对不起,只剩这么些个分了) ( 积分: 39 )

雪球

Unregistered / Unconfirmed
GUEST, unregistred user!
现在有一个无符号位的二进制转为十进制的程序,要怎么改动一下才能变成可以表示正负的?
Function binToDec(Value :string) : integer;
VAR
str : String;
Int : Integer;
i : integer;
begin
Str := UpperCase(Value);
Int := 0;
FOR i := 1 TO Length(str)do
Int := Int * 2+ ORD(str) - 48;
Result := Int;
end;
 
现在有一个无符号位的二进制转为十进制的程序,要怎么改动一下才能变成可以表示正负的?
Function binToDec(Value :string) : integer;
VAR
str : String;
Int : Integer;
i : integer;
begin
Str := UpperCase(Value);
Int := 0;
FOR i := 1 TO Length(str)do
Int := Int * 2+ ORD(str) - 48;
Result := Int;
end;
 
很简单,首位判断正负,其余减一位求值。
 
Function binToDec(Value :string) : integer;
VAR
str : String;
Int : Integer;
i : integer;
begin
Str := UpperCase(Value);
Int := 0;
FOR i := 2 TO Length(str)do
Int := Int * 2+ ORD(str) - 48;
if str[1] = '1' then
Result := 0 - Int
else
Result := Int;
end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
644
import
I
I
回复
0
查看
757
import
I
顶部