Convert hex string to int in Crystal
Asked Answered
K

1

6

I want to convert an hex string value in a variable to int.

This works for a literal: "#{0xFF}".to_i

But with a var...?

Not working test (interpolation error):

_myvar = "FF"
"#{0x_myvar}".to_i
Karli answered 25/12, 2016 at 9:21 Comment(0)
M
7

Why not check the API documentation for String

"FF".to_i(16)
Merciful answered 25/12, 2016 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.