Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142353

ArrayBuffer and Float64Array in Swift

$
0
0

I'm trying to convert one of the javascript functions into Swift 5 function. But even after so much of extensive search I couldn't find anything on it.

function toArrayBuffer(type, ts, x, y, z, sc) {
  let userId = userID.getUserId();
  //console.log("Found GPS UserID: " + userId);
  if (userId == "Unauthor") {
    //console.log("GPS did not find the userID")
    return null;
  }

  let buffer = new ArrayBuffer(8 * 4);

  // Offset: 0
  let float64Bytes = new Float64Array(buffer);
  float64Bytes[0] = ts;

  // Offset: 8
  let bytes = new Float32Array(buffer);
  bytes[2] = x;
  bytes[3] = y;
  bytes[4] = z;
  bytes[5] = sc;

  // Offset: 24
  let uint8Bytes = new Uint8Array(buffer);
  uint8Bytes[24] = type;

  for (let i = 0; i < 8; i++) {
    if (userId.charCodeAt(i)) {
      uint8Bytes[i + 25] = userId.charCodeAt(i);
    }
  }

  return buffer;
}

Basically I tried to build the same function with var byteArray = [UInt8](stringVal.utf8) but UInt8 can store only upto 256, but I had to store epoch time stamp. So, it doesn't work too. Any help would be appreciated.


Viewing all articles
Browse latest Browse all 142353

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>