I'm converting arrays consisting of four byte values to 32bit numbers by executing the following code:
a = [0, 16, 82, 0]
i = a.map { |e| "%02x" % e }.join.to_i(16)
# => 1069568
It works as intended, but I wonder if there's a more elegant way to perform this task. Maybe not utilizing strings.
Using pack and unpack1:
a = [0, 16, 82, 0]
a.pack('C4').unpack1('L>')
#=> 1069568
C4 means 8-bit unsigned (4 times) and L> means 32-bit unsigned (big endian).
However, pack returns a binary string, so this is not string-free.
If you have one byte, that would be the result as is. If you add one byte to the right side, that would make the original result move two positions to the left (which means multiplying by 0x100, or 16 ** 2 = 256), and add the new byte. You can repeat this as many times as there are bytes.
a.inject{|acc, byte| acc * 0x100 + byte}
# => 1069568
Related
I want to convert a number, say 1, into a 32 bit binary number:
00000000000000000000000000000001
how can I do this to ensure the full string is of length 32, no matter how small the number might be?
I had a sprintf working for 8 bit binary, but not sure how to make it 32.
Use String#rjust:
1.to_s(2).rjust(32, '0')
#⇒ "00000000000000000000000000000001"
String#% (via sprintf):
'%032b' % 7
=> "00000000000000000000000000000111"
Using pack and unpack1:
[1].pack('L>').unpack1('B*')
#=> "00000000000000000000000000000001"
L indicates 32-bit unsigned integer, > denotes big endian. B indicates bit string, * outputs all available bits.
This will wrap around when exceeding the 32-bit unsigned integer range:
[4_294_967_294].pack('L>').unpack1('B*') #=> "11111111111111111111111111111110"
[4_294_967_295].pack('L>').unpack1('B*') #=> "11111111111111111111111111111111"
[4_294_967_296].pack('L>').unpack1('B*') #=> "00000000000000000000000000000000"
[4_294_967_297].pack('L>').unpack1('B*') #=> "00000000000000000000000000000001"
I need to convert the follow 4 byte integer to signed integer, like:
input 65535 value -1
input 65534 value -2
input 65533 value -3
and so on...
I tried the follow:
puts (65533).to_s(16) #=> fffd
puts (65533).to_s(16).unpack('s') #=> doesn't work properly... return 26214
Can someone help me with code above?
Best Regards
You could pack it as as an unsigned integer and then unpack it as a signed integer:
[65535, 65534, 65533].pack('S*').unpack('s*')
#=> [-1, -2, -3]
S / s denote 16-bit integers, you can also use L / l for 32-bit or Q / q for 64-bit.
I'm working on a codewars kata that uses the Luhn Algorithm
to validate credit card numbers. Already have my answer, which uses string conversion to split the digits and then reconvert to an integer and then use the rest of the algorithm. All well and good until I test it on an octal number.
The code starts as:
def validate(n)
n.to_s.split("") #n = 7867 gives me ["7","8","6","7"], which is fine
n.to_s.split("") #n = 0776 gives me ["5","1","0], when I need ["0","7","7","6"]
n.to_s.split("") #n = 0100 gives me ["6", "4"] when I need ["0","1","0","0"]
#other code here
end
where the method should be called on whatever n is plugged in.
How do I prevent an octal, binary, or hexidecimal, etc number from converting like that? Is there a way to keep the digits as is so I can use them?
Your problem is that you are obviously storing the number as a number, in n; or else you would not need to say n.to_s.
There must be a place where those numbers enter your program; a user prompt, or by reading a file or whatever. You likely have something like n = user_input.to_i somewhere, forcing ruby to convert the string to a number. Make sure that you treat n as string everywhere, and leading zeroes will stick around.
If you are using numeric literals in your program, you can explicitly say that they are decimal:
n = 0d0100 # or "0100".to_i
n.to_s.rjust(4,"0").split("")
#=> ["0", "1", "0", "0"]
Also, use rjust to make them 4 digits long in string form as credit/debit cards have four 4-digit numbers
To split credit card into an array of integer, you could do something like below:
cc = "4111 1111 1111 1111".gsub(/ /,"").split("").map(&:to_i)
#=> [4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Ruby implements PRNGs as "a modified Mersenne Twister with a period of 2**19937-1." 1
The way I understand MT is that it operates on 2^32 different seeds. What confuses me is that Random.new(seed) accepts arbitrarily big numbers such as Random.new(2**100).
However, I wasn't able to find (logical) collisions:
Random.new(1).rand(10**5) == Random.new(2**32-1).rand(10**5) => false
Random.new(1).rand(10**5) == Random.new(2**32).rand(10**5) => false
Random.new(1).rand(10**5) == Random.new(2**32+1).rand(10**5) => false
Given that we'd like to utilize MT's maximum seed range in the sense that we want to use as many different seeds as possible while still avoiding collisions with two different seeds, what seed range achieves this?
I tried understanding what is happening inside the Ruby's random implementation, but didn't get too far. https://github.com/ruby/ruby/blob/c5e08b764eb342538884b383f0e6428b6faf214b/random.c#L370
The Mersenne Twister sequence is 2 ** ( 624 * 32 - 1 ) - 1 long, and the seed value is used to set an internal state for the PRNG that directly relates to the position within that sequence.
The easiest-to-find repeat appears to be every 2 ** ( 624 * 32 ), and can be shown to work like this:
repeat_every = 2 ** ( 624 * 32 )
start_value = 5024214421 # Try any value
r1 = Random.new( start_value )
r2 = Random.new( start_value + repeat_every )
r17 = Random.new( start_value + 17 * repeat_every )
r23 = Random.new( start_value + 23 * repeat_every )
r1.rand == r2.rand
# true
r17.rand == r23.rand
# true
Or try this:
repeat_every = 2 ** ( 624 * 32 )
start_value = 5024214421 # Try any value
r1 = Random.new( start_value )
r2 = Random.new( start_value + repeat_every )
Array.new(10) { r1.rand(100) }
# => [84, 86, 8, 58, 5, 21, 79, 10, 17, 50]
Array.new(10) { r2.rand(100) }
# => [84, 86, 8, 58, 5, 21, 79, 10, 17, 50]
The repeat value relates to how Mersenne Twister works. The internal state of MT is an array of 624 32-bit unsigned integers. The Ruby source code you linked packs a Ruby Fixnum into an array - the magic command is
rb_integer_pack( seed, buf, len, sizeof(uint32_t), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER );
however, this isn't something easy to play with, it is defined in internal.h, so only really accessible if you work on Ruby interpreter itself. You cannot access this function from within a normal C extension.
The packed integer is then loaded to the MT's internal state by the function init_by_array. This is quite a complex-looking function - the packed seed value is not written literally into the state, but instead the state is generated item by item, adding in the supplied array values, using a variety of xors, additions and cross-referencing the previous value (the Ruby source here also adds in the packed array's index position, commented "non-linear", I think that is one of the referenced modifications to standard MT)
Note that the size of the MT sequence is smaller than 2 ** ( 624 * 32 ) - the repeat_every value I show here is skipping over 2 sequences at a time, but it is the easiest to find repeating seed value, because it is easy to see how it sets the internal state exactly the same (because the first 624 items in the array representation of the seed are identical, and that is all that gets used later). Other seed values will also produce the same internal state, but the relationship is a complex mapping that pairs up each integer in the 19938-bit space with another integer which creates the same state for MT.
I am looking for a way to convert a base-10 number into a base-N number where N can be large. Specifically i am looking at converting to base-85 and back again. Does anyone know a simple algorithm to perform the conversion? Ideally it would provide something like:
to_radix(83992, 85) -> [11, 53, 12]
Any ideas are appreciated!
Roja
That was kind of an interesting question, so I went a little overboard:
class Integer
def to_base(base=10)
return [0] if zero?
raise ArgumentError, 'base must be greater than zero' unless base > 0
num = abs
return [1] * num if base == 1
[].tap do |digits|
while num > 0
digits.unshift num % base
num /= base
end
end
end
end
This works for arbitrary bases. It only works for integers, although there is no reason why it couldn't be extended to work with any arbitrary number. Also, it ignores the sign of the number. Again, there is no reason why it must do that, but mainly I didn't want to have to come up with a convention for returning the sign in the return value.
class Integer
old_to_s = instance_method(:to_s)
define_method :to_s do |base=10, mapping=nil, sep=''|
return old_to_s.bind(self).(base) unless mapping || base > 36
mapping ||= '0123456789abcdefghijklmnopqrstuvwxyz'
return to_base(base).map {|digit| mapping[digit].to_s }.join(sep)
end
end
[Fixnum, Bignum].each do |klass|
old_to_s = klass.instance_method(:to_s)
klass.send :define_method, :to_s do |base=10, mapping=nil, sep=''|
return old_to_s.bind(self).(base) unless mapping || base > 36
return super(base, mapping, sep) if mapping
return super(base)
end
end
I also extended the to_s method so that it works with bases greater than 36. If you want to use a base greater than 36, you have to pass in a mapping object which maps the "digits" to strings. (Well, actually, all that is required is that you provide an object that responds to [] and returns something which responds to to_s. So, a string is perfect, but e.g. an array of integers also works.)
It also accepts an optional separator, which is used to separate the digits.
For example, this allows you to format an IPv4 address by treating it as a base-256 number and using the identity for the mapping and '.' as the separator:
2_078_934_278.to_s(256, Array.new(256) {|i| i }, '.') # => '123.234.5.6'
Here's an (incomplete) testsuite:
require 'test/unit'
class TestBaseConversion < Test::Unit::TestCase
def test_that_83992_in_base_85_is_11_53_12
assert_equal [11, 53, 12], 83992.to_base(85)
end
def test_that_83992_in_base_37_is_1_24_13_2
assert_equal [1, 24, 13, 2], 83992.to_base(37)
end
def test_that_84026_in_base_37_is_1_24_13_36
assert_equal [1, 24, 13, 36], 84026.to_base(37)
end
def test_that_0_in_any_base_is_0
100.times do |base|
assert_equal [0], 0.to_base(base)
assert_equal [0], 0.to_base(1 << base)
assert_equal [0], 0.to_base(base << base)
end
end
def test_that_84026_in_base_37_prints_1od_
assert_equal '1od_', 84026.to_s(37, '0123456789abcdefghijklmnopqrstuvwxyz_')
end
def test_that_ip_address_formatting_works
addr = 2_078_934_278
assert_equal '123.234.5.6', addr.to_s(256, (0..255).to_a, '.')
assert_equal '123.234.5.6', addr.to_s(256, Array.new(256) {|i| i}, '.')
end
def test_that_old_to_s_still_works
assert_equal '84026', 84026.to_s
assert_equal '1su2', 84026.to_s(36)
end
end
The pseudocode for this is fairly straightforward. To base 85 from unsigned integers:
digits := '';
while (number > 0)
digit := number % 85
digits := base85Digit(digit) + digits
number /= 85 // integer division so the remainder is rounded off
end while
And to base 10:
mult := 1
result := 0
for each digit in digits // starting from the rightmost working left
result += base10(digit) * mult
mult *= 85
end for
Just a general pseudocode algorithm:
initialize empty list
take current number mod base, store result at front of list
divide current number by base and floor it (integer division does this perfectly)
if result is still greater than zero, repeat at #2
83992 / 85 = 988, reminder 12
988 / 85 = 11, reminder 53
11 / 85 = 0, reminder 11
write the reminder in reverse order: 11, 53, 12 to get your base-85 number.
To get it back:
11 * 85^2 + 53 * 85^1 + 12 * 85^0 = 83992
The simplest algorithm that I can think of is (in pseudo-code):
N = base-10 number
1) N mod 85 = 1st number
2) tempVal = floor(N/85)
3) if(tempVal > 0 && tempVal < 85) then
tempVal= 2nd number
else
2nd number = (tempVal mod 85), then goto step (2), replacing N with N1
Base 85 is particularly useful for ASCII encoding of binary data, which I presume is what you're using it for. (However, if this is why you should ask yourself whether it's really worth the extra hassle and whether Base 64 won't be good enough.)
If you're using this as an encoding scheme, your job is going to be to convert integers (4 bytes) into groups of 5 base85 numbers. (How you deal with things that are not multiples of 4 bytes is up to you--usually the end is padded with zeros. See the Wikipedia page on Base 85 for details.)
The basic algorithm is quite simple: take the remainder on division of 85 when packing into base 85, then divide and repeat, until you're done. To go back again, repeatedly add the value and multiply by 85 until you're done. I'm not terribly familiar with Ruby, so the code here is a C/C++/Javaish style, which hopefully you can interpret:
// To base 85
unsigned int n = // your number
byte b85[5]; // What you want to fill
for (int i=0 ; i<5 ; i++) {
b85[4-i] = (n%85); // Fill backwards to get most significant value at front
n = n/85;
}
// From base 85
n = 0;
for (int i=0 ; i< 5 ; i++) {
n = n*85 + b85[i];
}
This is without worrying about overflow, without worrying about adding 33 to get into ASCII range, and without worrying about the convention that zero is encoded as z not !!!!!, and so on.
because I feel recursion is under-represented in the answers I give the following rough draft
def to_radix(int, radix)
int == 0 ? [] : (to_radix(int / radix, radix) + [int % radix])
end
Fixnum#to_s won't help you, as it only goes up to base 36.
I'm surprised that you're going up to base 85. Can you explain how radixs work?