1

I have found the issue. In order to make question focus on the issue, I edited and reformat the question.

History: Previously I was using NSData instead of Data. I should use Data so I changed the question. Using Data does not solve the problem. I reformat it a little bit to make the problem stands out. See answer if you have the same issue

===========

I am using Swift 3.1

I want to convert the sub-array of a byte array into Int.

This is what I do here.

// I am extracting every 4 from the dataBytes and convert it to int
  var xDataArray = dataBytes[8...11]
  let xData = Data(buffer: UnsafeBufferPointer(start: &xDataArray, count: xDataArray.count))
  var xVal: Int32 = xData.withUnsafeBytes { $0.pointee }
  xVal = Int32(bigEndian: xVal)

  var yDataArray = dataBytes[12...15]
  let yData = Data(buffer: UnsafeBufferPointer(start: &yDataArray, count: yDataArray.count))
  var yVal: Int32 = yData.withUnsafeBytes { $0.pointee }
  yVal = Int32(bigEndian: yVal)

The xVal and yVal are the same and they seem to be very large random number.

enter image description here

enter image description here

6
  • 1
    Why are you using NSData instead of Data? Commented May 5, 2017 at 16:09
  • The reason I choose NSData instead of Data, is because if I call data.getBytes(&num, length: MemoryLayout<Int>.size) using Data then it will give a compiler error and force me to pass in Uint8 instead of Int. Commented May 5, 2017 at 16:10
  • 1
    The Data equivalent is very similar: Data(bytes: &src, count: MemoryLayout<Int>.size) Commented May 5, 2017 at 16:17
  • I found out that the problem is when I create the sub-array from the dataBytes @vadian I know the constructor for creating the data, however, what I want to do is to convert data into int. When I use Data instead of NSData I need to use copyBytes but it only accepts utf8 as input. Thanks for the help. Commented May 5, 2017 at 16:36
  • 1
    You don't need copyBytes. Data has appropriate API and is much more versatile than NSData. Did you read the duplicate question round trip Swift number types to/from Data and its answers in your linked answer? It's very informative. Commented May 5, 2017 at 16:41

1 Answer 1

1

I solved the issue.

I am using dataBytes[8...11] to get the subarray and this is the place where it get me the same result of the NSData.

Instead I use xDataArray:[UInt8] = [dataBytes[8], dataBytes[9], dataBytes[10], dataBytes[11]]

  var xDataArray: [UInt8] = [dataBytes[8], dataBytes[9], dataBytes[10], dataBytes[11]]
  let xData = NSData(bytes: &xDataArray, length: xDataArray.count)
  var xVal: Int32 = 0
  xData.getBytes(&xVal, length: MemoryLayout<Int32>.size)
  xVal = Int32(bigEndian: xVal)

  var yDataArray: [UInt8] = [dataBytes[12], dataBytes[13], dataBytes[14], dataBytes[15]]
  let yData = NSData(bytes: &yDataArray, length: yDataArray.count)
  var yVal: Int32 = 0
  yData.getBytes(&yVal, length: MemoryLayout<Int32>.size)
  yVal = Int32(bigEndian: yVal)

NOTE: If the Int gets from the Server is 32 Bit, Use Int32 instead of Int

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.