简体   繁体   中英

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:

"... 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;"

this value is equal with: ‭603712000‬ -> to byte array -> 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).

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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