フィルターのクリア

Binary to character function?

34 ビュー (過去 30 日間)
Andrew Ardolino
Andrew Ardolino 2014 年 11 月 24 日
コメント済み: Usha Padma 2021 年 6 月 14 日
So far I have this function:
function char2bin
char=input('Enter a character: ', 's');
char2=(dec2bin(char))
sprintf('%08s',char2)
which takes in a character, and converts it into a vector of 8 0's and 1's, forced to be 8 with the '%08s' thingy.
Next, I need to make a function which will:
1. Convert the vector of bits to a vector ASCII numbers (0 becomes 48, 1 becomes 49).
2. Convert the vector of ASCII numbers to a character string (use the char function).
3. Use the bin2dec function to convert the string into a single ASCII number.
4. Convert the ASCII number into a character.
and I have no idea where to start with that, any help would be great, thank you in advance!
  3 件のコメント
Andrew Ardolino
Andrew Ardolino 2014 年 11 月 24 日
not sure, do you think that'd work?
Erik Dekelbaum
Erik Dekelbaum 2014 年 11 月 24 日
It might. Try making a new function where the output is the input vector of bits + 1. Then check the new output variable in the workspace. If the values are all incremented by 1, you know it worked.

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

回答 (2 件)

Guillaume
Guillaume 2014 年 11 月 24 日
  1. simply use double(somestring) to get the ASCII values of the character
  2. to reverse that operation: char(asciivalues)
  3. doesn't make sense to me
  4. see 2.
  7 件のコメント
Walter Roberson
Walter Roberson 2021 年 6 月 12 日
out1 = char2bin('Hello, Usha!')
out1 = 1×96
0 1 0 0 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 1 1 0 0 0 1 1 0 1 1
out2 = bin2char(out1)
out2 = 'Hello, Usha!'
function binvec = char2bin(charinput)
binvec = reshape((dec2bin(charinput,8) - '0').',1,[]);
end
function origchar = bin2char(binvec)
origchar = char(reshape(bin2dec(reshape(char(binvec + '0'),8,[]).'),1,[]));
end
Usha Padma
Usha Padma 2021 年 6 月 14 日
Thank you. It helped me a lot

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


Image Analyst
Image Analyst 2014 年 11 月 25 日
See my latest answer in your original question: http://www.mathworks.com/matlabcentral/answers/163995#comment_251754 I think it does what you want. For example you can send in '123abc' and it will return
strAscii =
011000101100100110011110000111000101100011
asciiLogicalArray =
Columns 1 through 29
0 1 1 0 0 0 1 0 1 1 0 0 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 1
Columns 30 through 42
1 0 0 0 1 0 1 1 0 0 0 1 1
  2 件のコメント
Andrew Ardolino
Andrew Ardolino 2014 年 11 月 25 日
my char2bin function works correctly, but for the next function, he wants us to do almost the exact opposite thing, my current issue is in the above answer. Ugh thank you though, the other one does almost exactly what I need it to do, I just need to add an sprintf that incorporates '%08s' to force it to be 8 chars long. Do you have any idea for the other function though? Thanks so much for your help
Star Strider
Star Strider 2014 年 11 月 25 日
You can force it to be 8 characters long by using a second argument with your dec2bin call.
From the documentation (for R2014b):
str = dec2bin(d,n) produces a binary representation with at least n bits.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by