Faster way of converting image hex dump to binary array in Swift - arrays

I need to edit the pixels' raw binary values of an image. For this, I did this steps:
I obtained the CFData containing the hex dump of the image.
I converted the CFData obtained to an array of characters (using convertToArray function)
After that, I used convertToBinaryString function to obtain a string representing the base 2 value of the hex dump.
It does work and does the job for small files but when it comes to bigger ones it takes forever to finish. I failed in the struggle of finding a faster way. Could you help me?
Down here you can take a look of the functions I need to optimize:
func convertToArray(imageData : CFData) -> Array<Character>{
let arrayData : Array<Character> = Array(String(NSData(data: imageData)).characters)
print("Array : ")
Swift.print(arrayData)
return arrayData
}
func convertToBinaryString(array : Array<Character>) -> String{
let numberOfChars = array.count
var binaryString = convertHexToBinary(array[1])
for character in 2...numberOfChars - 2{
binaryString = binaryString + convertHexToBinary(array[character])
}
// print("BINARRY ARRAY : ")
// print(binaryString)
return binaryString
}

I would try this extension to NSData that provides a CollectionType interface. It seems like it would make it much easier for you to do your modifications without having to much around with UnsafeMutablePointer:
Bytes collection for NSData
Since BytesView and MutableBytesView are a CollectionType it gives you subscripting, indices, and more so you can iterate through your data and make the changes you want.
The article which is relevant to this question and introduces that linked code:
NSData, My Old Friend

Related

Beginner Question: Func return String and Array

I'm experimenting manipulating basic Swift data types and tried to add a (basic) layer of complexity to a simple function examples I was practising from Swifts Documentation. I was able to return as a simple array but when I tried to return an array within a String I got errors. I've read all the Swift Documentation for Arrays to try and solve this first with no luck.
Here is my code for the successful Array return:
func namesList(person: String) -> [(String)] {
let register = ["RoboCop", person, "Terminator"]
return register.sorted()
}
... and my unsuccessful code:
func namesList(person: String) -> [(String)] {
let register = "The alphabetic order of names are \(["RoboCop", person, "Terminator"])"
return register.sorted()
}
I think the problem is in my return parameters but couldn't find a way to Return string and array?
Many thanks
.sorted() operates on Sequence, and both Array and String are Sequences. [String].sorted (your first example) return a [String]. String.sorted (your second example) returns a [Character], since a String is made up of Characters.
Your second example is not "an array nested in a String." It's just a String. \(...) performs string interpolation. It doesn't nest anything.
If you want to return the String, you'll need to return String.
func namesList(person: String) -> String {
let register = ["RoboCop", person, "Terminator"].sorted()
return "The alphabetic order of names are \(register)"
}
As a very minor point, [(String)] is unusual syntax and can be confusing to experienced Swift devs. The correct way to write this is just [String] with no extra parentheses.
register variable is actually a String in the second case.
You should change the return type to just string.

swift 4 - get the memory size of array

I need to find out the size of an array (Swift 4 AnyObject) I just downloaded. Every mobile device has some free memory (size varies per user) and I need a way to examine the size of my download. I have searched and can't find a reasonable answer. Hope you can help!
This seems to be impossible currently. But if you just need a rough estimate, you can use class_getInstanceSize:
func size<T: AnyObject>(_ array: [T]) -> Int {
return class_getInstanceSize(T.self) * array.count
}
For this example array
class Test {
var a: Bool = false
var b: Int = 0
var c: String = ""
}
let array = [Test(), Test(), Test()]
you will get a size of 168. See this post for more information.
Of course, this approach will not work well with fields of variable length, for example with strings or arrays. The only possibility I see to get an accurate value is to write a function for each possible class to calculate the exact number of bytes need. For a string, for example, you can calculate the number of bytes with
"test".data(using: .utf8)!.count
But even with this approach you will not get the real size of the memory Swift uses internally to store the array.
So the question is: Why do you need to know the size of the array? And what is the reason that you do not know the size of downloaded data? If those data are received from outside, they must already be encoded in a byte representation, so counting should be quite easy.

Actionscript/Animate - Fill in next array spot if this one is already filled

