iOS Development : writing byteArray ( Int8 ) to a file and retrieve the byteArray ( Int8 ) from the file

Hello developers,

I am trying the write byteArray [Int8] in a file in document folder and upload it to my server.

On recieving the file on server i want to read the file and convert it back to original byteArray [Int8].


Note : My Server is on java platform, so i want to convert the content of byte in byte[].


I am new to iOS development.

How can i achieve this ?


So far, I have successfully created the file and have written the data in Data( ) format. But now, how can i convert it to ByteArray again on my server ?

Converting Data and [Int8] is not so difficult.


Example data:

let data = Data([1,2,254,255])
print(data as NSData) //-> <0102feff>


Data to [Int8]:

var bytes: [Int8] = data.map{Int8(bitPattern: $0)}
print(bytes) //-> [1, 2, -2, -1]


[Int8] to Data:

let data2 = Data(bytes: bytes, count: bytes.count)
print(data2 as NSData) //-> <0102feff>


How to send data to your server depends on your server.

But, for example, you can use base64 encoding.


In Swift, Data can be easily coverted to base64 string:

let base64 = data.base64EncodedString()
print(base64) //-> AQL+/w==

If you decode the base64 string in Java, you can get byte array of type byte[].


Anyway, my recommendation, allways use `Data` on Swift side. You can send it in various ways -- raw binary, base64, hex...

And on Java side, you can easily receive it as or convert it to byte[].

Thank you so much. 🙂

Let me try this and will let you know.

Thank you so much for help.


I can now successfully create a file and read data back from it.


But i need to write in byteArray Int8[ ] instead of Data( ) format.

Is this possible ?

It would help if you explained the API you’re using to communicate with your server because the correct way to supply the data depends on the API. Some APIs, like

URLSession
, work in terms of
Data
values, but other APIs, like
OutputStream
, work in terms byte pointers and lengths.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
iOS Development : writing byteArray ( Int8 ) to a file and retrieve the byteArray ( Int8 ) from the file
 
 
Q