How to convert a binary string to binary number?

31 vues (au cours des 30 derniers jours)
Kiran Bhageerath Reddy Vuyurru
Commenté : Walter Roberson le 2 Fév 2021
Hello
I wanted to convert a string of binary number to just a binary number(not array)
For example:
if this is a input binary_string = '00110110101'
expected_output = 00110110101

Réponse acceptée

Walter Roberson
Walter Roberson le 31 Jan 2021
binary_string = '00110110101'
binary_string = '00110110101'
expected_output = uint16(bin2dec(binary_string));
expected_output
expected_output = uint16 437
This is the base 10 display of a binary number whose bit pattern is 000000110110101
MATLAB has no built-in facility to create binary numbers that automatically display in binary format.
Is it possible that what you want is the base 10 number that MATLAB would output as 110110101, decimal one hundred and ten million, one hundred and ten thousand, one hundred and one ? Note that MATLAB will never automatically display leading zeros on a decimal number (after all, there are an infinite number of leading zeros.)
  2 commentaires
Kiran Bhageerath Reddy Vuyurru
Thanks for the solution.
The Problem I am working on is to convert a real number into its single precision floating point number. When I am using CustomFloat to convert the number the output is a char vector on which I am unable to do shift operations.
I am expecting the answer to be in binary(base-2) for the question you have asked.
Walter Roberson
Walter Roberson le 2 Fév 2021
When you have a vector of characters, then bit shifting corresponds to circshift() if you are using rotation, or to throwing away columns at the end if you are shifting right, or to adding columns of '0' at the right if you are shifting left.
I do not know what a "real number" is in this context. If you have a double precision number you need to convert to single precision, then use single()
format long g
p = pi
p =
3.14159265358979
ps = single(p)
ps = single
3.141593

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by