Character to Binary function?

I need to create a function that will: accept a character and returns a vector of 8 logical values (bits) that are the binary equivalent of the ASCII value of the character, while using functions dec2bin, sprintf, and logical. While converting the 0's and 1's to ASCII codes, we aren't supposed to use a if statement, we're supposed to follow these rules:
If you have the character '0' (which in Matlab is the same as the number 48), how would you convert that 48 into a 0? In other words, how do you get from 48 to 0? How do you get from 49 to 1? Or for that matter, how would you get from 50 to 2? The relationship is the same in each case.
I'm not sure where to get started on this function, any help would be great, thanks in advance!!
From what I understand, we need to accept a character, then convert it to ASCII (not sure how to do this), then output a vector of 8 bits (0s and 1s). So far, I know a 0 is 48 in ASCII, but when I use dec2bin, i get:
110100
111000
So i'm not really sure where to go from there.

回答 (2 件)

Image Analyst
Image Analyst 2014 年 11 月 24 日
編集済み: Image Analyst 2014 年 11 月 24 日

0 投票

Subtraction seem like the obvious operation that they were hinting strongly at. Did you try subtraction:
ch = '0' % or anything from '0' - '9'
decimalValue = ch - '0'
dec2bin(ch) % Binary of the ascii value.
dec2bin(decimalValue) % Binary of the decimal value
I trust you can take it from there.

8 件のコメント

Andrew Ardolino
Andrew Ardolino 2014 年 11 月 24 日
the inputted characters need to also be able to be letters. So I'm not sure how to convert the letters to ascii values. Also he said something about using sprintf "use the format string '%08s' to force a string to be 8 characters long, and padded with leading 0s" So I also am not sure how to utilize that, even if i had the ascii values?
Image Analyst
Image Analyst 2014 年 11 月 24 日
The letter is the ascii value. Just run this to prove it.
userInput = input('Enter a numerical digit : ', 's')
decimalValue = userInput - '0'
dec2bin(userInput) % Binary of the ascii value.
dec2bin(decimalValue) % Binary of the decimal value
Andrew Ardolino
Andrew Ardolino 2014 年 11 月 24 日
input=double('a')
does this also work for converting it to ascii? after doing that, how do you force it to output the string of 8 bits?
Andrew Ardolino
Andrew Ardolino 2014 年 11 月 24 日
No, that wouldn't work because it needs to be logical rather than double. So far what I have is:
function char2bin
charinput = input('Enter a character: ', 's')
decval= charnput - '0'
char=dec2bin(decval)
sprintf('%08s',char)
Which doesn't work
Image Analyst
Image Analyst 2014 年 11 月 24 日
編集済み: Image Analyst 2014 年 11 月 24 日
What do you mean by logical? Logical is just true or false. You can't have a variety of ascii values represented by true or false. That's only two possible values, not 10. Do you mean a logical array where the dec2bin string is converted to a logical array? Anyway, DON'T USE CHAR as the name of your variable since it's the name of a built in function.
Image Analyst
Image Analyst 2014 年 11 月 24 日
I'm going to assume what I said is true - that you want a logical array. So the function below handles both cases of ASCII and decimal, since I'm not clear which one you want. Please adapt if necessary.
function [decimalLogicalArray, asciiLogicalArray] = char2bin()
clc;
userInput = input('Enter a numerical digit : ', 's')
% userInput is the ASCII value of the digit. So for 2, the value is 50.
decimalValue = userInput - '0'
% Get the binary value of the ascii value of the digit in a string (a character array).
% For example, for 2, strAscii will be 110010 (which = 50 in decimal).
strAscii = dec2bin(userInput) % Binary of the ascii value.
% Get the binary value of the decimal value of the digit in a string (a character array).
% For example, for 2, strDecimal will be 10 (which = 2 in decimal).
strDecimal = dec2bin(decimalValue) % Binary of the decimal value
% Convert the binary string into a logical array.
% So for example, 2 = 50 = 110110 will be a logical array [1,1,0,1,1,0]
asciiLogicalArray = logical(strAscii-'0')
% So for example, 2 = 10 will be a logical array [1,0]
decimalLogicalArray = logical(strDecimal-'0')
Andrew Ardolino
Andrew Ardolino 2014 年 11 月 24 日
but I need to be able to use characters as well as digits, and i need to force the vector to be 8 digits long
Image Analyst
Image Analyst 2014 年 11 月 25 日
This works bot both numbers and characters in a string. For example '123abc'.
function asciiLogicalArray = char2bin()
clc;
userInput = input('Enter a numerical digit : ', 's')
% userInput is the ASCII value of the digit. So for 2, the value is 50.
decimalValue = userInput
% Get the binary value of the ascii value of the
% characters in a string (a character array).
% For example, for 2, strAscii will be 110010 (which = 50 in decimal).
strAscii = dec2bin(userInput) % Binary of the ascii value.
% Convert row-wise to a row vector
strAscii = reshape(strAscii', [1, numel(strAscii)])
% Convert the binary string into a logical array.
% So for example, 2 = 50 = 110110 will be a logical array [1,1,0,1,1,0]
asciiLogicalArray = logical(strAscii - 48)

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

Thorsten
Thorsten 2014 年 11 月 24 日

0 投票

c = input('Enter a character > ', 's');
binvecc = logical(dec2bin(c, 8) - '0');
sprintf('%x', binvecc)

4 件のコメント

Andrew Ardolino
Andrew Ardolino 2014 年 11 月 24 日
This works correctly for most things, but we were provided with 3 "tests" to make sure our function works correctly, which were 'a', '0', and 'Z'. They worked except for the Z, do you know how I would be able to fix that? Thanks!
Andrew Ardolino
Andrew Ardolino 2014 年 11 月 24 日
oops, it also doesn't work with the '0'
Andrew Ardolino
Andrew Ardolino 2014 年 11 月 24 日
nevermind, I think this works. is ascii involved with this? can you explain the -'0' to me?
Thorsten
Thorsten 2014 年 11 月 26 日
編集済み: Thorsten 2014 年 11 月 26 日
c is represented as an ASCII value.
dec2bin returns a array of characters, e.g. '0110001'. To convert this into numbers you have to subtract '0', which works on every character in the array. Just as [ 1 2 3] - 4 works in Matlab.

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2014 年 11 月 24 日

編集済み:

2014 年 11 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by