hexString -> [UInt8] -> hexString

In cadence you can have a hex string say "776f6f74" which you can then use decodeHex with to get a [UInt8] which would give you [119, 111, 111, 116]

let hexString = "776f6f74"
let hexArray = hexString.decodeHex()
assert(hexArray == [119, 111, 111, 116])

I am looking for a way in cadence to do the inverse, go from [119, 111, 111, 116] to the hexString of "776f6f74"

let hexArray = [119, 111, 111, 116]
let hexString = lookingForThisFunction(hexArray)
assert(hexString == "776f6f74")

// so that
let hex = "776f6f74"
assert(lookingForThisFunction(hex.decodeHex()) == hex)

My questions:

  • does lookingForThisFunction from the above example exist?
  • if it doesn’t exist, is there a straight forward way for me to make it exist in cadence?
1 Like

No, this currently does not exist in the standard library (it’s still quite small), but could be easily added. I imagine it could be something like String.encodeHex(_ data: [UInt8]): String.

Please feel free to open a feature request in the Cadence repo. This would be a good first issue :wink:

1 Like

Thanks @turbolent I will open an issue.

Issue created: Function that converts a `[UInt8]` to a hex string. · Issue #952 · onflow/cadence · GitHub

Pull request up: Add a String constructor function and a String.encodeHex function by turbolent · Pull Request #953 · onflow/cadence · GitHub
Thank you @turbolent