ASCII coding to get corresponding character

I wrote this code so far, and I am getting the corresponding characters, except I get them all from 32 through 126, no matter what number I type in. Essentially I want to be able to type in a number between 32:126 and get only that numbers character. I want it to be if the code is less than 32 or more than 126 (aka out of that range) the user gets a "?" as an answer. And then the code answers "Bye!" when the -1 is inputed, since it is used to quit. Could I have some help adjusting this code to do that. Thank you!
character=input ('Enter ASCII code(s) or -1 to quit', 's')
n=32:126; %ASCII codes
for i=1:length(n)
fprintf('%c=%3d\n',n(i),n(i));
if input<32
output=?
if input>126
output=?
if input=-1
output='Bye!'
end
end
end
end

回答 (1 件)

Adam Danz
Adam Danz 2020 年 3 月 19 日
編集済み: Adam Danz 2020 年 3 月 19 日

0 投票

You can use a switch/case or if/else as shown below.
input is a scalar, integer value (not to be confused with the input() function).
if input < 32 || input > 126
output = '?';
elseif input == -1
output = 'Bye!';
else
output = char(input);
end

16 件のコメント

Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
Adam Danz
Adam Danz 2020 年 3 月 19 日
編集済み: Adam Danz 2020 年 3 月 19 日
In my answer, integer is a numeric scalar value (a single, numberic value). For example, input = 99.
If you're using the input function, change that variable name because Matlab will think that you're using the input function.
Do you intend to enter a vector of values or a single numeric value?
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
I intend to enter a single numeric value, and obtain the corresponding character for it from the ASCII code. How would I change the input function?
Adam Danz
Adam Danz 2020 年 3 月 19 日
You won't change the input function. You'll change the variable name "input" from my answer. You can change it to any variable name such as inputNumber.
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
It's still not working, please Im desperate. I will post the code I have. The desired output is, you can type in a number between 32 and 126 and get the character it is on the ASCII. If they put -1, the code says "Bye!", and if its not between 32-126 is says "?"
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
Adam Danz
Adam Danz 2020 年 3 月 19 日
編集済み: Adam Danz 2020 年 3 月 19 日
"It's still not working,"
Are you getting an error? An unexpected output?
Screenshots aren't that helpful because I can't copy/paste your code.
Also, why is n = 32:126 in there? And why the loop?
Take a few minutes and read the code. Look what it's doing. Make sense of it before running it. It's only a few lines of code. It seems like you're just copy/pasting without thinking about it.
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
it says "index in position 1 exceeds array bounds (must not exceed 1)." and "error in line 1: character = input ('Enter ASCII code(s) or -1 to quit','s');
Adam Danz
Adam Danz 2020 年 3 月 19 日
My bet is that you have a variable in the workspace named input. Remove it.
clear('input')
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
number=input ('Enter ASCII code(s) or -1 to quit','s');
n=32:126;
for i=1:length(n)
if inputNumber <32 || inputNumber>126
output='?';
elseif inputNumber == -1
output= 'Bye!';
else
output= char (inputNumber);
end
end
BobH
BobH 2020 年 3 月 19 日
Merging Charlotte's code and Adam's answer to remove the variable which shares a name with function
userinput = input('Enter ASCII code or -1 to quit ');
if userinput < 32 || userinput > 126
output = '?';
elseif userinput == -1
output = 'Bye!';
else
output = char(userinput);
end
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
I
I copied it exact and the reponses in the command window were:
>> Index exceeds the number of array elements (95).
Error in (line 1)
userinput = input ('Enter ASCII code(s) or -1 to quit');
Adam Danz
Adam Danz 2020 年 3 月 19 日
@Charlotte Reed, note BobH's syntax with the input() function.
Charlotte Reed
Charlotte Reed 2020 年 3 月 19 日
Sorry, what do you mean by that?
Walter Roberson
Walter Roberson 2020 年 3 月 19 日
clear input
You have a left-over variable named input
Adam Danz
Adam Danz 2020 年 3 月 20 日
編集済み: Adam Danz 2020 年 3 月 20 日
See my comment here that suggests to clear the input variable in addition to Walter's comment above.
Please take time to read and think about the code.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2020 年 3 月 19 日

編集済み:

2020 年 3 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by