Crystal: Signed hex parsing is wrong

Created on 7 Sep 2020  路  1Comment  路  Source: crystal-lang/crystal

I have these two numbers, 0xffea1a7f and 0xfffffa95.
Both are signed integer of size 32, evaluates to -1435009 and -1387 respectively.
Crystal parses the numbers as unsigned.

x : Int32 = 0xffea1a7f
y : Int32 = 0xfffffa95

puts "x = #{x}; y = #{y}"

# Error: type must be Int32, not Int64

And Crystal can't do this either:

x : Int32 = 0xffea1a7f_i32
y : Int32 = 0xfffffa95_i32

puts "x = #{x}; y = #{y}"

# Error: 4293532287 doesn't fit in an Int32

This is just an example, others signed integer are having the same behavior. Hex can be signed and unsigned, and we should let Crystal handle these situatin correctly, given that Crystal is supposed to be strong static-typing.

Most helpful comment

It's not documented in https://crystal-lang.org/reference/syntax_and_semantics/literals/integers.html but those are Int64 integer literals because they exceed the maximum size of Int32.

If you want negative numbers you always have tui use the minus sign. The rules for numbers are not like C.

>All comments

It's not documented in https://crystal-lang.org/reference/syntax_and_semantics/literals/integers.html but those are Int64 integer literals because they exceed the maximum size of Int32.

If you want negative numbers you always have tui use the minus sign. The rules for numbers are not like C.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asterite picture asterite  路  3Comments

Papierkorb picture Papierkorb  路  3Comments

ArthurZ picture ArthurZ  路  3Comments

oprypin picture oprypin  路  3Comments

cjgajard picture cjgajard  路  3Comments