Apoc 5.5 missing function apoc.convert.toInteger('0x' + hex)

Hi neo4j team,

I've been using poc.convert.toInteger to convert hex to integer values. Now that some convert functions are discontinued or moved to cypher I'm missing this feature (though I've got a workaround).

with
  ['fc','fc','fc'] as rgb

return
  {
    r: apoc.convert.toInteger('0x' + rgb[0]),
    g: apoc.convert.toInteger('0x' + rgb[1]),
    b: apoc.convert.toInteger('0x' + rgb[2])
  } as rgb

You can just treat the hex values as literals:

RETURN toInteger(0xfc)
----------------------------
252

or even

RETURN 0xfc
----------------
252

No, I can't, because the literal is concatenated. The toInteger() function then handles it as a string.

1 Like

Ah - gotcha. What is the workaround you mentioned?

Not that fancy...

with
  ['fc','fc','fc'] as rgb

with
  [
    value in rgb |
    reduce(
      result = 0, char in split(toUpper(value),'') |
      result * 16 +
      case char
        when 'F' then 15
        when 'E' then 14
        when 'D' then 13
        when 'C' then 12
        when 'B' then 11
        when 'A' then 10 
        else toInteger(char)
      end
    )
  ] as rgb

return
  {
    r: rgb[0],
    g: rgb[1],
    b: rgb[2]
  } as rgb

... but it works. :sweat_smile:

1 Like