Search code examples
javascriptflatbuffers

In JavaScript Google Flatbuffers, how does one write a ulong?


I have no problem with doing this in a C++ program, but I am stuck on writing a ulong in JS.

FB has no issue if I used the 32bit process.hrtime() value.

But how does do a createLong() for a 64bit ?

see: [ 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 template file

// 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;

Solution

  • You can use this function, it works well for timestamp

    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)
    

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