I want to convert an NSDecimalNumber to a byte array. Also, I do not want to use any library for that like BigInt, BInt, etc.
I tried this one:
static func toByteArray<T>(_ value: T) -> [UInt8] {
var value = value
return withUnsafeBytes(of: &value) { Array($0) }
}
let value = NSDecimalNumber(string: "...")
let bytes = toByteArray(value.uint64Value)
If the number is not bigger than Uint64, it works great. But, what if it is, how can we convert it?
The problem is obviously the use of
uint64Value, which obviously cannot represent any value greater thanUInt64.max, and your example, 59,785,897,542,892,656,787,456, is larger than that.If you want to grab the byte representations of the 128 bit mantissa, you can use
_mantissatuple ofUInt16words ofDecimal, and convert them to bytes if you want. E.g.And
Returning:
This, admittedly, only defers the problem, breaking the 64-bit limit of your
uint64Valueapproach, but is still constrained to the inherent 128-bit limit ofNSDecimalNumber/Decimal. To capture numbers greater than 128 bits, you'd need a completely different representation.NB: This also assumes that the exponent is
0. If, however, you had some large number, e.g. 4.2e101 (4.2 * 10101), the exponent will be100and the mantissa will simply be42, which I bet is probably not what you want in your byte array. Then again, this is an example of a number that is too large to represent as a single 128 bit integer, anyway:Yielding: