From 876286c9f931dc8627029e4e8bcaebbbafd09ce5 Mon Sep 17 00:00:00 2001 From: Jayant Sogikar Date: Thu, 1 Oct 2020 01:11:37 +0530 Subject: [PATCH 1/7] Create README.md --- StringAndIntOps/README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 StringAndIntOps/README.md diff --git a/StringAndIntOps/README.md b/StringAndIntOps/README.md new file mode 100644 index 000000000..54078f7ca --- /dev/null +++ b/StringAndIntOps/README.md @@ -0,0 +1,39 @@ +# String And Int Operations +The importance of various number like binary numbers, hexadecimals, ASCII values are well known. + +Hence, this file aims to simply and aid in the process of conversion to required result within a tap of few buttons by the help of extensions. + +## Binary Numbers +A base-2 numerical system where an integer is defined using only 1's and 0's +Example : 13 in binary is 1101 + +## Hexadecimal Numbers +A system of representation of numbers where the radix present is 16 i.e. the numbers used in this system range from 0...9 & A,B,C,D,E,F. +Example : 10 in hexadecimal is a + +## ASCII +A method adapted that represents characters as numbers +Example : 'a'(lowercase) in terms of ASCII code is 97 + +## Implementation +```swift +let value = "10".intToHex // returns "10" as Hexadecimal +print(value) // prints "a" +``` + +```swift +let value = "A".ascii // returns "A" as Ascii value +print(value) // prints 65 +``` + +```swift +let value = 13.binString // returns 13 as Binary String +print(value) // prints "1101" +``` + +```swift +let value = 97.intToAscii // returns 97 as a character taking in the integer as ASCII value +print(value) // prints "a" +``` + +*Written for Swift Algorithm Club by Jayant Sogikar* From 40c8e1c7ea4407510827e5ec3596546c5240f504 Mon Sep 17 00:00:00 2001 From: Jayant Sogikar Date: Thu, 1 Oct 2020 01:12:31 +0530 Subject: [PATCH 2/7] Added the String and Int Operation file --- StringAndIntOps/String<->Int Operations.swift | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 StringAndIntOps/String<->Int Operations.swift diff --git a/StringAndIntOps/String<->Int Operations.swift b/StringAndIntOps/String<->Int Operations.swift new file mode 100644 index 000000000..06a63ef11 --- /dev/null +++ b/StringAndIntOps/String<->Int Operations.swift @@ -0,0 +1,31 @@ +import UIKit +extension String { + // a -> 10 + var hexToInt : Int{return Int(strtoul(self, nil, 16))} + // a -> 10.0 + var hexToDouble : Double{return Double(strtoul(self, nil, 16))} + // a -> 1010 + var hexToBin : String{return String(hexToInt, radix: 2)} + // 10 -> a + var intToHex : String{ return String(Int(self) ?? 0, radix: 16)} + // 10 -> 1010 + var intToBin : String{return String(Int(self) ?? 0, radix: 2)} + // 1010 -> 10 + var binToInt : Int{return Int(strtoul(self, nil, 2))} + // 1010 -> 10.0 + var binToDouble : Double{return Double(strtoul(self, nil, 2))} + // 1010 -> a + var binToHexa : String{return String(binToInt, radix: 16)} + // a -> 97 + var ascii : Int{return Int(self.unicodeScalars.first!.value)} +} +extension Int { + // 10 -> 1010 + var binString : String{return String(self, radix: 2)} + // 10 -> a + var hexString : String{return String(self, radix: 16)} + // 10 -> 10.0 + var double : Double{ return Double(self)} + // 97 -> a + var intToAscii : String{return String(Character(UnicodeScalar(self)!))} +} From 15b6432c64005674da66919d7ae74350c572d864 Mon Sep 17 00:00:00 2001 From: Jayant Sogikar Date: Thu, 1 Oct 2020 01:18:19 +0530 Subject: [PATCH 3/7] Create README.md --- String And Int Operation/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 String And Int Operation/README.md diff --git a/String And Int Operation/README.md b/String And Int Operation/README.md new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/String And Int Operation/README.md @@ -0,0 +1 @@ + From 1f2acc4f79a10026b5c3220c6b50f8d8a68b04e2 Mon Sep 17 00:00:00 2001 From: Jayant Sogikar Date: Thu, 1 Oct 2020 01:19:19 +0530 Subject: [PATCH 4/7] Update README.md --- String And Int Operation/README.md | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/String And Int Operation/README.md b/String And Int Operation/README.md index 8b1378917..54078f7ca 100644 --- a/String And Int Operation/README.md +++ b/String And Int Operation/README.md @@ -1 +1,39 @@ +# String And Int Operations +The importance of various number like binary numbers, hexadecimals, ASCII values are well known. +Hence, this file aims to simply and aid in the process of conversion to required result within a tap of few buttons by the help of extensions. + +## Binary Numbers +A base-2 numerical system where an integer is defined using only 1's and 0's +Example : 13 in binary is 1101 + +## Hexadecimal Numbers +A system of representation of numbers where the radix present is 16 i.e. the numbers used in this system range from 0...9 & A,B,C,D,E,F. +Example : 10 in hexadecimal is a + +## ASCII +A method adapted that represents characters as numbers +Example : 'a'(lowercase) in terms of ASCII code is 97 + +## Implementation +```swift +let value = "10".intToHex // returns "10" as Hexadecimal +print(value) // prints "a" +``` + +```swift +let value = "A".ascii // returns "A" as Ascii value +print(value) // prints 65 +``` + +```swift +let value = 13.binString // returns 13 as Binary String +print(value) // prints "1101" +``` + +```swift +let value = 97.intToAscii // returns 97 as a character taking in the integer as ASCII value +print(value) // prints "a" +``` + +*Written for Swift Algorithm Club by Jayant Sogikar* From 722a7993ca6ca4a32a3cdbfb5944a80c33143e81 Mon Sep 17 00:00:00 2001 From: Jayant Sogikar Date: Thu, 1 Oct 2020 01:20:37 +0530 Subject: [PATCH 5/7] Delete String<->Int Operations.swift --- StringAndIntOps/String<->Int Operations.swift | 31 ------------------- 1 file changed, 31 deletions(-) delete mode 100644 StringAndIntOps/String<->Int Operations.swift diff --git a/StringAndIntOps/String<->Int Operations.swift b/StringAndIntOps/String<->Int Operations.swift deleted file mode 100644 index 06a63ef11..000000000 --- a/StringAndIntOps/String<->Int Operations.swift +++ /dev/null @@ -1,31 +0,0 @@ -import UIKit -extension String { - // a -> 10 - var hexToInt : Int{return Int(strtoul(self, nil, 16))} - // a -> 10.0 - var hexToDouble : Double{return Double(strtoul(self, nil, 16))} - // a -> 1010 - var hexToBin : String{return String(hexToInt, radix: 2)} - // 10 -> a - var intToHex : String{ return String(Int(self) ?? 0, radix: 16)} - // 10 -> 1010 - var intToBin : String{return String(Int(self) ?? 0, radix: 2)} - // 1010 -> 10 - var binToInt : Int{return Int(strtoul(self, nil, 2))} - // 1010 -> 10.0 - var binToDouble : Double{return Double(strtoul(self, nil, 2))} - // 1010 -> a - var binToHexa : String{return String(binToInt, radix: 16)} - // a -> 97 - var ascii : Int{return Int(self.unicodeScalars.first!.value)} -} -extension Int { - // 10 -> 1010 - var binString : String{return String(self, radix: 2)} - // 10 -> a - var hexString : String{return String(self, radix: 16)} - // 10 -> 10.0 - var double : Double{ return Double(self)} - // 97 -> a - var intToAscii : String{return String(Character(UnicodeScalar(self)!))} -} From 1e9285f0493250f0b26f94c104c98fc40247fcd6 Mon Sep 17 00:00:00 2001 From: Jayant Sogikar Date: Thu, 1 Oct 2020 01:20:48 +0530 Subject: [PATCH 6/7] Delete README.md --- StringAndIntOps/README.md | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 StringAndIntOps/README.md diff --git a/StringAndIntOps/README.md b/StringAndIntOps/README.md deleted file mode 100644 index 54078f7ca..000000000 --- a/StringAndIntOps/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# String And Int Operations -The importance of various number like binary numbers, hexadecimals, ASCII values are well known. - -Hence, this file aims to simply and aid in the process of conversion to required result within a tap of few buttons by the help of extensions. - -## Binary Numbers -A base-2 numerical system where an integer is defined using only 1's and 0's -Example : 13 in binary is 1101 - -## Hexadecimal Numbers -A system of representation of numbers where the radix present is 16 i.e. the numbers used in this system range from 0...9 & A,B,C,D,E,F. -Example : 10 in hexadecimal is a - -## ASCII -A method adapted that represents characters as numbers -Example : 'a'(lowercase) in terms of ASCII code is 97 - -## Implementation -```swift -let value = "10".intToHex // returns "10" as Hexadecimal -print(value) // prints "a" -``` - -```swift -let value = "A".ascii // returns "A" as Ascii value -print(value) // prints 65 -``` - -```swift -let value = 13.binString // returns 13 as Binary String -print(value) // prints "1101" -``` - -```swift -let value = 97.intToAscii // returns 97 as a character taking in the integer as ASCII value -print(value) // prints "a" -``` - -*Written for Swift Algorithm Club by Jayant Sogikar* From ebf17444d440e3bcfb001568a83eb943d0cfa1d5 Mon Sep 17 00:00:00 2001 From: Jayant Sogikar Date: Thu, 1 Oct 2020 01:21:41 +0530 Subject: [PATCH 7/7] Added the String And File in a new Renamed Folder --- .../String<->Int Operations.swift | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 String And Int Operation/String<->Int Operations.swift diff --git a/String And Int Operation/String<->Int Operations.swift b/String And Int Operation/String<->Int Operations.swift new file mode 100644 index 000000000..06a63ef11 --- /dev/null +++ b/String And Int Operation/String<->Int Operations.swift @@ -0,0 +1,31 @@ +import UIKit +extension String { + // a -> 10 + var hexToInt : Int{return Int(strtoul(self, nil, 16))} + // a -> 10.0 + var hexToDouble : Double{return Double(strtoul(self, nil, 16))} + // a -> 1010 + var hexToBin : String{return String(hexToInt, radix: 2)} + // 10 -> a + var intToHex : String{ return String(Int(self) ?? 0, radix: 16)} + // 10 -> 1010 + var intToBin : String{return String(Int(self) ?? 0, radix: 2)} + // 1010 -> 10 + var binToInt : Int{return Int(strtoul(self, nil, 2))} + // 1010 -> 10.0 + var binToDouble : Double{return Double(strtoul(self, nil, 2))} + // 1010 -> a + var binToHexa : String{return String(binToInt, radix: 16)} + // a -> 97 + var ascii : Int{return Int(self.unicodeScalars.first!.value)} +} +extension Int { + // 10 -> 1010 + var binString : String{return String(self, radix: 2)} + // 10 -> a + var hexString : String{return String(self, radix: 16)} + // 10 -> 10.0 + var double : Double{ return Double(self)} + // 97 -> a + var intToAscii : String{return String(Character(UnicodeScalar(self)!))} +}