How to convert an Int to Hex String in Swift

后端 未结 6 1071
我在风中等你
我在风中等你 2020-11-29 23:59

In Obj-C I used to convert an unsigned integer n to a hex string with

 NSString *st = [NSString stringWithFormat:@\"%2X\", n];

I tried for

6条回答
  •  温柔的废话
    2020-11-30 01:02

    With Swift 5, according to your needs, you may choose one of the three following methods in order to solve your problem.


    #1. Using String's init(_:radix:uppercase:) initializer

    Swift String has a init(_:radix:uppercase:) initializer with the following declaration:

    init(_ value: T, radix: Int = 10, uppercase: Bool = false) where T : BinaryInteger
    

    Creates a string representing the given value in base 10, or some other specified base.

    The Playground code below shows how to create a String instance that represents an integer value in hexadecimal format by using init(_:radix:uppercase:) and without having to import Foundation:

    let string1 = String(2, radix: 16)
    print(string1) // prints: "2"
    
    let string2 = String(211, radix: 16)
    print(string2) // prints: "d3"
    
    let string3 = String(211, radix: 16, uppercase: true)
    print(string3) // prints: "D3"
    

    #2. Using String's init(format:​_:​) initializer

    Foundation provides String a init(format:​_:​) initializer. init(format:​_:​) has the following declaration:

    init(format: String, _ arguments: CVarArg...)
    

    Returns a String object initialized by using a given format string as a template into which the remaining argument values are substituted.

    The Apple's String Programming Guide gives a list of the format specifiers that are supported by String and NSString. Among those format specifiers, %X has the following description:

    Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and uppercase A–F.

    The Playground code below shows how to create a String instance that represents an integer value in hexadecimal format with init(format:​_:​):

    import Foundation
    
    let string1 = String(format:"%X", 2)
    print(string1) // prints: "2"
    
    let string2 = String(format:"%02X", 1)
    print(string2) // prints: "01"
    
    let string3 = String(format:"%02X", 211)
    print(string3) // prints: "D3"
    
    let string4 = String(format: "%02X, %02X, %02X", 12, 121, 255)
    print(string4) // prints: "0C, 79, FF"
    

    #3. Using String's init(format:​arguments:​) initializer

    Foundation provides String a init(format:​arguments:​) initializer. init(format:​arguments:​) has the following declaration:

    init(format: String, arguments: [CVarArg])
    

    Returns a String object initialized by using a given format string as a template into which the remaining argument values are substituted according to the user’s default locale.

    The Playground code below shows how to create a String instance that represents an integer value in hexadecimal format with init(format:​arguments:​):

    import Foundation
    
    let string1 = String(format:"%X", arguments: [2])
    print(string1) // prints: "2"
    
    let string2 = String(format:"%02X", arguments: [1])
    print(string2) // prints: "01"
    
    let string3 = String(format:"%02X",  arguments: [211])
    print(string3) // prints: "D3"
    
    let string4 = String(format: "%02X, %02X, %02X",  arguments: [12, 121, 255])
    print(string4) // prints: "0C, 79, FF"
    

提交回复
热议问题