-1

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:

enter image description here enter image description here

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

Community
  • 1
  • 1
Anavas
  • 55
  • 1
  • 8
  • 1
    "But this will not work": please explain. – Scott Hunter Dec 10 '16 at 01:12
  • 1
    I'm not understanding what you're trying to do. Can you provide a workspace snippet that shows your input, and what you hope your output would be? – Travis Griggs Dec 10 '16 at 01:20
  • The user input will be 60 characters all recognizable as hexadecimal values. My code will then take 4 characters at a time starting at the beginning, convert it into binary then output the binary by character. The first word (i.e. the first 4 characters of the users input) Are to be recognized has a hex value, converted to binary, and then that 16 digit binary code will have an error message tied in with each character. If the output is a 0, there is no error, if the output is a 1, there is an error, etc – Anavas Dec 10 '16 at 04:14
  • @ScottHunter It will not work because Swift does not have any built in way to recognize what that code is, like C++ does. I am trying to find a work around to print out each character and have each character tied in with a string. I get this error message when attempting: 'subscript' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion – Anavas Dec 10 '16 at 04:16
  • Could you post a [minimal but compilable sample](http://stackoverflow.com/help/mcve), an example input string and the expected output. – shallowThought Dec 10 '16 at 20:39
  • @shallowThought Is this a little bit clearer? I am getting a new error when attempting to do what you had shown me: Cannot subscript a value of type 'String.CharacterView' with an index of type 'Int'. Am I just failing to convert it into a string? Yes, I can post a compilable sample – Anavas Dec 10 '16 at 20:40
  • That is my mistake. sorry. My comment was wrong. Take your time to post some code we can past to playground and that is showing the error. – shallowThought Dec 10 '16 at 20:46
  • @shallowThought I added some code that works in playground to get the first 4 characters of the users input and converts it to binary. I added comments of code that break it if it were to be implemented. – Anavas Dec 10 '16 at 21:21

1 Answers1

1

It is not clear enough what you actually want to do, but you can get every character of a string applying map to characters property, or using for-in.

With map:

let error_str = padded_str.characters.map {"Not used:\($0)"}.joined(separator: " \n")
print(error_str)
/*↓
Not used:0
Not used:0
Not used:0
Not used:0
Not used:0
Not used:0
Not used:0
Not used:1
Not used:0
Not used:0
Not used:1
Not used:0
Not used:0
Not used:0
Not used:1
Not used:1
 */

Or the following for-in example is nearly equivalent:

var error_str = ""
for ch in padded_str.characters {
    if !error_str.isEmpty {
        error_str.append(" \n")
    }
    error_str.append("Not used:\(ch)")
}
print(error_str)
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • The first example is was looking for. I just have to figure out how to write a custom error message for the statement. For example the first 3 zero's are not used, the the next one is left leg broken, the one after that right wing fractured etc. I apologize for not being clear enough, I will attempt to to do so much better in the future. – Anavas Dec 11 '16 at 04:14