I want to create an Integer-to-Hex function for all integer types.
For 1-byte Int8, it returns two letters, eg 0A
For 2-byte Int16, it returns four letters, eg 0A0B
for 8-byte Int64, it returns 16 letters, eg, 0102030405060708
func hex(v: Int) -> String {
var s = ""
var i = v
for _ in 0..<sizeof(Int)*2 {
s = String(format: "%x", i & 0xF) + s
i = i >> 4
}
return s
}
func hex(v: Int64) -> String {
var s = ""
var i = v
for _ in 0..<sizeof(Int64)*2 {
s = String(format: "%x", i & 0xF) + s
i = i >> 4
}
return s
}
func hex(v: Int32) -> String {
var s = ""
var i = v
for _ in 0..<sizeof(Int32)*2 {
s = String(format: "%x", i & 0xF) + s
i = i >> 4
}
return s
}
func hex(v: Int16) -> String {
var s = ""
var i = v
for _ in 0..<sizeof(Int16)*2 {
s = String(format: "%x", i & 0xF) + s
i = i >> 4
}
return s
}
func hex(v: Int8) -> String {
var s = ""
var i = v
for _ in 0..<sizeof(Int8)*2 {
s = String(format: "%x", i & 0xF) + s
i = i >> 4
}
return s
}
The above code works fine.
I then tried to create a generic version like this:
func hex<T: IntegerType>(v: T) -> String {
var s = ""
var i = v
for _ in 0..<sizeof(T)*2 {
s = String(format: "%x", i & 0xF) + s
i = i >> 4
}
return s
}
When compiling this code, I got the error: T is not convertible to Int
What is the correct way to achieve this task?
Very simple solution is to coalesce the input value into IntMax
with .toIntMax()
.:
func hex<T: IntegerType>(v: T) -> String {
var s = ""
var i = v.toIntMax()
for _ in 0..<sizeof(T)*2 {
s = String(format: "%x", i & 0xF) + s
i >>= 4
}
return s
}
Note: This works with only 0...Int64.max
values.
But, I would do:
func hex<T: IntegerType>(v: T) -> String {
return String(format:"%0\(sizeof(T) * 2)x", v.toIntMax())
}
Note: This works with only 0...UInt32.max
values.
Added: This works with all available integer types/values.
func hex<T:IntegerType>(var v:T) -> String {
var s = ""
for _ in 0..<sizeof(T) * 2 {
s = String(format: "%X", (v & 0xf).toIntMax()) + s
v /= 16
}
return s
}
.toIntMax()
to cast T
to concrete integer type./ 16
instead of >> 4
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With