How to convert byte array to string in swift 3 - arrays

I referred C# code for byte array to string conversion in swift
System.Text.Encoding.UTF8.GetString(encryptedpassword)
I tried to convert byte array to string by using below few codes:
(A)
let utf8 : [UInt8] = [231, 13, 38, 246, 234, 144, 148, 111, 174, 136, 15, 61, 200, 186, 215, 113,0]
let str = NSString(bytes: utf8, length: utf8.count, encoding: String.Encoding.utf8.rawValue)
print("str : \(str)")
result : getting nil value
(B)
let datastring = NSString(bytes: chars, length: count, encoding: String.Encoding.utf8.rawValue)
print("string byte data\(chars.map{"\($0)"}.reduce(""){$0+$1})")
result : 23113382462341441481111741361561200186215113 (I thought this is not ideal way)
I searched from last two days and tried other multiple ways but I missed something or doing some mistake .
Please help me out resolved this issue.
Referred links:
how-to-convert-uint8-byte-array-to-string-in-swift
how-to-convert-uint8-byte-array-to-string-in-swift 2

Related

Converting image identified by PyTesseract to an array

I have an image with a list of numbers which I have scanned using PyTesseract to construct a string. Concretely, here is the code:
from PIL import Image
import pytesseract
from scipy import stats
import numpy as np
pytesseract.pytesseract.tesseract_cmd = r'C:\\\Program Files\\\Tesseract-OCR\\\tesseract.exe'
str1=pytesseract.image_to_string(Image.open('D:/Image.png'))
Here's the image I am scanning:
The problem is that PyTesseract is scanning the image as individual characters instead of integers.
I would like to understand why this is happening and what can I do to get the desired result.
In short, PyTesseract is not scanning integers in a list of numbers, instead scanning them as individual characters. How do I tell it to scan for integers and put them in an array?
Well,If you only want to get a list,Use re.split and strip can solve it.(Because tesseract's result has some errors).
You can try this:
import pytesseract
import re
data = pytesseract.image_to_string('OCR.png')
dataList = re.split(r',|\.| ',data) # split the string
resultList = [int(i.strip()) for i in dataList if i != ''] # remove the '' str and convert str to int.
print(resultList)
# result: [71, 194, 38, 1701, 89, 76, 11, 83, 1629, 48, 94, 63, 132, 16, 111, 95, 84, 341, 975, 14, 40, 64, .......

Breaking Array Values as Characters

I have an array with ascii characters values like this
arryAsc[] = [97, 100, 97, 115, 100]
Now I want to make an array arrSplit[] and want to save these values in array like this.
arrSplit[] = [9, 7, 1, 0, 0, 9, 7, 1, 1, 5, 1, 0, 0]
If anyone can help thanks in advance.
You can simply do like this :
var a:Array = [97, 100, 97, 115, 100];
var b:Array = a.join('').split('');
trace(b); // gives : 9,7,1,0,0,9,7,1,1,5,1,0,0
Hope that can help.
Loop through the array, casting each element as a String, then use split() with an empty string as delimiter to break apart each string into an array of characters. You can use concat to combine all the smaller arrays back into your larger arrSplit array.

String Input in Java

I was trying to take input many strings in separate lines and want to store all of them for use later.For example want to take input as follows(last line ends with a ".")-
My name is ABCD
My name is BCDS
My name is fdada.
How can I implement this?? Also I want to use all these strings.In java or any other language I would have made a string array and used that array to access all the three strings.
But the moment I enter 1st line it gives me false.
you can use a failure driven loop, like
:- dynamic a_line/1.
read_lines :-
retractall(a_line(__)),
repeat,
read_line_to_codes(user_input, L),
assertz(a_line(L)),
( last(L, 0'.) ; fail ).
and then
?- read_lines.
|: My name is ABCD
|: My name is BCDS
|: My name is fdada.
true .
the result get stored in a_line/1, so
?- a_line(L),atom_codes(A,L).
L = [77, 121, 32, 110, 97, 109, 101, 32, 105|...],
A = 'My name is ABCD'
L = [32, 77, 121, 32, 110, 97, 109, 101, 32|...],
A = ' My name is BCDS'.
...

Swift Array optional Type and subscripting (Beta 3)

I'm following the 2014 WWDC tutorial 408: Swift Playgrounds using XCode Beta 3 (30 minutes in). The Swift syntax has changed since Beta 2.
var data = [27, 46, 96, 79, 56, 85, 45, 34, 2, 57, 29, 66, 99, 65, 66, 40, 40, 58, 87, 64]
func exchange<T>(data: [T], i: Int, j: Int) {
let temp = data[i]
data[i] = data[j] // Fails with error '#lvalue $T8' is not identical to 'T'
data[j] = temp // Fails with error '#lvalue $T5' is not identical to 'T'
}
exchange(data, 0 , 2)
data
Why I can't modify a mutable integer array in this way?
Because subroutine parameters are implicitly defined with let hence, non mutable. Try changing the declaration to:
func exchange<T>(inout data: [T], i: Int, j: Int) {
and the invocation to:
exchange(&date, 0, 2)
You can also use var but that would only allow the array to be modified within the subroutine. The big change for beta 3 was to make arrays really pass by value instead of just kind of sorta pass by value some of the time, but not the rest.
#David answer is correct, let me explain why: arrays (as well as dictionaries and strings) are value types (structs) and not reference types. When a value type has to be passed to a function, a copy of it is created, and the function works on that copy.
By using the inout modifier, the original array is passed instead, so in that case it's possible to make changes on it.

Parsing JSON array from Tuple

So I'm trying to follow the Pebble SDK over here alongside the httpebble library.
Assume DictionaryIterator* received contains the following JSON payload:
{"0":["Oz Lotto","Powerball","TattsLotto","Super 66","Monday & Wednesday Lotto","The Pools"],"1":["17, 26, 38, 7, 36, 45, 9 (44) (41)","15, 37, 32, 6, 24, 10 (11)","37, 12, 15, 16, 42, 45 (6) (9)","1, 8, 5, 8, 8, 5","16, 40, 44, 8, 24, 15 (39) (3)","5, 17, 20, 22, 27, 31 (16)"]}
main.c:
I want to be able to extract the data to later store them into a list in my Pebble app. I'm having a bit of trouble grasping how a Tuple works though.
This is what I have so far, except I'm afraid I'm not sure how to actually extract values out of lotto_names and lotto_numbers.
void http_success(int32_t request_id, int http_status, DictionaryIterator* received, void* context) {
if (request_id != HTTP_COOKIE) {
return;
}
Tuple *lotto_names = dict_find(received, 0);
Tuple *lotto_numbers = dict_find(received, 1);
for (int i = 0; i < 5; i++) {
const char* lotto_name = lotto_names[i]; // ?
}
}
I've also asked the question over at the Pebble SDK forum. However noone has responded to my post. Any help would be appreciated.
In your example, the key "0" is associated to an array which is a form of nesting. As explained in httpebble documentation, there cannot be any nesting in the dictionary you return to `httpebble.
The key/value pairs attached to the request are sent as a flat JSON dictionary. There cannot be any nesting.
You need to rework your JSON data to send keys associated directly with values.

Resources