フィルターのクリア

Converting decimal to binary, help!!

2 ビュー (過去 30 日間)
SB
SB 2012 年 11 月 2 日
I'm trying to write a function that converts a string containing a number from decimal to binary (this number may include decimal points or a negative sign). I know to find, say the binary form of 25, I would simply find the remainder of 25/2, 12/2, 6/2, 3/2 and 1/2, and take those numbers and reverse them. I'm not sure how to implement this in code though. I also know that I need to split the string at the decimal point and identify its sign, which i've done below:
% function[bin]=myreal2bin(decstring)
if decstring(1)=='-'
decstring = decstring(2:end);
F = -1;
else
F = 1;
end
Ind = strfind(decstring, '.');
L = length(decstring);
if isempty(Ind)
Ind = L+1;
end
Num1 = decstring(1:Ind-1);
LN1 = length(Num1);
Num2 = decstring(Ind+1:end);
LN2 = length(Num1);
end
  2 件のコメント
SB
SB 2012 年 11 月 2 日
Nah, I'm frustrated because I can't understand how to write the code for this problem and I've spent hours and hours trying to figure it out. I'll change my name back soon.
Image Analyst
Image Analyst 2012 年 11 月 2 日
In the past, has changing your name ever worked to help you to understand how to write code?

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

回答 (1 件)

John Petersen
John Petersen 2012 年 11 月 2 日
  5 件のコメント
John Petersen
John Petersen 2012 年 11 月 2 日
編集済み: John Petersen 2012 年 11 月 2 日
What are you doing with
str2num(Num1) = q;
You cannot assign a number to a function call. I think you need to assign a temp variable to it like
floatnum = str2num(Num1);
and then use this whereever you have str2num(Num1) Try that for starters.
John Petersen
John Petersen 2012 年 11 月 2 日
Also, the last line
bin=bin1+bin2
will fail because these are strings. To concatenate strings, simply do
bin = [bin1, bin2];

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by