How can i change a binary message of type double (1000101000111001000100) into type char ('1000101000111001000100') so in between ' '?
1 次查看(过去 30 天)
显示 更早的评论
See title. Simply doing num2str does not work.
4 个评论
Guillaume
2018-1-9
To be even clearer:
You cannot store 1000101000111001000100 as a double number. It will be rounded to the nearest integer that can be represented as double which is 1000101000111001042944.
The magnitude of the number is just too large to be stored as double (or even as uint64).
See Jan's answer for one way to store that number. You could also simply store it the way a computer is designed to do it as:
a = 2264644;
John D'Errico
2018-1-9
You could store your numbers in symbolic form. That would be inefficient of course. Best, if you wish to store strings of binary digits is to just use a numeric vector, so maybe doubles, or uint8, or logicals, or a character vector. Any of these methods will not be limited in the number of bits you can store.
回答(2 个)
Jan
2018-1-9
编辑:Jan
2018-1-9
I guess, that you mean:
d = [1,0,0,0,1,0,1,0,0,0,1,1,1,0,0,1,0,0,0,1,0,0]
and want to convert it to a char:
s = sprintf('%d', d)
Alternatively:
Pool = '01';
s = Pool(d + 1)
1 个评论
John D'Errico
2018-1-9
+1. The classic alternative to sprintf is to use char.
char(d + '0')
ans =
'1000101000111001000100'
which for me is somehow easier to remember how to do, whereas I always need to read the help for sprintf to use it.
Birdman
2018-1-9
a=1000101000111001000100;
aChar=char(floor(vpa(num2str(a),numel(num2str(a)))))
9 个评论
Jan
2018-1-9
编辑:Jan
2018-1-9
It is essential to understand, that:
a = 1000101000111001000100
does not store 21 digits in a double, because according to IEEE754 doubles contain only about 16 significant digits due to the limited precision. Even converting the data by vpa afterwards cannot avoid this limitation, because the last digits are cropped during the assignment "a = ..." already. num2str() does not reproduce the lost digits also, because this would be pure magic.
This discussion might concern exactly the problem of the OP. Therefore it might help to solve the OP's problem because (not although) it was not correct. So please do not delete this answer, but maybe insert an explanation inside the answer, why it does not work as expected. It is very useful to let others learn from mistakes.
Thanks, Birdman, your smileys help to clarify the tone. Fortunately we are discussing about computer science only, not about serious topics. :-)
I like Stephen's attention. I post too many wrong suggestions, and it is useful for the forum, that he cross-checks them.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!