base-conversion

PHP - Generate an 8 character hash from an integer

你。 提交于 2019-11-27 18:19:53
问题 Is there a way to take any number, from say, 1 to 40000 and generate an 8 character hash? I was thinking of using base_convert but couldn't figure out a way to force it to be an 8 character hash. Any help would be appreciated! 回答1: Why don't you just run md5 and take the first 8 characters? Because you are wanting a hash, it doesn't matter whether portions are discarded, but rather that the same input will produce the same hash. $hash = substr(md5($num), 0, 8); 回答2: >>> math.exp(math.log

Converting an integer to a hexadecimal string in Ruby

半世苍凉 提交于 2019-11-27 16:43:56
Is there a built in way to convert an integer in Ruby into its hexadecimal equivalent? Something like the opposite of String#to_i : "0A".to_i(16) #=>10 Like perhaps: "0A".hex #=>10 I know how to roll my own, but it's probably more efficient to use a built in Ruby function. Jean You can give to_s a base other than 10: 10.to_s(16) #=> "a" flxkid How about using % / sprintf : i = 20 "%x" % i #=> "14" To summarize: p 10.to_s(16) #=> "a" p "%x" % 10 #=> "a" p "%02X" % 10 #=> "0A" p sprintf("%02X", 10) #=> "0A" p "#%02X%02X%02X" % [255, 0, 10] #=> "#FF000A" Ultrasaurus Here's another approach:

Base 10 to base 2,8,16 conversion in java [duplicate]

人走茶凉 提交于 2019-11-27 14:17:37
问题 This question already has an answer here: In Java how do you convert a decimal number to base 36? 10 answers I'm in this Object Oriented class, but I just don't know how to do this. I know basic fundamentals but nothing like this. I am asked to create a program that converts #'s base 10 to base 2, base 8,and base 16. Then, after we are asked to convert it back from 2,8,16 to base 10. I have gathered some information from other websites and I actually ended up editing a bit of it. Please help!

Converting an integer to a hexadecimal string in Ruby

岁酱吖の 提交于 2019-11-26 18:44:03
问题 Is there a built in way to convert an integer in Ruby into its hexadecimal equivalent? Something like the opposite of String#to_i: "0A".to_i(16) #=>10 Like perhaps: "0A".hex #=>10 I know how to roll my own, but it's probably more efficient to use a built in Ruby function. 回答1: You can give to_s a base other than 10: 10.to_s(16) #=> "a" 回答2: How about using %/sprintf: i = 20 "%x" % i #=> "14" 回答3: To summarize: p 10.to_s(16) #=> "a" p "%x" % 10 #=> "a" p "%02X" % 10 #=> "0A" p sprintf("%02X",