Skip to content

Commit 0ec7214

Browse files
author
Nathan Fallet
authored
Init a BInt from a [UInt8] (#56)
* Updating project settings * Init a `BInt` from a `[UInt8]` * Get bytes `[UInt8]` from `BInt` * Adding `Byte` and `Bytes` type aliases
1 parent 367471a commit 0ec7214

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

Sources/Swift-Big-Number-Core.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public typealias Limb = UInt64
121121
public typealias Digits = [UInt64]
122122
public typealias Digit = UInt64
123123

124+
// Bytes allow to initialize and export BInt for operations like network related ones.
125+
126+
public typealias Bytes = [UInt8]
127+
public typealias Byte = UInt8
128+
124129
// MARK: - Imports
125130
// ————————————————————————————————————————————————————————————————————————————————————————————
126131
// |||||||| Operators |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@@ -432,6 +437,19 @@ public struct BInt:
432437
{
433438
self.init(source)
434439
}
440+
441+
/// Creates a new instance from a `[UInt8]` array
442+
public init(bytes: Bytes)
443+
{
444+
var num = BInt()
445+
446+
for byte in bytes
447+
{
448+
num = num << 8 | BInt(byte)
449+
}
450+
451+
self.init(sign: num.sign, limbs: num.limbs)
452+
}
435453

436454
//
437455
//
@@ -551,6 +569,20 @@ public struct BInt:
551569
i += 1
552570
}
553571
}
572+
573+
/// Bytes of the number
574+
public func getBytes() -> Bytes
575+
{
576+
var bytes = Bytes()
577+
var copy = self
578+
579+
while copy != 0 {
580+
bytes.append(Byte(copy & 0xff))
581+
copy >>= 8
582+
}
583+
584+
return bytes.reversed()
585+
}
554586

555587
//
556588
//

Swift-BigNumber.xcodeproj/project.pbxproj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@
364364
isa = PBXProject;
365365
attributes = {
366366
LastSwiftUpdateCheck = 0920;
367-
LastUpgradeCheck = 0940;
367+
LastUpgradeCheck = 1250;
368368
ORGANIZATIONNAME = "Marcel Kröker";
369369
TargetAttributes = {
370370
AE8388A5203F1CAF00FAC88F = {
@@ -546,7 +546,7 @@
546546
GCC_C_LANGUAGE_STANDARD = gnu11;
547547
INFOPLIST_FILE = "BigNumber-iOS/Info.plist";
548548
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
549-
IPHONEOS_DEPLOYMENT_TARGET = 11.3;
549+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
550550
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
551551
PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOS";
552552
PRODUCT_NAME = BigNumber;
@@ -577,7 +577,7 @@
577577
GCC_C_LANGUAGE_STANDARD = gnu11;
578578
INFOPLIST_FILE = "BigNumber-iOS/Info.plist";
579579
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
580-
IPHONEOS_DEPLOYMENT_TARGET = 11.3;
580+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
581581
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
582582
PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOS";
583583
PRODUCT_NAME = BigNumber;
@@ -604,7 +604,7 @@
604604
DEVELOPMENT_TEAM = "";
605605
GCC_C_LANGUAGE_STANDARD = gnu11;
606606
INFOPLIST_FILE = "BigNumber-iOSTests/Info.plist";
607-
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
607+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
608608
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
609609
PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOSTests";
610610
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -628,7 +628,7 @@
628628
DEVELOPMENT_TEAM = "";
629629
GCC_C_LANGUAGE_STANDARD = gnu11;
630630
INFOPLIST_FILE = "BigNumber-iOSTests/Info.plist";
631-
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
631+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
632632
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
633633
PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOSTests";
634634
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -663,6 +663,7 @@
663663
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
664664
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
665665
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
666+
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
666667
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
667668
CLANG_WARN_STRICT_PROTOTYPES = YES;
668669
CLANG_WARN_SUSPICIOUS_MOVE = YES;
@@ -720,6 +721,7 @@
720721
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
721722
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
722723
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
724+
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
723725
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
724726
CLANG_WARN_STRICT_PROTOTYPES = YES;
725727
CLANG_WARN_SUSPICIOUS_MOVE = YES;

Tests/Test_Initialization.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ class Test_Initialization: XCTestCase {
3434
XCTAssert(b.rawValue.limbs == [0, 0, 0, 0, 1])
3535
}
3636

37+
func testBytes()
38+
{
39+
// Bytes and expected number
40+
// 0x0102030405 is 4328719365 in decimal
41+
let array: Bytes = [0x01, 0x02, 0x03, 0x04, 0x05]
42+
let expected: Int = 4328719365
43+
44+
// Init from bytes (array)
45+
let b = BInt(bytes: array)
46+
XCTAssertEqual(b.description, expected.description)
47+
48+
// Convert back to bytes
49+
let bytes = b.getBytes()
50+
XCTAssertEqual(bytes, array)
51+
}
52+
3753
func testCodable() {
3854
for i in 0..<50 {
3955
let one = BInt(i)

0 commit comments

Comments
 (0)