2013-07-12 10 views
6

Znalazłem to dziwne, że String # hex w Ruby nie zwraca właściwej wartości szesnastkowej dla danego znaku. Mogę być nieporozumienie metodę, ale przyjąć następujący przykład:Ciąg Ruby # heksadecymalny błąd

'a'.hex 
=> 10 

Podczas gdy prawo wartość hex dla 'a' będzie 61:

'a'.unpack('H*') 
=> 61 

Am I czegoś brakuje? Po co jest hex? Wszelkie wskazówki są mile widziane!

Dzięki

+1

Co masz na myśli, mówiąc o "wartości heksadecymalnej" dla postaci? Ponieważ "a" jest szesnastkowe dla 16. Czy masz na myśli [kod ASCII] (http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters)? – rampion

+2

"a" jest szesnastkowe dla 10. "f" jest szesnastkowe dla 16. –

+2

W rzeczywistości "f" jest szesnastkowe dla 15. – robnasby

Odpowiedz

11

String#hex nie daje indeks ASCII znaku, to dla przekształcenia base-16 numer (sześciokątną adecimal) z łańcucha na liczbę całkowitą:

% ri String\#hex 
String#hex 

(from ruby site) 
------------------------------------------------------------------------------ 
    str.hex -> integer 


------------------------------------------------------------------------------ 

Treats leading characters from str as a string of hexadecimal digits 
(with an optional sign and an optional 0x) and returns the 
corresponding number. Zero is returned on error. 

    "0x0a".hex  #=> 10 
    "-1234".hex #=> -4660 
    "0".hex  #=> 0 
    "wombat".hex #=> 0 

dlatego wykorzystuje mapowanie normalne:

'0'.hex #=> 0 
'1'.hex #=> 1 
... 
'9'.hex #=> 9 
'a'.hex #=> 10 == 0xA 
'b'.hex #=> 11 
... 
'f'.hex #=> 15 == 0xF == 0x0F 
'10'.hex #=> 16 == 0x10 
'11'.hex #=> 17 == 0x11 
... 
'ff'.hex #=> 255 == 0xFF 

jest bardzo podobny do String#to_i podczas korzystania z bazy 16:

'0xff'.to_i(16) #=> 255 
'FF'.to_i(16) #=> 255 
'-FF'.to_i(16) #=> -255 

Od docs:

% ri String\#to_i 
String#to_i 

(from ruby site) 
------------------------------------------------------------------------------ 
    str.to_i(base=10) -> integer 


------------------------------------------------------------------------------ 

Returns the result of interpreting leading characters in str as an 
integer base base (between 2 and 36). Extraneous characters past the 
end of a valid number are ignored. If there is not a valid number at the start 
of str, 0 is returned. This method never raises an exception 
when base is valid. 

    "12345".to_i    #=> 12345 
    "99 red balloons".to_i #=> 99 
    "0a".to_i    #=> 0 
    "0a".to_i(16)   #=> 10 
    "hello".to_i    #=> 0 
    "1100101".to_i(2)  #=> 101 
    "1100101".to_i(8)  #=> 294977 
    "1100101".to_i(10)  #=> 1100101 
    "1100101".to_i(16)  #=> 17826049 
+0

Bardzo wyczerpująca odpowiedź. Usunąłem moją poprzednią odpowiedź, ponieważ nie miało to żadnego sensu. Dzięki za wyjaśnienie! – FullOfCaffeine

0

Jeszcze jedna przewaga nad metodą hex. '10 -0 'do 256.

Weź pod uwagę, że chcesz porównać'100'> '20'. Powinien zwracać wartość true, ale zwraca false. Użyj "100'.hex>" 20'.hex. Zwraca true. Co jest bardziej dokładne.