フィルターのクリア

How to convert char string to bit string

18 ビュー (過去 30 日間)
bikekowal Marcin Kowalski
bikekowal Marcin Kowalski 2016 年 10 月 11 日
コメント済み: Walter Roberson 2016 年 10 月 11 日
Hi,
I am trying to solve a problem which might look like quite easy to solve, hopefully. I have a structure with 5 cells. Each cell contains a string of characters. I would like to concatenate all the strings into one string. I have used strjoin function but the result is following: "1100100 1110011 1100100 1100001 1100100". There are gaps between these small strings. Next step I need to do is to convert the entire string into bit string. Any ideas what I do wrong?
best, Marcin

回答 (3 件)

Massimo Zanetti
Massimo Zanetti 2016 年 10 月 11 日
str='100100 1110011 1100100 1100001 1100100'
str=regexprep(str,' ','')
result = str-'0'

Walter Roberson
Walter Roberson 2016 年 10 月 11 日
編集済み: Walter Roberson 2016 年 10 月 11 日
onestring = strjoin(TheCellArrayOfStrings, '');
bitstring = reshape(dec2bin(onestring, 8).', 1, []);
You might want to subtract '0' from the result, if what you want is binary rather than a string of text.
  2 件のコメント
Jos (10584)
Jos (10584) 2016 年 10 月 11 日
what is wrong with simple concatenation
onestring = cat(1,TheCellArrayOfStrings{:})
Walter Roberson
Walter Roberson 2016 年 10 月 11 日
編集済み: Walter Roberson 2016 年 10 月 11 日
Each cell contains a string of characters
To that point we had not been given that the characters are restricted to '0' and '1'.
People asking here about converting strings of characters to bitstrings are typically working with steganography (or watermarking) and typically have arbitrary text to convert into bit format.

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


bikekowal Marcin Kowalski
bikekowal Marcin Kowalski 2016 年 10 月 11 日
編集済み: per isakson 2016 年 10 月 11 日
Thank you guys for all the answers. This is what I wanted:
onestring = strjoin(TheCellArrayOfStrings, '');
I see now that I didn't express my mind correctly. The initial string contains characters ('0' and '1') that represent bit values. Is it possible to convert the char string without actual converting it? It means to convert it to bit string without changing its content but type of data.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 10 月 11 日
If you have a character vector S (not a cell array, just a single string), then you can convert the type of data but not its value by using uint16(S) . For example,
>> uint16('101')
ans =
1×3 uint16 row vector
49 48 49
You need to use uint16() for this purpose rather than uint8(), because characters in MATLAB are 16 bits, so converting to 8 bits would be a change in content.
You might have expected the integers 1 0 1 as the output, but the characters '0' and '1' are not represented by the integers 0 and 1: they are represented by the integers 48 and 49. See https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/ASCII-Table-wide.svg/2000px-ASCII-Table-wide.svg.png . Converting '0' and '1' to 0 and 1 requires a change in content.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by