简体   繁体   English

时间转换为十六进制(字节数组)和反向

[英]Time conversion to hexadecimal (byte array) and reverse

I tried since yesterday to find out the reverse of this formula: I work with HART (Highway Addressable Remote Transducer) Protocol and the specification said this:我从昨天开始就试图找出这个公式的反面:我使用 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;" “...对于 DEFAULT_VALUE,常量表达式必须解析为无符号的 4 字节或 8 字节整数。例如 05:14:26 的 4 字节 TIME_VALUE 编码可以表示为:DEFAULT_VALUE = ((5*60 +14)*60+26)*32000;"

this value is equal with: ‭603712000‬ -> to byte array -> 23 FB EA 00这个值等于:603712000 -> 到字节数组 -> 23 FB EA 00

Can anyone please help me to find the reverse formula?谁能帮我找到反向公式? for example 444800000 -> to byte array -> ‭1A 83 1C 00‬.. this number first is divided with 32000 and it's equal to : 13900, and from here I want to obtain the readable time format: hh:mm:ss (like in the above example).例如 444800000 -> 到字节数组 -> 1A 83 1C 00.. 这个数字首先除以 32000,它等于:13900,从这里我想获得可读的时间格式:hh:mm:ss (就像上面的例子一样)。

I made this functions but seems to not work as I expected:我做了这个功能,但似乎没有按我预期的那样工作:

 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))

here I get a possible readable format, but when I transform this to byte array is not 1a 83 1c 00 is totally another value.. 1A 82 9F 00. One of these functions doesn't work properly.在这里,我得到了一种可能的可读格式,但是当我将其转换为字节数组时,不是 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"))

I found the problem, it was the first function, the calculation was completely wrong:发现问题了,是第一个函数,计算完全错误:

 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))

And now with this output, the second functions convert to the correct hexadecimal value:现在有了这个输出,第二个函数转换为正确的十六进制值:

 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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM