本文介绍了如何为所有Integer类型创建一个通用的整数到十六进制函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为所有整数类型创建一个Integer-to-Hex函数。

对于1字节的Int8,它返回两个字母,例如0A



对于2字节Int16,它返回4个字母,例如8字节Int64的0A0B p

,它返回16个字母,例如,0102030405060708

  func十六进制(v:Int) - >字符串{
var s =
var i = v
for _ in 0 ..& lt; sizeof(Int)* 2 {
s = String(format: %x,i& 0xF)+ s
i = i>> 4
}
返回s
}

func十六进制(v:Int64) - >字符串{
var s =
var i = v
for _ in 0 ..& lt; sizeof(Int64)* 2 {
s = String(format: %x,i& 0xF)+ s
i = i>> 4
}
返回s
}

func十六进制(v:Int32) - >字符串{
var s =
var i = v
for _ in 0 ..& lt; sizeof(Int32)* 2 {
s = String(format: %x,i& 0xF)+ s
i = i>> 4
}
返回s
}

func十六进制(v:Int16) - >字符串{
var s =
var i = v
for _ in 0 ..& lt; sizeof(Int16)* 2 {
s = String(format: %x,i& 0xF)+ s
i = i>> 4
}
返回s
}

func十六进制(v:Int8) - >字符串{
var s =
var i = v
for _ in 0 ..& lt; sizeof(Int8)* 2 {
s = String(format: %x,i& 0xF)+ s
i = i>> 4
}
return s
}

上面的代码有效好的。

然后我试着创建一个这样的通用版本:

  func hex< T:IntegerType>(v:T) - >字符串{
var s =
var i = v
for _ in 0 ..< sizeof(T)* 2 {
s = String(format:%x ,i& 0xF)+ s
i = i>> 4
}
返回s
}

编译此代码时,我得到了错误:T不能转换为Int



完成此任务的正确方法是什么?

.toIntMax()

  func hex< T:IntegerType>(v:T) - >字符串{
var s =
var i = v.toIntMax()
for _ in 0 ..< sizeof(T)* 2 {
s = String(format :%x,i& 0xF)+ s
i>> = 4
}
返回s
}

注意:这只适用于 0 ... Int64.max 值。






但是,我会这样做:

  func hex< T:IntegerType>(v:T) - > String {
return String(format:%0\(sizeof(T)* 2)x,v.toIntMax())
}

注意:这仅适用于 0 ... UInt32.max 值。






新增:这适用于所有可用整数类型/值。

  func hex< T:IntegerType>(var v:T) - >字符串{
var s =
for _ in 0 ..< sizeof(T)* 2 {
s = String(format:%X,(v& 0xf) .toIntMax())+ s
v / = 16
}
return s
}




  • .toIntMax()转换为 T
  • / 16 而不是>> 4


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..&lt;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..&lt;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..&lt;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..&lt;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..&lt;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.

这篇关于如何为所有Integer类型创建一个通用的整数到十六进制函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 12:11