从Millis转换为十六进制字符串 [英] Convert from millis to hex String

查看:124
本文介绍了从Millis转换为十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将当前时间设置为信标的主要和次要值。假设这是当前时间 1465398279009 。我希望 9827 是专业的价值,而 9009 是次要的价值。我使用Java代码调用Shell脚本。这是我的Java代码

I want to set the current time as the major and minor values of a beacon. Lets suppose this is the current time 1465398279009. I want 9827 to be the value of the major and 9009 the value of the minor. I use a java code to call the shell script. this is my java code

Long millis = System.currentTimeMillis();
            String time=Long.toString(millis);
           // String major1=time.substring(...);
            String major1="99";
            String major2="99";
            String minor1="99";
            String minor2="99";
  ProcessBuilder pb2=new ProcessBuilder("/test.sh");
            pb2.environment().put("major1", major1);
            pb2.environment().put("major2", major2);
            pb2.environment().put("minor1", minor1);
            pb2.environment().put("minor2", minor2);
            Process script_exec = pb2.start();

这是test.sh的内容。

and this is the content of test.sh

sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d sd fh fb 33 j2 k9 j9 23 h2 n9 g7 v7 $param1 $major1 $major2 $minor1 $minor2 c5 00

在此示例中,我只是尝试将两个值都放入 9999 9999 ,但是结果是 39321 值将转换为大端。我很困惑,我不太了解哪种类型以及如何转换字符串。

In this example I just try to put both values to 9999 and 9999, but I get 39321 as result, I think the values are converted to big endian. I get confused and I don't understand well in which type and how shall I convert the String.

推荐答案

尝试一下:

 String major1 = String.format("%02X", (millis >> 32) & 0xff);
 String major2 = String.format("%02X", (millis >> 16) & 0xff);
 String minor1 = String.format("%02X", (millis >> 8) & 0xff);
 String minor2 = String.format("%02X", millis & 0xff);

上面的代码访问时间戳中的每个字节,并将其分配给适当的字段,然后对其进行格式化作为两个字符(一个字节)的十六进制数字。您希望脚本使用两个字符的十六进制数字。

The code above accesses each byte out of the timestamp and assigns it to the proper field, then formats it as a two character (one byte) hex number. A two character hex number is what you want for the script.

这篇关于从Millis转换为十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