diff --git a/Sources/Swift-Big-Number-Core.swift b/Sources/Swift-Big-Number-Core.swift index be2dbbe..374b5ca 100644 --- a/Sources/Swift-Big-Number-Core.swift +++ b/Sources/Swift-Big-Number-Core.swift @@ -121,6 +121,11 @@ public typealias Limb = UInt64 public typealias Digits = [UInt64] public typealias Digit = UInt64 +// Bytes allow to initialize and export BInt for operations like network related ones. + +public typealias Bytes = [UInt8] +public typealias Byte = UInt8 + // MARK: - Imports // ———————————————————————————————————————————————————————————————————————————————————————————— // |||||||| Operators ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| @@ -432,6 +437,19 @@ public struct BInt: { self.init(source) } + + /// Creates a new instance from a `[UInt8]` array + public init(bytes: Bytes) + { + var num = BInt() + + for byte in bytes + { + num = num << 8 | BInt(byte) + } + + self.init(sign: num.sign, limbs: num.limbs) + } // // @@ -551,6 +569,20 @@ public struct BInt: i += 1 } } + + /// Bytes of the number + public func getBytes() -> Bytes + { + var bytes = Bytes() + var copy = self + + while copy != 0 { + bytes.append(Byte(copy & 0xff)) + copy >>= 8 + } + + return bytes.reversed() + } // // diff --git a/Swift-BigNumber.xcodeproj/project.pbxproj b/Swift-BigNumber.xcodeproj/project.pbxproj index 8e49b86..e17e32d 100644 --- a/Swift-BigNumber.xcodeproj/project.pbxproj +++ b/Swift-BigNumber.xcodeproj/project.pbxproj @@ -364,7 +364,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0920; - LastUpgradeCheck = 0940; + LastUpgradeCheck = 1250; ORGANIZATIONNAME = "Marcel Kröker"; TargetAttributes = { AE8388A5203F1CAF00FAC88F = { @@ -546,7 +546,7 @@ GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = "BigNumber-iOS/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 11.3; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOS"; PRODUCT_NAME = BigNumber; @@ -577,7 +577,7 @@ GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = "BigNumber-iOS/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 11.3; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOS"; PRODUCT_NAME = BigNumber; @@ -604,7 +604,7 @@ DEVELOPMENT_TEAM = ""; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = "BigNumber-iOSTests/Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 11.2; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOSTests"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -628,7 +628,7 @@ DEVELOPMENT_TEAM = ""; GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = "BigNumber-iOSTests/Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 11.2; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOSTests"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -663,6 +663,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -720,6 +721,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; diff --git a/Tests/Test_Initialization.swift b/Tests/Test_Initialization.swift index 374a473..0d5e85d 100644 --- a/Tests/Test_Initialization.swift +++ b/Tests/Test_Initialization.swift @@ -34,6 +34,22 @@ class Test_Initialization: XCTestCase { XCTAssert(b.rawValue.limbs == [0, 0, 0, 0, 1]) } + func testBytes() + { + // Bytes and expected number + // 0x0102030405 is 4328719365 in decimal + let array: Bytes = [0x01, 0x02, 0x03, 0x04, 0x05] + let expected: Int = 4328719365 + + // Init from bytes (array) + let b = BInt(bytes: array) + XCTAssertEqual(b.description, expected.description) + + // Convert back to bytes + let bytes = b.getBytes() + XCTAssertEqual(bytes, array) + } + func testCodable() { for i in 0..<50 { let one = BInt(i)