在JavaScript Google Flatbuffers中,如何编写ulong?

时间:2018-09-05 20:30:29

标签: javascript flatbuffers

我在C ++程序中做到这一点没有问题,但是我一直坚持用JS写ulong。

如果我使用32位process.hrtime()值,FB没问题。

但是对于64位的createLong()怎么办?

请参阅:[[https://nodejs.org/api/process.html#process_process_hrtime_bigint]

# commented line does not work
# let timeStamp = process.hrtime.bigint()
        let timeStamp = process.hrtime()
        let ts = builder.createLong(0, timeStamp)
        PNT.Telemetry.startTelemetry(builder)
        PNT.Telemetry.addSystemTime(builder, ts)

FB模板文件

// Simple Telemetry data from/to Sim and Sensor
namespace PNT;
enum DeviceType:byte { IMU, VAN, GPS, MAGNAV, SOOP }
struct PosVector {
  lat:double;
  lon:double;
  alt:double;
}
table Telemetry {
  source: string;
  systemTime:ulong = 0;
  systemTimeString: string;
  description: string;
  position: PosVector; 
}

root_type Telemetry;

2 个答案:

答案 0 :(得分:1)

使用flatbuffers.Long,下面是一个示例:https://github.com/google/flatbuffers/blob/master/tests/JavaScriptTest.js#L160

不幸的是,FlatBuffers中没有代码可以将bigint转换为2个32位值,这很容易添加。目前,您可能需要进行自己的转换。

答案 1 :(得分:1)

您可以使用此功能,它可以很好地用于时间戳记

var flatBufferTimeStamp = function(value) {
    var bin = (value).toString(2); 
    var pad = new Array(64 - bin.length + 1 ).join('0'); 
    bin = pad + bin;  
    return {
        low: parseInt(bin.substring(32), 2), 
        high: parseInt(bin.substring(0, 32), 2)
    };
}


var timeStamp = flatBufferTimeStamp(process.hrtime())
let ts = builder.createLong(timeStamp.low, timeStamp.high);
PNT.Telemetry.startTelemetry(builder)
PNT.Telemetry.addSystemTime(builder, ts)

来源:https://groups.google.com/forum/#!topic/flatbuffers/ieXNEsB_2wc