I am taking in a 60 character user input, all hex characters, and breaking it down into segments of 4, therefore giving me 15 segments of 4, aka 15 'words'. Then I take one of these words, convert it into a binary number of 16 characters. These 16 characters each map to their own specific error code, if its a 0, no error, if its a 1, error. I am trying to figure out if there is a way to take each character of that binary string and map it to its own specific error code like such.
var error_str = "Not used:\(decNumb_str[0]) \nNot used:\(decNumb_str[1])"......
Therefore giving me 16 error messages per word.
I have already looked at:
Get nth character of a string in Swift programming language
However, I do not feel that the code shown in the link will assist me when I am attempting to complete this action as such:
let wordOne = UIAlertAction(title: "Word 1", style: .default)
{
(alert) -> Void in
var str_start = 0
var str_end = 56
if self.textView.text == "" || self.textView.text == "No input value"
{
self.textView.text = "No input value"
}
else
{
let start_index = orig_hex_str.index(orig_hex_str.startIndex, offsetBy: str_start)
let end_index = orig_hex_str.index(orig_hex_str.endIndex, offsetBy: -str_end)
let sub_str = orig_hex_str[start_index..<end_index]
let hexNumb_int = Int(sub_str, radix:16)
let decNumb_str = String(hexNumb_int!, radix:2)
let padded_str = String(self.pad(string: decNumb_str, toSize: 16))
//Attempting to put error statement here
//var error_str = "Not used:\(padded_str?.characters[0]) \nNot used:\(padded_str?.characters[1]) \nNot used:\(padded_str?.characters[2])"
self.textView.text = padded_str
}
}
wordSelection.addAction(wordOne)
Here are images of user sample input and the sample output if the user was to select word 1, aka the first 4 digits of the user's input:
Here is a minimial but compilable sample that gets the first 4 characters of the users input and converts it into binary. Before I output it, I would like to take each character of that binary and pass it to its own error code:
import UIKit
import Foundation
var initial_str = "0123456789abcdef"
// WORD 1
func pad(string : String, toSize: Int) -> String
{
var padded = string
for _ in 0..<toSize - string.characters.count
{
padded = "0" + padded
}
return padded
}
let str_start = 0
let str_end = 12
let start_index = initial_str.index(initial_str.startIndex, offsetBy: str_start)
let end_index = initial_str.index(initial_str.endIndex, offsetBy: -str_end)
let sub_str = initial_str [start_index..<end_index]
let hexNumb_int = Int(sub_str, radix:16)
let decNumb_str = String(hexNumb_int!, radix:2)
let padded_str = pad(string: decNumb_str, toSize: 16)
//The commented segments below are what breaks the code above. I would like to put a string that
//Accesses each character of the binary and puts a message for each string
//let error_str = "Not used:\(padded_str.characters[0]) \nNot used:\(padded_str.characters[1]) \nNot used:\(padded_str.characters[2])"
//print(error_str)
print(padded_str)
Thank you in advance for any assistance