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

nuynait
  • 1,912
  • 20
  • 27
  • 1
    Why are you using `NSData` instead of `Data`? – rmaddy May 05 '17 at 16:09
  • The reason I choose `NSData` instead of `Data`, is because if I call `data.getBytes(&num, length: MemoryLayout.size)` using `Data` then it will give a compiler error and force me to pass in `Uint8` instead of `Int`. – nuynait May 05 '17 at 16:10
  • 1
    The `Data` equivalent is very similar: `Data(bytes: &src, count: MemoryLayout.size)` – vadian May 05 '17 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. – nuynait May 05 '17 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. – vadian May 05 '17 at 16:41
  • @vadian It is so strange that I didn't notice the duplication of that question. It would be so regrettable if I miss reading that answer. You are right, that is really informative and I feel like learning a lot on `Data` object. I am changing everything into `Data` now. Really appreciate your help. – nuynait May 05 '17 at 17:05

1 Answers1

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

Community
  • 1
  • 1
nuynait
  • 1,912
  • 20
  • 27