so I am working on a graphical calculator (bit more of a challenge than the basic windows one), and I want to be able to do the entire "math" in one textfield, just like typing in "5+3-5*11/3" and it gives you the solution when you press '='
I decided to make it with arrays of numbers and symbols, but I have no idea how to make it to fill the next array if this one already is used:
var numbers:Array = new Array("","","","","","","","","","","","","","","","");
var actions:Array = new Array("","","","","","","","","","","","","","","","");
I am using split to split the numbers I input with symbols, and I want the numbers to be placed in the arrays. Example: I type in 555+666 and then I need to have something like
if (numbers[0] = "") {numbers[0] = 555}
else if (numbers[1] = "") {numbers[1] = 555}
else if.....
Know what I mean?
Pretty hard to describe...
something like... When I type in a number, if the numbers[0] is already filled, go fill in numbers[1], if numbers[1] is filled, go to numbers[2] etc
Even if I agree with #Nbooo and the Reverse Polish Notation
However Vectors may have a fixed length.
This is not an answer but just an example (if the length of Your Array must be defined):
//Just for information..
var numbs:Vector.<Number> = new Vector.<Number>(10,true);
var count:uint = 1;
for (var i in numbs){
numbs[i] = count++
}
trace(numbs);
// If You try to add an element to a Vector,
// You will get the following Error at compile time :
/*
RangeError: Error #1126: Cannot change the length of a fixed Vector.
at Vector$double/http://adobe.com/AS3/2006/builtin::push()
at Untitled_fla::MainTimeline/frame1()
*/
numbs.push(11);
// Will throw an Error #1126
trace(numbs);
If You use this code to update a fixed Vector, this will not throw an ERROR :
numbs[4]=11;
trace(numbs);
Output :
1,2,3,4,5,6,7,8,9,10
1,2,3,4,11,6,7,8,9,10
// length is 10, so no issue...
If You consider the performance between Arrays and vectors check this reference : Vector class versus Array class
I hope this may be helpful.
[EDIT]
I suggest you to check at those links too :
ActionScript 3 fundamentals: Arrays
ActionScript 3 fundamentals: Associative arrays, maps, and dictionaries
ActionScript 3 fundamentals: Vectors and ByteArrays
[/EDIT]
Best regards.
Nicolas.
What you want to implement is the Reverse Polish Notation. In actionscript3 arrays are dynamic, not fixed size, that means you can add elements to the array without concern about capacity (at least in your case).
const array:Array = new Array();
trace(array.length); // prints 0
array.push(1);
array.push(2);
trace(array.length); // prints 2
I suggest using "push" and "pop" methods of Array/Vector, since it's much more natural for such task. Using those methods will simplify your implementation, since you'll get rid of unnecessary checks like
if (numbers[1] == "") {...}
and replace it just with:
numbers.push(value);
and then to take a value from the top:
const value:String = numbers.pop();

Passing Swift strings as C strings in variable argument lists

I am trying to use the old printf-style string formatters to define a new Swift string from an old Swift string.
I notice this works fine as long as I am starting with a Swift string literal, but not a string variable.
// OK!
String(format:"%s", "hello world".cStringUsingEncoding(NSUTF8StringEncoding))
// ERROR: argument type '[CChar]?' does not conform to expected type 'CVarArgType'
let s = "hello world"
String(format:"%s", s.cStringUsingEncoding(NSUTF8StringEncoding))
Why does this happen?
And what's the best workaround?
(Please note that I am aware of but do not want to use the Cocoa string formatter %#. I want the printf-style formatting code because what I'm actually trying to do is get quick and dirty tabular alignment with codes like %-10s.)
This question concerns Swift 2.2.
Don't create a C string just for alignment. There is a method stringByPaddingToLength for that.
// Swift 2
s.stringByPaddingToLength(10, withString: " ", startingAtIndex: 0)
// Swift 3
s.padding(toLength: 10, withPad: " ", startingAt: 0)
Note that it will truncate the string if it is longer than 10 UTF-16 code units.
The problem here is, there are two methods in Swift named cStringUsingEncoding:
func cStringUsingEncoding(encoding: UInt) -> UnsafePointer<Int8>, as a method of NSString
func cStringUsingEncoding(encoding: NSStringEncoding) -> [CChar]?, as an extension of String
If we want a pointer, we need to ensure we are using an NSString, not a String.
In the first example, a string literal can be a String or NSString, so the compiler chooses NSString since the other one won't work.
But in the second example, the type of s is already set to String, so the method that returns [CChar]? is chosen.
This could be worked-around by forcing s to be an NSString:
let s: NSString = "hello world"
String(format:"%s", s.cStringUsingEncoding(NSUTF8StringEncoding))
kennytm's answer is clearly the best way.
But for anyone else who is still wondering about how to do it the wrong way, and get access to a c string based on a Swift String without going through NSString, this also seems to work (Swift 2.2):
var s:String = "dynamic string"
s.nulTerminatedUTF8.withUnsafeBufferPointer { (buffptr) -> String in
let retval:String = String(format:"%s",buffptr.baseAddress)
return retval
}

Conversion of entire array from int to double in order to do some aritmetic operations

I have this bit of code:
var arrayIntegers : [Int] = []
arrayIntegers += 0...33
var arrayDecimal : [Int] = []
arrayDecimal += 0...999
The problem is I can´t convert the values of both arrays to Double. What I want to do is to get a new array (called arrayDoubleComposed) with composite values, taking a value of arrayIntegers as the integer part and then taking another value of arrayDecimal as the floating part.
When I try to typecast this:
var arrayDoubleComposed : [Double] = []
arrayDoubleComposed = Double (arrayIntegers[] + (arrayDecimal[])/1000)
I´ve got an error. The same if I suppress the [].
I´m a little bit newcomer, I know...
Converting one kind of array into another is a job for map, which applies a function to each element of an array, returning the results as a new array of the type that function returns. In this case, you want a function that converts the Ints to a Double.
Try something like this:
let integers = Array(0...33)
let fractions = Array(0...999)
let arrayDoubleComposed = map(Zip2(integers, fractions)) {
(i, f) in
Double(i) + Double(f)/1_000
}
Zip2 takes two sequences and pairs them up – first elements together, second elements together etc. Then this passes that into map which combines the two elements.
(note also you can just initialize the arrays from the ranges rather than declaring them then adding the values)
It’s not clear what you mean to do, as the integer and fractional arrays are going to be different length. The way Zip2 handles this is to stop when the first sequence runs out, but this may not be what you want.
P.S. casts like the one you tried, which convert the contents of an array en-mass, only work in special cases when converting from Objective-C types to native Swift types, when the Swift compiler sprinkles some magic.

Resources