时间转换为十六进制(字节数组)和反转
Time conversion to hexadecimal (byte array) and reverse
我从昨天开始尝试找出这个公式的反面:
我使用 HART(高速可寻址远程传感器)协议,规范是这样说的:
"... for the DEFAULT_VALUE, the constant-expression must resolve to an
unsigned 4-byte or 8-byte integer. example 4-byte TIME_VALUE encoding
for 05:14:26 could be expressed as: DEFAULT_VALUE =
((5*60+14)*60+26)*32000;"
这个值等于:603712000 -> 到字节数组 -> 23 FB EA 00
任何人都可以帮我找到反向公式吗?例如 444800000 -> 到字节数组 -> 1A 83 1C 00.. 这个数字首先除以 32000 等于:13900,从这里我想获得可读的时间格式:hh:mm:ss (就像上面的例子)。
我实现了这个功能,但似乎没有像我预期的那样工作:
secondsPassedToTime = function (seconds) {
var decimalTime = seconds / 86400;
var hour = decimalTime * 24;
var minutes = (hour % 1) * 60 // --> (hour % 1) -> get fractional part from number: 1.9 = 1 + 0.9
var seconds = (minutes % 1) * 60
hour = (~~hour).toString().length < 2 ? "0" + (~~hour).toString() : (~~hour).toString(); // --> (~~hour) -> get int from float: 1.9 = 1
minutes = (~~minutes).toString().length < 2 ? "0" + (~~minutes).toString() : (~~minutes).toString();
seconds = (~~seconds).toString().length < 2 ? "0" + (~~seconds).toString() : (~~seconds).toString();
var time = hour.toString() + ":" + minutes.toString() + ":" + seconds.toString();
return time;
};
console.log(secondsPassedToTime(13900))
这里我得到了一种可能的可读格式,但是当我将其转换为字节数组时,1a 83 1c 00 完全是另一个值。1A 82 9F 00。其中一个函数无法正常工作。
timeToHartTimeByteArray = function (time) {
var byteArray = new Array();
var regex = /^[0-9]{2}\:[0-9]{2}\:[0-9]{2}/;
if (time.match(regex)) {
time = time;
}
else {
throw "Invalid format for TIME! Format must be: hh:mm:ss";
}
var time = time.split(":");
var hours = parseFloat(time[0]) * 3600;
var minutes = parseFloat(time[1]) * 60;
var seconds = parseFloat(time[2]);
var finalTime = hours + minutes + seconds;
finalTime = finalTime * 32000;
var hexTime = finalTime.toString(16)
if (hexTime.length != 8) {
var hexTime = "0" + hexTime;
byteArray.push(hexTime.slice(0, 2))
byteArray.push(hexTime.slice(2, 4))
byteArray.push(hexTime.slice(4, 6))
byteArray.push(hexTime.slice(6, 8))
}
else {
byteArray.push(hexTime.slice(0, 2))
byteArray.push(hexTime.slice(2, 4))
byteArray.push(hexTime.slice(4, 6))
byteArray.push(hexTime.slice(6, 8))
}
return byteArray;
};
console.log(timeToHartTimeByteArray("03:51:39"))
找到问题了,是第一个函数,计算完全错误:
secondsPassedToTime = function (seconds) {
var hour = seconds * 0.00027778;
var hh = (~~hour).toString().length < 2 ? "0" + (~~hour).toString() : (~~hour).toString();
var minutes = (hour - hh) * 60.000;
var mm = (~~minutes).toString().length < 2 ? "0" + (~~minutes).toString() : (~~minutes).toString();
var seconds = (minutes - mm) / 0.016667;
var ss = (~~seconds).toString().length < 2 ? "0" + (~~seconds).toString() : (~~seconds).toString();
return (hh + ":" + mm + ":" + ss)
};
console.log(secondsPassedToTime(13900))
现在有了这个输出,第二个函数转换为正确的十六进制值:
timeToHartTimeByteArray = function (time) {
var byteArray = new Array();
var regex = /^[0-9]{2}\:[0-9]{2}\:[0-9]{2}/;
if (time.match(regex)) {
time = time;
}
else {
throw "Invalid format for TIME! Format must be: hh:mm:ss";
}
var time = time.split(":");
var hours = parseFloat(time[0]) * 3600;
var minutes = parseFloat(time[1]) * 60;
var seconds = parseFloat(time[2]);
var finalTime = hours + minutes + seconds;
finalTime = finalTime * 32000;
var hexTime = finalTime.toString(16)
if (hexTime.length != 8) {
var hexTime = "0" + hexTime;
byteArray.push(hexTime.slice(0, 2))
byteArray.push(hexTime.slice(2, 4))
byteArray.push(hexTime.slice(4, 6))
byteArray.push(hexTime.slice(6, 8))
}
else {
byteArray.push(hexTime.slice(0, 2))
byteArray.push(hexTime.slice(2, 4))
byteArray.push(hexTime.slice(4, 6))
byteArray.push(hexTime.slice(6, 8))
}
return byteArray;
};
console.log(timeToHartTimeByteArray("03:51:40"))
我从昨天开始尝试找出这个公式的反面: 我使用 HART(高速可寻址远程传感器)协议,规范是这样说的:
"... for the DEFAULT_VALUE, the constant-expression must resolve to an unsigned 4-byte or 8-byte integer. example 4-byte TIME_VALUE encoding for 05:14:26 could be expressed as: DEFAULT_VALUE = ((5*60+14)*60+26)*32000;"
这个值等于:603712000 -> 到字节数组 -> 23 FB EA 00
任何人都可以帮我找到反向公式吗?例如 444800000 -> 到字节数组 -> 1A 83 1C 00.. 这个数字首先除以 32000 等于:13900,从这里我想获得可读的时间格式:hh:mm:ss (就像上面的例子)。
我实现了这个功能,但似乎没有像我预期的那样工作:
secondsPassedToTime = function (seconds) {
var decimalTime = seconds / 86400;
var hour = decimalTime * 24;
var minutes = (hour % 1) * 60 // --> (hour % 1) -> get fractional part from number: 1.9 = 1 + 0.9
var seconds = (minutes % 1) * 60
hour = (~~hour).toString().length < 2 ? "0" + (~~hour).toString() : (~~hour).toString(); // --> (~~hour) -> get int from float: 1.9 = 1
minutes = (~~minutes).toString().length < 2 ? "0" + (~~minutes).toString() : (~~minutes).toString();
seconds = (~~seconds).toString().length < 2 ? "0" + (~~seconds).toString() : (~~seconds).toString();
var time = hour.toString() + ":" + minutes.toString() + ":" + seconds.toString();
return time;
};
console.log(secondsPassedToTime(13900))
这里我得到了一种可能的可读格式,但是当我将其转换为字节数组时,1a 83 1c 00 完全是另一个值。1A 82 9F 00。其中一个函数无法正常工作。
timeToHartTimeByteArray = function (time) {
var byteArray = new Array();
var regex = /^[0-9]{2}\:[0-9]{2}\:[0-9]{2}/;
if (time.match(regex)) {
time = time;
}
else {
throw "Invalid format for TIME! Format must be: hh:mm:ss";
}
var time = time.split(":");
var hours = parseFloat(time[0]) * 3600;
var minutes = parseFloat(time[1]) * 60;
var seconds = parseFloat(time[2]);
var finalTime = hours + minutes + seconds;
finalTime = finalTime * 32000;
var hexTime = finalTime.toString(16)
if (hexTime.length != 8) {
var hexTime = "0" + hexTime;
byteArray.push(hexTime.slice(0, 2))
byteArray.push(hexTime.slice(2, 4))
byteArray.push(hexTime.slice(4, 6))
byteArray.push(hexTime.slice(6, 8))
}
else {
byteArray.push(hexTime.slice(0, 2))
byteArray.push(hexTime.slice(2, 4))
byteArray.push(hexTime.slice(4, 6))
byteArray.push(hexTime.slice(6, 8))
}
return byteArray;
};
console.log(timeToHartTimeByteArray("03:51:39"))
找到问题了,是第一个函数,计算完全错误:
secondsPassedToTime = function (seconds) {
var hour = seconds * 0.00027778;
var hh = (~~hour).toString().length < 2 ? "0" + (~~hour).toString() : (~~hour).toString();
var minutes = (hour - hh) * 60.000;
var mm = (~~minutes).toString().length < 2 ? "0" + (~~minutes).toString() : (~~minutes).toString();
var seconds = (minutes - mm) / 0.016667;
var ss = (~~seconds).toString().length < 2 ? "0" + (~~seconds).toString() : (~~seconds).toString();
return (hh + ":" + mm + ":" + ss)
};
console.log(secondsPassedToTime(13900))
现在有了这个输出,第二个函数转换为正确的十六进制值:
timeToHartTimeByteArray = function (time) {
var byteArray = new Array();
var regex = /^[0-9]{2}\:[0-9]{2}\:[0-9]{2}/;
if (time.match(regex)) {
time = time;
}
else {
throw "Invalid format for TIME! Format must be: hh:mm:ss";
}
var time = time.split(":");
var hours = parseFloat(time[0]) * 3600;
var minutes = parseFloat(time[1]) * 60;
var seconds = parseFloat(time[2]);
var finalTime = hours + minutes + seconds;
finalTime = finalTime * 32000;
var hexTime = finalTime.toString(16)
if (hexTime.length != 8) {
var hexTime = "0" + hexTime;
byteArray.push(hexTime.slice(0, 2))
byteArray.push(hexTime.slice(2, 4))
byteArray.push(hexTime.slice(4, 6))
byteArray.push(hexTime.slice(6, 8))
}
else {
byteArray.push(hexTime.slice(0, 2))
byteArray.push(hexTime.slice(2, 4))
byteArray.push(hexTime.slice(4, 6))
byteArray.push(hexTime.slice(6, 8))
}
return byteArray;
};
console.log(timeToHartTimeByteArray("03:51:40"))