Converting Swift Data into Int16


I have a Java server where I take a short[] and I convert it to a byte[] (Big Endian) and I send it to an iOS device. I am having trouble converting this byte array (or the Data in Swift) into an int16 array ([Int16]). I was also wondering if I was correct in assuming that the Swift equivalent of a Java short type is a Int16 in Swift.


Similarly as in <a href="https://stackoverflow.com/questions/38023838/round-trip-swift-number-types-to-from-data" rel="nofollow">round trip Swift number types to/from Data</a> you can use the withUnsafeBytes method and UnsafeBufferPointer<Int16> to get a view of the data as 16-bit integers. Then use the Int16(bigEndian:) initializer to convert the numbers from big endian to host byteorder. Example:

let data = Data(bytes: [0, 1, 0, 2, 1, 0, 255, 255]) let i16array = data.withUnsafeBytes { UnsafeBufferPointer<Int16>(start: $0, count: data.count/2).map(Int16.init(bigEndian:)) } print(i16array) // [1, 2, 256, -1]


  • Normalized string is not the same as ToCharArray
  • I would like to make an EditFor textbox accept numbers only
  • System.OverflowException: Value was either too large or too small for an Int16 in iis 6
  • EF Core to Mysql table binding
  • Convert int16 array to float
  • Equals int and short c# returns false [duplicate]
  • how do I design intermittent via points for travel itinerary
  • Numpy : Grouping/ binning values based on associations
  • Bing Map polyline using MVVM pattern XAML
  • query large table
  • count(*) cannot be selected in PL/SQL
  • Does using package generics require the package to be in Depends or Imports?
  • NHibernate: Persist an object with children
  • Obtain access token for both Microsoft Graph and individual service API endpoints (Outlook REST APIs
  • How to make a div appear under button
  • QObject::findChild() returns None without obvious reason
  • Get the negative of an Optional Chain
  • No such module 'Parse' following Parse iOS Swift Quickstart guide
  • What is this strange character in chrome's resource css viewer?
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • How can we prepend rows to a react native list-view?
  • Using MouseListener to select a range of cells in a grid
  • How to explicitly/implicitly implemented interface members in C++/CLI?
  • Web.config system.webserver errors
  • OOP Javascript - Is “get property” method necessary?
  • Cancel a live stream “fast motion” catch-up in Flash
  • Is there a javascript serializer for JSON.Net?
  • swift auto completion not working in Xcode6-Beta
  • Where to put my custom functions in Wordpress?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • 0x202A in filename: Why?
  • Python: how to group similar lists together in a list of lists?
  • Buffer size for converting unsigned long to string
  • Understanding cpu registers
  • embed rChart in Markdown
  • File not found error Google Drive API
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app