# Convert an Character to the ASCII Character Code

**URL:** <https://www.ruby-forum.com/t/convert-an-character-to-the-ascii-character-code/171575>\
**Category:** Ruby\
**Created:** [July 6, 2009, 12:32am UTC](https://www.ruby-forum.com/t/convert-an-character-to-the-ascii-character-code/171575 "2009-07-06T00:32:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [July 6, 2009, 12:32am UTC](https://www.ruby-forum.com/t/convert-an-character-to-the-ascii-character-code/171575/1 "2009-07-06T00:32:34Z")

</div>

I have Ruby 1.9.1p129 and I want to convert an ASCII character to an  
Integer and a Float.

While reading the following web page, I found conflicts in the code  
usage:

‘Convert an Character to the ASCII Character Code’

Integer (?e)  
=\> 101

(then further down)

Float (?e)  
=\> 101.0

I received errors as per the attached screen snapshot.

I have found out that it is because I have version 1.9 and the above was  
only workable in version 1.8.

I would still like to know how to get the same results using my current  
version.

Thanks.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [July 6, 2009, 1:13am UTC](https://www.ruby-forum.com/t/convert-an-character-to-the-ascii-character-code/171575/2 "2009-07-06T01:13:49Z")

</div>

On Mon, Jul 06, 2009 at 07:32:38AM +0900, John Gregory wrote:

> I would still like to know how to get the same results using my current  
> version.

How about this:

“e”.getbyte(0)

and:

“e”.getbyte(0).to\_f

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [July 6, 2009, 1:23am UTC](https://www.ruby-forum.com/t/convert-an-character-to-the-ascii-character-code/171575/3 "2009-07-06T01:23:35Z")

</div>

Aaron P. wrote:

> On Mon, Jul 06, 2009 at 07:32:38AM +0900, John Gregory wrote:
> 
> > I would still like to know how to get the same results using my current  
> > version.
> 
> How about this:
> 
> “e”.getbyte(0)
> 
> and:
> 
> “e”.getbyte(0).to\_f

That is perfect, thanks a lot!

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [July 6, 2009, 9:58am UTC](https://www.ruby-forum.com/t/convert-an-character-to-the-ascii-character-code/171575/4 "2009-07-06T09:58:55Z")

</div>

Aaron P. wrote:

> On Mon, Jul 06, 2009 at 07:32:38AM +0900, John Gregory wrote:
> 
> > I would still like to know how to get the same results using my current  
> > version.
> 
> How about this:
> 
> “e”.getbyte(0)
> 
> and:
> 
> “e”.getbyte(0).to\_f

Or:

irb(main):001:0\> “e”.ord  
=\> 101

This is a bit different, as it works in terms of codepoints (characters)  
rather than bytes. This won’t make a difference if you are using  
US-ASCII only.

irb(main):003:0\> “Ãª”.encoding  
=\> #Encoding:UTF-8  
irb(main):004:0\> “Ãª”.ord  
=\> 234  
irb(main):005:0\> “Ãª”.bytesize  
=\> 2  
irb(main):006:0\> “Ãª”.getbyte(0)  
=\> 195  
irb(main):007:0\> “Ãª”.getbyte(1)  
=\> 170
