Re: Euphoria ASCII function
- Posted by Robert Craig <rds at MSN.COM> Jul 25, 1997
- 847 views
Steve Elder asks: > Is there a Euphoria function that will return an atom with the ASCII value > of a character passed to it? * Short answer: No there isn't. It isn't necessary. * Extremely long answer: In BASIC, APL, and many other languages, (but not C) there is a wrong-headed notion that the world should be divided into "characters" (or strings) versus "numbers". Some variables are "string" variables, while others are "numeric" variables, and we must never let the two types mix together without strict supervision! People who come to Euphoria from one of these languages, and even many C people, have conceptual difficulty with the fact that in Euphoria *there are no characters*, there are only numbers. That's why there is no function that will convert a character to an ASCII number. Wait a minute!, you say, if there are no characters or strings in Euphoria then what the heck are "Hello, World" and '5' doing in some Euphoria programs that I've seen? When Euphoria reads your program, the first thing that it does, along with discarding your comments, is to convert things like "Hello, World" and '5', into sequences and atoms like: {72,101,108,108,111,44,32,87,111,114,108,100} and: 53 If you consult a table, you will see that the numbers above are the ASCII codes for "Hello, World" and '5'. You could have typed these codes in yourself. Your program would run exactly the same. '5' is *exactly* the same a 53 as far as Euphoria is concerned. Try the following 3 line program: ? "Hello, World" ? '5' ? 53 The statement: x = '5' means *exactly* the same thing as: x = 53 You'll notice that sometimes we use double quotes and sometimes just single quotes. The difference is that single quotes give you an atom, while double quotes give you a sequence. Remember that *all* data in Euphoria is either an atom (single number), or a sequence (a collection of numbers). Try: ? "5" ? {5} You should get the same thing printed. OK, you say, if there are no characters or strings, then how can I read a string from a file, or display a string on my printer or monitor? The answer is, you don't read strings from files, you read a series of *numbers*. Your program can choose to manipulate those numbers *as if* they are strings of text, but that's up to you. Similarly, you send a series of numbers to your monitor or disk file or printer. Your printer may choose to put ink on your page to make an A shape when you send it a 65, but the "character" A doesn't exist in your Euphoria program, only the *number* 65. When you wrote: ? "Hello, World" you saw a bunch of numbers on your screen. Those are the ASCII numbers that actually exist in your program. To display a bunch of ASCII numbers so that they appear as human-readable text, use: puts(1, "Hello, World") which is exactly equivalent to using: puts(1, {72,101,108,108,111,44,32,87,111,114,108,100}) This causes Euphoria to send the numbers 72, 101, 108, ... to the operating system, and eventually pixels for the familiar alphabetic shapes are drawn on your screen. Are there any drawbacks to not making strings distinct from numbers? Well, if you've ever looked at an ex.err dump, you'll notice that Euphoria doesn't know which of your variables are supposed to contain "strings" and which are supposed to contain numbers, so it displays just numbers, or sometimes it shows both numbers and associated ASCII characters. If you didn't *get* any of the above, don't worry, I've explained this to people face to face, and they just ended up more confused! Other people, who's brains were never contaminated by the separation of characters and numbers, will wonder why I wasted so much time explaining the obvious. Regards, Rob Craig Rapid Deployment Software