Skip to content

Commit 7c7a2fe

Browse files
authored
Merge pull request #222 from LukePrior/testing
HEX to Int helper function
2 parents 9e8c099 + c0106b5 commit 7c7a2fe

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

js/xdata.js

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99
OIF411_Cef_Pressure = [ 0, 2, 3, 5, 10, 20, 30, 50, 100, 200, 300, 500, 1000, 1100];
1010
OIF411_Cef = [ 1.171, 1.171, 1.131, 1.092, 1.055, 1.032, 1.022, 1.015, 1.011, 1.008, 1.006, 1.004, 1, 1];
1111

12+
// https://stackoverflow.com/a/34679269/9389353
13+
function hexToInt(hex) {
14+
// Helper function to convert a signed hex value to an integer
15+
if (hex.length % 2 != 0) {
16+
hex = "0" + hex;
17+
}
18+
var num = parseInt(hex, 16);
19+
var maxVal = Math.pow(2, hex.length / 2 * 8);
20+
if (num > maxVal / 2 - 1) {
21+
num = num - maxVal
22+
}
23+
return num;
24+
}
1225

1326
function lerp(x, y, a){
1427
// Helper function for linear interpolation between two points
@@ -97,10 +110,7 @@ function parseOIF411(xdata, pressure){
97110
} else if (xdata.length == 20){
98111
// Measurement Data (Table 18)
99112
// Ozone pump temperature - signed int16
100-
_ozone_pump_temp = parseInt(xdata.substr(4,4),16);
101-
if ((_ozone_pump_temp & 0x8000) > 0) {
102-
_ozone_pump_temp = _ozone_pump_temp - 0x10000;
103-
}
113+
_ozone_pump_temp = hexToInt(xdata.substr(4,4));
104114
_ozone_pump_temp = _ozone_pump_temp*0.01; // Degrees C
105115
_output['oif411_ozone_pump_temp'] = Math.round(_ozone_pump_temp * 10) / 10; // 1 DP
106116

@@ -166,10 +176,7 @@ function parseCFH(xdata) {
166176
_output['cfh_instrument_number'] = parseInt(xdata.substr(2,2),16);
167177

168178
// Mirror temperature
169-
_mirror_temperature = parseInt(xdata.substr(4,6),16);
170-
if ((_mirror_temperature & 0x80000) > 0) {
171-
_mirror_temperature = _mirror_temperature - 0x1000000;
172-
}
179+
_mirror_temperature = hexToInt(xdata.substr(4,6));
173180
_mirror_temperature = _mirror_temperature*0.00001; // Degrees C
174181
_output['cfh_mirror_temperature'] = Math.round(_mirror_temperature*100000) / 100000; // 5 DP
175182

@@ -178,10 +185,7 @@ function parseCFH(xdata) {
178185
_output['cfh_optics_voltage'] = Math.round(_optics_voltage*1000000) / 1000000; // 6 DP
179186

180187
// Optics temperature
181-
_optics_temperature = parseInt(xdata.substr(16,4),16)*0.01; // Degrees C
182-
if ((_optics_temperature & 0x8000) > 0) {
183-
_optics_temperature = _optics_temperature - 0x10000;
184-
}
188+
_optics_temperature = hexToInt(xdata.substr(16,4))*0.01; // Degrees C
185189
_output['cfh_optics_temperature'] = Math.round(_optics_temperature*100) / 100; // 2 DP
186190

187191
// CFH battery
@@ -223,39 +227,24 @@ function parseCOBALD(xdata) {
223227
_output['cobald_sonde_number'] = parseInt(xdata.substr(4,3),16);
224228

225229
// Internal temperature
226-
_internal_temperature = parseInt(xdata.substr(7,3),16);
227-
if ((_internal_temperature & 0x800) > 0) {
228-
_internal_temperature = _internal_temperature - 0x1000;
229-
}
230+
_internal_temperature = hexToInt(xdata.substr(7,3));
230231
_internal_temperature = _internal_temperature/8; // Degrees C
231232
_output['cobald_internal_temperature'] = Math.round(_internal_temperature * 10) / 10; // 1 DP
232233

233234
// Blue backscatter
234-
_blue_backscatter = parseInt(xdata.substr(10,6),16);
235-
if ((_blue_backscatter & 0x800000) > 0) {
236-
_blue_backscatter = _blue_backscatter - 0x1000000;
237-
}
235+
_blue_backscatter = hexToInt(xdata.substr(10,6));
238236
_output['cobald_blue_backscatter'] = _blue_backscatter;
239237

240238
// Red backckatter
241-
_red_backscatter = parseInt(xdata.substr(16,6),16);
242-
if ((_red_backscatter & 0x800000) > 0) {
243-
_red_backscatter = _red_backscatter - 0x1000000;
244-
}
239+
_red_backscatter = hexToInt(xdata.substr(16,6));
245240
_output['cobald_red_backscatter'] = _red_backscatter;
246241

247242
// Blue monitor
248-
_blue_monitor = parseInt(xdata.substr(22,4),16);
249-
if ((_blue_monitor & 0x8000) > 0) {
250-
_blue_monitor = _blue_monitor - 0x10000;
251-
}
243+
_blue_monitor = hexToInt(xdata.substr(22,4));
252244
_output['cobald_blue_monitor'] = _blue_monitor;
253245

254246
// Red monitor
255-
_red_monitor = parseInt(xdata.substr(26,4),16);
256-
if ((_red_monitor & 0x8000) > 0) {
257-
_red_monitor = _red_monitor - 0x10000;
258-
}
247+
_red_monitor = hexToInt(xdata.substr(26,4));
259248
_output['cobald_red_monitor'] = _red_monitor;
260249

261250
return _output

0 commit comments

Comments
 (0)