Swift BigInt to [UInt8]

2019-08-20 05:14发布

问题:

I am using swift 4 and am using this implementation of BigInt: https://github.com/attaswift/BigInt

I noticed there are no native methods to convert a BigInt or NSDecimalNumber into a [UInt8], do any of you know a good implementation of this as I have not been able to find one.

Thank you

回答1:

BigUInt has a

/// Return a `Data` value that contains the base-256 representation of this integer,
/// in network (big-endian) byte order.
public func serialize() -> Data 

method (see Data Conversion.swift), so what you can do is

let bi: BigInt = ...
let bytes = Array(BigUInt(bi).serialize())