필터 지우기
필터 지우기

ASCII coding to get corresponding character

조회 수: 2 (최근 30일)
Charlotte Reed
Charlotte Reed 2020년 3월 19일
편집: Adam Danz 2020년 3월 20일
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일
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
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by