diff --git a/BinaryConverter.xcodeproj/project.pbxproj b/BinaryConverter.xcodeproj/project.pbxproj index 7807a68..6dec7d8 100644 --- a/BinaryConverter.xcodeproj/project.pbxproj +++ b/BinaryConverter.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 2C53DAA62AF866B200EB233F /* AnyBaseConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C53DAA52AF866B200EB233F /* AnyBaseConverter.swift */; }; 2CCE3E9F2AF678B200BB0ED3 /* BinaryConverterApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CCE3E9E2AF678B200BB0ED3 /* BinaryConverterApp.swift */; }; 2CCE3EA12AF678B200BB0ED3 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CCE3EA02AF678B200BB0ED3 /* ContentView.swift */; }; 2CCE3EA32AF678B300BB0ED3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2CCE3EA22AF678B300BB0ED3 /* Assets.xcassets */; }; @@ -14,10 +15,10 @@ 2CCE3EAD2AF67A9000BB0ED3 /* UtilsFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CCE3EAC2AF67A9000BB0ED3 /* UtilsFunctions.swift */; }; 2CCE3EAF2AF67BA900BB0ED3 /* BinaryConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CCE3EAE2AF67BA900BB0ED3 /* BinaryConverter.swift */; }; 2CCE3EB12AF67CFD00BB0ED3 /* HexaConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CCE3EB02AF67CFD00BB0ED3 /* HexaConverter.swift */; }; - 2CCE3EB52AF6879200BB0ED3 /* OctalConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CCE3EB42AF6879200BB0ED3 /* OctalConverter.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 2C53DAA52AF866B200EB233F /* AnyBaseConverter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyBaseConverter.swift; sourceTree = ""; }; 2CCE3E9B2AF678B200BB0ED3 /* BinaryConverter.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BinaryConverter.app; sourceTree = BUILT_PRODUCTS_DIR; }; 2CCE3E9E2AF678B200BB0ED3 /* BinaryConverterApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryConverterApp.swift; sourceTree = ""; }; 2CCE3EA02AF678B200BB0ED3 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -26,7 +27,6 @@ 2CCE3EAC2AF67A9000BB0ED3 /* UtilsFunctions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilsFunctions.swift; sourceTree = ""; }; 2CCE3EAE2AF67BA900BB0ED3 /* BinaryConverter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryConverter.swift; sourceTree = ""; }; 2CCE3EB02AF67CFD00BB0ED3 /* HexaConverter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HexaConverter.swift; sourceTree = ""; }; - 2CCE3EB42AF6879200BB0ED3 /* OctalConverter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OctalConverter.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -61,9 +61,9 @@ children = ( 2CCE3E9E2AF678B200BB0ED3 /* BinaryConverterApp.swift */, 2CCE3EA02AF678B200BB0ED3 /* ContentView.swift */, - 2CCE3EB42AF6879200BB0ED3 /* OctalConverter.swift */, 2CCE3EB02AF67CFD00BB0ED3 /* HexaConverter.swift */, 2CCE3EAE2AF67BA900BB0ED3 /* BinaryConverter.swift */, + 2C53DAA52AF866B200EB233F /* AnyBaseConverter.swift */, 2CCE3EAC2AF67A9000BB0ED3 /* UtilsFunctions.swift */, 2CCE3EA22AF678B300BB0ED3 /* Assets.xcassets */, 2CCE3EA42AF678B300BB0ED3 /* Preview Content */, @@ -151,9 +151,9 @@ files = ( 2CCE3EA12AF678B200BB0ED3 /* ContentView.swift in Sources */, 2CCE3EAF2AF67BA900BB0ED3 /* BinaryConverter.swift in Sources */, + 2C53DAA62AF866B200EB233F /* AnyBaseConverter.swift in Sources */, 2CCE3EAD2AF67A9000BB0ED3 /* UtilsFunctions.swift in Sources */, 2CCE3EB12AF67CFD00BB0ED3 /* HexaConverter.swift in Sources */, - 2CCE3EB52AF6879200BB0ED3 /* OctalConverter.swift in Sources */, 2CCE3E9F2AF678B200BB0ED3 /* BinaryConverterApp.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/BinaryConverter/AnyBaseConverter.swift b/BinaryConverter/AnyBaseConverter.swift new file mode 100644 index 0000000..2a7a9a4 --- /dev/null +++ b/BinaryConverter/AnyBaseConverter.swift @@ -0,0 +1,48 @@ +// +// AnyBaseConverter.swift +// BinaryConverter +// +// Created by Louis Gallet on 06/11/2023. +// + +import SwiftUI + +struct AnyBaseConverter: View { + @State private var sourceBasedNumber: String = "" + @State private var basedNumber: String = "" + @State private var destinationBasedNumber: String = "" + @State private var resultConversion: String = "" + var body: some View { + Form { + Section(header: Text("Source base")) { + TextField("Enter the source base for the number", text: $sourceBasedNumber) + .keyboardType(.numberPad) + } + Section(header: Text("Number to convert")) { + TextField("Enter the number that you want to convert", + text: $basedNumber) + .keyboardType(.numberPad) + } + Section(header: Text("Destination base")) { + TextField("Enter the arrival base", + text: $destinationBasedNumber) + .keyboardType(.numberPad) + } + Section(header: Text("Result")) { + Text("Your number in base \(destinationBasedNumber): \(resultConversion)") + } + Button("Dismiss Keyboard") { + UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) + } + } + .onChange(of: $basedNumber, $sourceBasedNumber) { newValue in resultConversion = convertFromAnyBaseToAnyBase(basedNumber, fromBase: Int(sourceBasedNumber)!, toBase: Int(destinationBasedNumber))!} +// .onChange(of: $sourceBasedNumber) { newValue in +// $destinationBasedNumber = convertToHex(newValue) +// } + + } +} + +#Preview { + AnyBaseConverter() +} diff --git a/BinaryConverter/OctalConverter.swift b/BinaryConverter/OctalConverter.swift deleted file mode 100644 index 589d006..0000000 --- a/BinaryConverter/OctalConverter.swift +++ /dev/null @@ -1,18 +0,0 @@ -// -// OctalConverter.swift -// BinaryConverter -// -// Created by Louis Gallet on 04/11/2023. -// - -import SwiftUI - -struct OctalConverter: View { - var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) - } -} - -#Preview { - OctalConverter() -} diff --git a/BinaryConverter/UtilsFunctions.swift b/BinaryConverter/UtilsFunctions.swift index 3ecc3f8..fab95f1 100644 --- a/BinaryConverter/UtilsFunctions.swift +++ b/BinaryConverter/UtilsFunctions.swift @@ -29,3 +29,17 @@ func convertToHex(_ decimalString: String) -> String { return "Invalid Input" } } + +/// Converts a number from the source base to the destination base +/// - Parameters: +/// - decimalString: The number that you want to convert +/// - fromBase: The base of the number that you want to convert +/// - toBase: The destination base +/// - Returns: Return the number in toBase base or return "Invalid Input" if the input is not valid +func convertFromAnyBaseToAnyBase(_ decimalString: String, fromBase: Int, toBase: Int) -> String { + if let number = Int(decimalString, radix: fromBase) { + return String(number, radix: toBase) + } else { + return "Invalid Input" + } +}