2014-10-28 83 views
1

是否有更好的方法來顯示以十六進制數字顯示前導0?我想:如何以十六進制數字打印前導零

i.to_s(16) 

2.to_s(16) #=> "2" 

,我希望"02"。我試圖打印格式:

"%02x" % i 

該協會致力於2,但

"%02x" % 256 #=> "100" 

,我想"0100"。所以,我想出了這個:

class Integer 
    def to_hex_string 
    ("%0x" % self).size % 2 == 0 ? "%0x" % self : "%0#{("%0x" % self).size+1}x" % self 
    end 
end 

它的工作原理:

2.to_hex_string #=> "02" 
256.to_hex_string #=> "0100" 

它的工作原理也與Bignumber類,但它看起來很奇怪,這樣一個簡單的請求需要這樣的伎倆。有什麼更好的想法

+5

如果你只想把一個零在號碼前面,嘗試' 「0#{i.to_s(16)}」'或' 「0%X」 %i'。但是這通常表示一個八進制數。常用的十六進制前綴是「0x」,可以使用「%#x」%i「打印。 – Stefan 2014-10-28 11:15:34

+0

感謝您的評論,但似乎to_s(16)與BigNumber – Bruno 2014-10-31 16:16:02

回答

0

你正在使這種方式太複雜。如果你要打印帶前導零十六進制整數,那麼它只是

class Integer 
    def to_hex_string 
    "0#{to_s(16)}" 
    end 
end 

2.to_hex_string # => 02 
256.to_hex_string # => 0100 
+0

錯誤地確定它是這樣更容易!謝謝! – Bruno 2014-10-28 15:17:45

+0

但它似乎是笨號的bug。如果您嘗試使用「ff」* 192例,如果將bug轉換爲bignum並返回十六進制 – Bruno 2014-10-28 15:34:14

+0

這個答案是錯誤的恕我直言:它總是在前面放一個0。我認爲在'2.to_hex_string' = 02的情況下OP需要0,但是'200.to_hex_string'可能應該返回「c8」而不是「0c8」,即前導0應該只加在奇數字符串長度上。 – claus 2016-11-25 09:33:30

1

是的,它的錯誤:

讓我們試試這個:

class Integer 
    def to_hex_string 
    "0#{to_s(16)}" 
    end 
end 

class BigNumber 
    def to_hex_string 
    "0#{to_s(16)}" 
    end 
end 

class String 
    def to_hex_string 
    self.unpack('H*').first 
    end 

    def to_bytes_string 
    unless self.size % 2 == 0 
     raise "Can't translate a string unless it has an even number of digits" 
    end 
    raise "Can't translate non-hex characters" if self =~ /[^0-9A-Fa-f]/ 
    [self].pack('H*') 
    end 

    def to_bignum 
    self.bytes.inject { |a,b| (a << 8) + b } 
    end 
end 

p a="ff"*192 # => "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 

p bytestring=a.to_bytes_string # => "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" 

p bytestring.to_hex_string # => "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 

p biga=a.to_bytes_string.to_bignum # => 24103124269210325885801166060283141129120932479456889513596750390652573915918032006690850241073460496634487662808880047878624169787949583249696129878907746514552133393816252247707820779176814996768455431373878200575973458579045991094613871220995079649978156413423006776294733552816174284117941639677858703703689691092215919430542320115627584500805795878509009937148922834766466311815150638048733751822605062469928378987059710125258433244

而且BUG是在這裏:

p biga.to_hex_string # => "0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 

哪裏這個0來自????

更令人奇怪的是,我複雜的解決方案工作:

p ("%0x" % biga).size % 2 == 0 ? "%0x" % biga : "%0#{("%0x" % biga).size+1}x" % biga # => "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" 

也許中的錯誤 「0#{to_s(16)}」?

+0

沒人,有什麼想法?錯誤? – Bruno 2014-11-03 12:37:33

0

這是我剛剛嘗試解決此問題時在Google上的第一次打擊。我不得不找到其他幾個帖子來完成我的解決方案,但我認爲這很乾淨。

class Fixnum 
    def to_hex(bits) 
    rjust = (bits/4 + (bits.modulo(4)==0 ? 0 : 1)) 
    "0x" + self.to_s(16).rjust(rjust, "0") 
    end 
end