javascript - Swift 中的 ArrayBuffer 和 Float64Array
问题描述
我正在尝试将其中一个 javascript 函数转换为 Swift 5 函数。但即使经过如此广泛的搜索,我也找不到任何东西。
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;
}
基本上我尝试构建相同的功能,var byteArray = [UInt8](stringVal.utf8)
但UInt8
最多只能存储 256 个,但我必须存储纪元时间戳。所以,它也不起作用。任何帮助,将不胜感激。
解决方案
推荐阅读
- c++ - 如何根据对的第二个元素对 STL c++ 中的对集合进行排序?
- sql - 查询以选择类别 ID,其中至少存在所有子类别 ID。(SQL 服务器)
- c# - 如何将 Azure 函数与部分类一起使用
- python - 如何检查熊猫导入是 modin 还是 original
- laravel - 数组视图不适用于 Laravel Blade 模板。(未定义的变量: )
- python - (x,y,w,h) = cv2.boundingRect(contour) 它总是返回 0,0,0,0
- swift - 在 SwiftUI 中使用全局函数
- c++ - 如何处理 RAII 中的资源等待
- django - 使用 Cypress 在 Heroku 上测试 Django 应用程序
- ruby-on-rails - Ruby on Rails:语法错误,意外的 tSYMBEG,期待 do 或 '{' 或 '('