2011/02/13

The chr() in Ruby.

Cause I have to write bytes string using Ruby.

I  collation some rules of chr() to clarify my mind.

At first. create new file.

file = File.new("helloRuby", "w+")

 

There are three way to get the same result :

file.write(65.chr)

file.write(Integer(0x41).chr)

file.write("A")

upon three all get the hexdump result like :

00000000  41                                                |A|


And if using String.hex , ruby will write the string of hex number to file

file.write("A".hex)

00000000  31 30                                             |10|

 

Then, if I want to write the real hex byte to file.

I can using

file.write("A".hex.chr)

file.write(Integer(10).chr)

00000000  0a 0a                                             |..|


Important note :

chr() of Number form and chr() of String form will get the different result !

 

沒有留言:

張貼留言