473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Displaying ASCII values

180 100+
Hello All,
How do I display ASCII values from A..Z?
I tried the following but get some compiler error:

Expand|Select|Wrap|Line Numbers
  1. def DisplayAscii()
  2.     for index in 'A'..'Z' do
  3.         puts ?index
  4.     end    
  5. end
Please help...
Oct 12 '07 #1
7 7100
improvcornartist
303 Expert 100+
The ? only looks at the next character, i, so the remaining 'ndex' causes an error. I'm not sure how to use ? to get what you want, but using a string element reference would work.

Expand|Select|Wrap|Line Numbers
  1. for index in 'A'..'Z' do
  2.   puts index[0]
  3. end
Oct 12 '07 #2
vermarajeev
180 100+
The ? only looks at the next character, i, so the remaining 'ndex' causes an error. I'm not sure how to use ? to get what you want, but using a string element reference would work.

Expand|Select|Wrap|Line Numbers
  1. for index in 'A'..'Z' do
  2.   puts index[0]
  3. end
But I need to use '?' to display the ASCII values of the characters.
Oct 16 '07 #3
improvcornartist
303 Expert 100+
Is this homework? What else are you allowed to use? Eval would work.
Oct 16 '07 #4
vermarajeev
180 100+
Is this homework? What else are you allowed to use? Eval would work.
Homework, absolutely no. Why do you think that could be a homework? I come from a C++ background and learning Ruby. The best way to learn any language is to play with it. I want to know how do I display the ASCII value of a char using the '?'. By the way how do you declare a character in Ruby?
Oct 17 '07 #5
improvcornartist
303 Expert 100+
I only thought it could possibly be homework because you said you "need to use '?'". The loop I gave you would do the same thing - it would list the ASCII values. Since you rejected it and said you needed to use '?' I thought it may be a homework requirement or something. In any case, iterating a loop like you tried doesn't work because of the way '?' is defined. However, if you use eval, you can use an index and then evaluate the expression.

Expand|Select|Wrap|Line Numbers
  1. for index in 'A'..'Z' do
  2.   puts eval("?#{index}")
  3. end
This will fill in the index, then get the ? value of that index. For defining a character, I don't think Ruby actually has a character class. So you would probably use the same method as you would for a string, char = 'A'. I'm still fairly new to Ruby myself, so I don't know if there's a better way to do characters. If so, I haven't found it.
Oct 17 '07 #6
KOTP
2
Expand|Select|Wrap|Line Numbers
  1. for index in 'A'..'Z' do
  2.   puts eval("?#{index}")
  3. end
Without using eval you can simply do this, if you want to simply puts the ascii for A - Z

Expand|Select|Wrap|Line Numbers
  1. ('A'..'Z').each do |x|
  2.   puts x[0]
  3. end
Jul 31 '08 #7
improvcornartist
303 Expert 100+
Without using eval you can simply do this, if you want to simply puts the ascii for A - Z

Expand|Select|Wrap|Line Numbers
  1. ('A'..'Z').each do |x|
  2.   puts x[0]
  3. end
You are correct, using x[0] works. That was actually my solution in post #2. But the original poster wanted to use the '?', which is why I used eval.
Jul 31 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: chunhui_true | last post by:
i have a class, it can read one line(\r\n ended) from string,when i read line from utf8 string i can't get any thing! maybe i should conversion utf8 to ascii??there is any function can conversion...
1
by: Kermit Piper | last post by:
Hello, I have a function that lets me convert one character and throw an alert with the corresponding ASCII value, but what I am having trouble with is applying it to a text box. What I'm trying...
12
by: korund | last post by:
How to make javascript alert with non-english text displaying correctly on computers where english only is default system & language settings? For web page the solution is just use meta tags:...
7
by: Jeffrey Spoon | last post by:
Hello, I'm a bit stuck trying to convert a text file which contains extended ASCII text and changing the ASCII values so they become readable. I do this by subtracting 127 from the ASCII value....
0
by: nmsreddi | last post by:
Hi friends I am working on serialport in c# ,i am using C#2005 i have successfully done the serial communication with GSM modem and able to send and receive data , the main problem ,the serial...
6
by: ssetz | last post by:
Hello, For work, I need to write a password filter. The problem is that my C+ + experience is only some practice in school, 10 years ago. I now develop in C# which is completely different to me....
4
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. ...
5
by: tushar.saxena | last post by:
This post is a follow up to the post at : http://groups.google.com/group/comp.lang.c++/browse_thread/thread/83af6123fa945e8b?hl=ug#9eaa6fab5622424e as my original question was answered there, but I...
9
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.