feat: 🚧 Starting conversion from any base to another

This commit is contained in:
Louis Gallet 2023-11-06 01:23:21 +01:00
parent 03e0d03e9f
commit a6d602b726
Signed by: lgallet
SSH Key Fingerprint: SHA256:qnW7pk4EoMRR0UftZLZQKSMUImbEFsiruLC7jbCHJAY
4 changed files with 66 additions and 22 deletions

View File

@ -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 = "<group>"; };
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 = "<group>"; };
2CCE3EA02AF678B200BB0ED3 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@ -26,7 +27,6 @@
2CCE3EAC2AF67A9000BB0ED3 /* UtilsFunctions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilsFunctions.swift; sourceTree = "<group>"; };
2CCE3EAE2AF67BA900BB0ED3 /* BinaryConverter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryConverter.swift; sourceTree = "<group>"; };
2CCE3EB02AF67CFD00BB0ED3 /* HexaConverter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HexaConverter.swift; sourceTree = "<group>"; };
2CCE3EB42AF6879200BB0ED3 /* OctalConverter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OctalConverter.swift; sourceTree = "<group>"; };
/* 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;

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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"
}
}