473,395 Members | 1,762 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,395 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 7099
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.