D
Dave M
Hello all
I would like to convert integers to strings in a way that I haven't come
across in the Ruby functions I have looked at. I would like the string
to be exactly what the integer would be if I read it in from a binary file.
In other words, the integer 0x61626364 would be the string "abcd" after
the conversion. I can write code to do it, but I think there is
probably a better way than the approach I have come up with.
This works but seems very brute force to me:
# assumes 32 bit numbers.
class Bignum
def to_ls
i = self
s = ""
4.times { s.concat(i & 0xff); i >>= 8}
return s.reverse
end
end
puts 0x61626364.to_ls
abcd
Also, it seems that I need this for both Bignum and Fixnum to cover the
entire 32 bit number range.
Thanks
Dave M
I would like to convert integers to strings in a way that I haven't come
across in the Ruby functions I have looked at. I would like the string
to be exactly what the integer would be if I read it in from a binary file.
In other words, the integer 0x61626364 would be the string "abcd" after
the conversion. I can write code to do it, but I think there is
probably a better way than the approach I have come up with.
This works but seems very brute force to me:
# assumes 32 bit numbers.
class Bignum
def to_ls
i = self
s = ""
4.times { s.concat(i & 0xff); i >>= 8}
return s.reverse
end
end
puts 0x61626364.to_ls
abcd
Also, it seems that I need this for both Bignum and Fixnum to cover the
entire 32 bit number range.
Thanks
Dave M