I would like to extract each character from a text file. Later convert each character(in ASCII) to binary and store the binary form in an array.
2 ビュー (過去 30 日間)
古いコメントを表示
For example, text file: example.txt which contains text "hello, hi". It will take each character and convert it to binary and store the value in an array.
0 件のコメント
回答 (1 件)
Guillaume
2016 年 5 月 4 日
Note that computers store characters and numbers in binary form, so it's not clear what you mean by "convert it to binary".
In any case, to read the file:
content = fileread('C:\full\path to\example.txt');
If you want matlab to display the ASCII value (strictly speaking it's not ASCII as characters can be outside the range 0-127):
asciivalues = double(content);
If what you meant is that you want the individual bits of each character as separate elements of your array:
charbits = cell2mat(arrayfun(@(c) bitget(c, 8:-1:1), asciivalues, 'UniformOutput', false));
2 件のコメント
Guillaume
2016 年 5 月 4 日
Simply transpose the cell array returned by arrayfun before the call to cell2mat. That is:
charbits = cell2mat(arrayfun(@(c) bitget(c, 8:-1:1), asciivalues, 'UniformOutput', false)'); %note the '
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!