Conversion from hex to decimal in ruby - ruby

0xFFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFE_FFFFFC2F.ord
=> 115792089237316195423570985008687907853269984665640564039457584007908834671663
0xFFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFE_FFFFFC2F.to_i
=> 115792089237316195423570985008687907853269984665640564039457584007908834671663
Could somebody explain me why both of these methods return the same integer value?
I'm not sure that i understand the method ord...
Returns the codepoint of the first character of the string, assuming a single-byte character encoding"
I found that, but here I've go a hex value and i used the method then get a decimal value, so "What the hex?" :D
PS: Is it decimal value for sure or am I wrong?

You've quoted String#ord and here you call #ord on Integer - this value is not decimal, nor string, but integer.
Integer#ord docs:
ord → self
Returns the int itself.
97.ord #=> 97
This method is intended for compatibility to character literals in Ruby 1.9.

Related

Why does typecasting a single byte to string not work in go?

I am trying to convert a single byte value to a string in golang. When I do a typecast of a byte to string like string(byte) and print the value I get "{" in response. I read other answers that the correct way to convert a byte to string would be strconv.Itoa(int(bytevalue)). Why does the former not work and why is the latter approach correct.
The expression string(bytevalue) is a conversion, not a typecast.
The specification says this about conversions from numeric types to a string:
Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.
The expression string(byte(123)) evaluates to the string "{" because { is the the string containing the UTF-8 representation of the rune 123.
Use the strconv package to get the decimal representation of the byte. The expression strconv.Itoa(int(byte(123))) evaluates to the string "123".

Decode in Ruby on rails

Is there any way to decode the below string,
"location.replace(i+\"&utm_content=\"+s)}(document,window,navigator,screen,\"\\x68\\x74\\x74\\x70\\x3a\\x2f\\x2f\\x6d\\x6f\\x62\\x76\\x69\\x64\\x69\\x2e\\x6d\\x6f\\x62\\x73\\x74\\x61\\x72\\x72\\x2e\\x63\\x6f\\x6d\\x2f\\x3f\\x75\\x74\\x6d\\x5f\\x74\\x65\\x72\\x6d\\x3d\\x36\\x35\\x34\\x33\\x34\\x39\\x39\\x37\\x36\\x39\\x31\\x38\\x32\\x39\\x34\\x36\\x33\\x30\\x32\\x26\\x63\\x6c\\x69\\x63\\x6b\\x76\\x65\\x72\\x69\\x66\\x79\\x3d\\x31\",fi
I have tried as,
URI.unescape string
But its not working
There may be another way to do this, but here's one way:
>> hex = "\\x68\\x74\\x74\\x70\\x3a\\x2f\\x2f\\x6d\\x6f\\x62\\x76\\x69\\x64\\x69\\x2e\\x6d\\x6f\\x62\\x73\\x74\\x61\\x72\\x72\\x2e\\x63\\x6f\\x6d\\x2f\\x3f\\x75\\x74\\x6d\\x5f\\x74\\x65\\x72\\x6d\\x3d\\x36\\x35\\x34\\x33\\x34\\x39\\x39\\x37\\x36\\x39\\x31\\x38\\x32\\x39\\x34\\x36\\x33\\x30\\x32\\x26\\x63\\x6c\\x69\\x63\\x6b\\x76\\x65\\x72\\x69\\x66\\x79\\x3d\\x31"
=> "\\x68\\x74\\x74\\x70\\x3a\\x2f\\x2f\\x6d\\x6f\\x62\\x76\\x69\\x64\\x69\\x2e\\x6d\\x6f\\x62\\x73\\x74\\x61\\x72\\x72\\x2e\\x63\\x6f\\x6d\\x2f\\x3f\\x75\\x74\\x6d\\x5f\\x74\\x65\\x72\\x6d\\x3d\\x36\\x35\\x34\\x33\\x34\\x39\\x39\\x37\\x36\\x39\\x31\\x38\\x32\\x39\\x34\\x36\\x33\\x30\\x32\\x26\\x63\\x6c\\x69\\x63\\x6b\\x76\\x65\\x72\\x69\\x66\\x79\\x3d\\x31"
>> Array(hex.gsub("\\x","")).pack('H*')
=> "http://mobvidi.mobstarr.com/?utm_term=6543499769182946302&clickverify=1"
I created a string variable for the hex string and then stripped out the backslashes and 'x' characters. Then, this is converted into an array so we can call the pack method (specifying the capital H string directive for a high nibble first hex string) which you can read about here.

Keep leading zeroes when converting string to integer

For no particular reason, I am trying to add a #reverse method to the Integer class:
class Integer
def reverse
self.to_s.reverse.to_i
end
end
puts 1337.reverse # => 7331
puts 1000.reverse # => 1
This works fine except for numbers ending in a 0, as shown when 1000.reverse returns 1 rather than 0001. Is there any way to keep leading zeroes when converting a string into an integer?
Short answer: no, you cant.
2.1.5 :001 > 0001
=> 1
0001 doesn't make sense at all as Integer. In the Integer world, 0001 is exactly as 1.
Moreover, the number of leading integer is generally irrelevant, unless you need to pad some integer for displaying, but in this case you are probably converting it into another kind of object (e.g a String).
If you want to keep the integer as Fixnum you will not be able to add leading zeros.
The real question is: why do you want/need leading zeros? You didn't provide such information in the question. There are probably better ways to achieve your result (such as wrapping the value into a decorator object if the goal is to properly format a result for display).
Does rjust work for you?
1000.to_s.reverse.to_i.to_s.rjust(1000.to_s.size,'0') #=> "0001"
self.to_s.to_i does convert the integer to a string and this string "0001" to an integer value. Since leading zeros are not required for regular numbers they are dropped. In other words: Keeping leading zeros does not make sense for calculations, so they are dropped. Just ask yourself how the integer 1 would look like if leading zeros would be preserved, since it represents a 32 bit number. If you need the leading zeros, there is no way around a string.
BUT 10 + "0001".to_i returns 11, so you probably need to override the + method of the String class.

How do I retrieve the string value of a letter in an array?

I am trying to retrieve the value of a letter in an array. The array I have is:
a = ["a","b","c","d","e","f","g","h"].
If I want to retrieve a letter's value, the code is:
?a
However, when I tried ?a[5] it gives me 0. Does anyone know why this is the case?
--sorry to clarify I am using 1.8.7 :)
Actually, as of Ruby 1.9. ?a will give you the string "a", where it used to give you the ASCII code for that character. It's just another (limited) string literal mechanism at this point.
If you want a characters value, you need to use the ord method, as in a[5].ord or ?a.ord. ord is documented at http://ruby-doc.org/core-2.0.0/String.html#method-i-ord
The behavior of the ? operator and the String [] method when passed a single Fixnum changed at the same time in Ruby 1.9, with both returning a one-character string where they used to return a Fixnum.
As an aside, I can't find the documentation of the ? operator in the Literals/String section of the same official reference.
That is because of associativity/precedence. ?a[5] is interpreted as (?a)[5], not as ?(a[5]). I suppose you expected the latter, but it actually is the former.
In Ruby 1.8 ?a == 'a'[0]. Info about the ? operator (search for: "Integer and Floating Point Numbers")
Therefore you can get the value for a letter in the array a with:
a[5][0]
Whereas in Ruby > 1.9 use the method ord: a[5].ord

Hexa to decimal conversion in Ruby

I have "\001\022" as value of a. my desired decimal value is 274.
I tried following function . but I get ["0112"]
a.unpack("H*") ==> ["0112"]
When I convert this "0112" to decimal using calculator it gives me 274. How can i get like
this using ruby methods.
Thanks
The format string in your question: "H*", is for "hex string (high nibble first)". Therefore it decoded your string as an array of 4-bit hexadecimal elements.
You need a different format.
Try this, which decodes it as a "16-bit unsigned, network (big-endian) byte order" integer:
a.unpack("n") # => [274]
For full details on what characters you can use in the format string, check the Ruby Documentation for String#unpack.

Resources