tcoo_a
Last Updated: February 25, 2016
·
250
· dionysios

Convert to Decimal Representation in Q

q
kdb
sv

The verb "sv" (string from vector) if used with numeric arguments on both sides will output the decimal representation of the number on its right using as base the number on its left.

The number on the left can be anything, including negative numbers, zero and one (contrary to the "Q for mortals" tutorial)

  • When the left operand is zero, the output is just the leftmost digit
  • When the left operand is equal to one, the output is just the sum of all digits converted into the decimal system.
  • When the left operand is negative, then the output is computed as for the equivalent positive base, but with the odd powers negated.

Note that all digits are first converted to the decimal system and then added to the total.

The only bases possible are 2, 10 and 256.

Examples:

0 sv 1110b /output is 0

0 sv 0xffff0a /output is 10

1 sv 1110b / output is 3

1 sv 0xffff0a / output is 255 + 255 + 10 = 520

-2 sv 1110b /output is 0 * 1 + 1 *(-2) + 1 * (-2)^2 + 1 * (-2)^3 = -6

-3 sv 0xffff0a /output is 10 + 255 * (-3) + 255 * (-3)^2 = 1540