Skip to content

Commit

Permalink
fix hex bit strings with leading zeroes in postgresql
Browse files Browse the repository at this point in the history
ensure leading zeroes are preserved, i.e. "0x0F" results in the
bit string "00001111" instead of "1111"
  • Loading branch information
jstanley0 committed Mar 22, 2024
1 parent 68b20b6 commit 8ea1283
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def cast_value(value)
if ::String === value
case value
when /^0x/i
value[2..-1].hex.to_s(2) # Hexadecimal notation
# Hexadecimal notation (with possible leading zeroes)
format("%0*b", (value.rstrip.size - 2) * 4, value[2..].hex)
else
value # Bit-string notation
# Bit-string notation
value
end
else
value.to_s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ def test_roundtrip

record.a_bit = "11111111"
record.a_bit_varying = "0xF"
record.another_bit_varying = "0x0010"
record.save!

assert record.reload
assert_equal "11111111", record.a_bit
assert_equal "1111", record.a_bit_varying
assert_equal "0000000000010000", record.another_bit_varying
end
end

0 comments on commit 8ea1283

Please sign in to comment.