Ruby/Converting data to another type2

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application
Converting data to another type: type conversion or typecasting, page 2

What we've observed from these example is the fact that spaces will be ignored when converting from strings to integers." 555 " converted to integer will result in "555" When converting something different than a string of numbers, the result will be 0 if you use the ".to_s" method or an error if you use the "Integer()" instead.

If you are working with binary,octal or hexadecimal strings, there are certain rules to be followed when converting.

-> a string in binary needs to start with 0b

-> a string in octal needs to start with 0

-> a string in hexadecimal needs to start with 0x

Converting from any of these will work only with the Integer method, ".to_i" will return 0.

If you want to use the ".to_i" method, you don't need to use any supplementary tag indicators (0b,0 or 0x) before your string.Instead, you will use parameters (10 for base 10, 2 for binary, 8 for octal and 16 for hexadecimal )

Image:Typecast2.gif

Remember not to include any numbers higher than the base indicator in your string or it will get ignored or result in a an error if you used the Integer method.

So if the base is 2, make sure your string contains only 0 and/or 1, if the base is 8, make sure the string does not contain 8 itself too because from 0 to 7 there are already 8 numbers and so on...

The ".to_i" method supports conversions up to base 36.

You can also convert a number straight to a certain base by doing something similar:

x=555.to_s(2)

Or use the % method included in the String class:

binary="%b" % 555
octal="%o" % 555
hexadecimal="%x" % 555

Image:Typecast3.gif

Previous Next
Personal tools