フィルターのクリア

conversion from binary to decimal

1 回表示 (過去 30 日間)
Eli
Eli 2020 年 1 月 2 日
コメント済み: Walter Roberson 2020 年 1 月 2 日
disp('conversion from binary to decimal');
z=input('give a floating point number ','s');
n=strfind(z,'.'); %index of the .
z1=z(1:n-1); %z1 vector
disp(z1);
z2=z(n+1:length(z)); %z2 second part after .
x=bin2dec(z1); %bin 2 dec transformation of the first part of the number (before .)
%transformation of the decimal part of the number
b=0.5;
y=0;
for i=1:length(z2)
y=y+(z2(i)-48)*b; %what is -48, it is set for precsion?
b=0.5*b;
end
fprintf('Il numero in base 10 e'':\n');
-48 is set for a better precision in the conversion from binary to decimal or there is another reason?
Thank you!!

採用された回答

David Hill
David Hill 2020 年 1 月 2 日
No, the -48 is converting the ascii character '0' or '1' to a number.
  2 件のコメント
John D'Errico
John D'Errico 2020 年 1 月 2 日
And that happens because '0' happens to be ascii 48.
+('0')
ans =
48
Walter Roberson
Walter Roberson 2020 年 1 月 2 日
In all character sets that I have been able to find since the 1930's, IBM's EBCDIC is the only one that did not encode the characters '0' through '9' as consecutive positions in the table with '0' being the low value (EBCDIC coded '1' as the low value and '0' followed '9' if my memory is correct)
Therefore for all computer languages that you are ever likely to encounter, you can rely on the fact that the character code for a particular single digit "Arabic numeral" is that many positions after the position for the character code for '0'.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by