[英]Combine 4 integer bytes to a 32bit unsigned int in android studio
I want to send a 32bit timestamp from my stm32wb55 to my own android app.我想从我的 stm32wb55 发送一个 32 位时间戳到我自己的 android 应用程序。 This is how I split the timestamp in 4 bytes on the microcontroller and send these over BLE to my phone:这就是我在微控制器上将时间戳拆分为 4 个字节并通过 BLE 将它们发送到我的手机的方式:
void calculate_timestamp(void){
//splitting in 4 bytes
uint8_t time_micro32_1 = time_micro32 & 0xFF;
uint8_t time_micro32_2 = (time_micro32 >> 8) & 0xFF;
uint8_t time_micro32_3 = (time_micro32 >> 16) & 0xFF;
uint8_t time_micro32_4 = (time_micro32 >> 24) & 0xFF;
//send the bytes over ble
P2P_Server_App_Context.Acceleration.Timestamp_1 = time_micro32_1; //8
P2P_Server_App_Context.Acceleration.Timestamp_2 = time_micro32_2; //9
P2P_Server_App_Context.Acceleration.Timestamp_3 = time_micro32_3; //10
P2P_Server_App_Context.Acceleration.Timestamp_4 = time_micro32_4; //11
} }
"time_micro32" includes the time in microseconds since startup from the microcontroller and will be displayed correct on my putty-console: And this is how I combine the 4 bytes in android studio: “time_micro32”包括从微控制器启动以来的微秒时间,并将在我的腻子控制台上正确显示:这就是我在 android 工作室中组合 4 个字节的方式:
//NotificationValue[8] includes value of time_micro32_1
//NotificationValue[9] includes value of time_micro32_2
//NotificationValue[10] includes value of time_micro32_3
//NotificationValue[11] includes value of time_micro32_4
int Timestamp = (NotificationValue[8] << 24) | (NotificationValue[9] << 16) | (NotificationValue[10] << 8) | (NotificationValue[11]);
I display the timestamp in my activity like this:我在我的活动中显示时间戳,如下所示:
((TextView) findViewById(R.id.Time_TextView)).setText("Timestamp = "+ Timestamp );
But the values in my activity never makes sense.但我活动中的价值观从来没有意义。 The values are false and be often negativ (what is never desired by timestamps).这些值是错误的,并且通常是负面的(时间戳永远不需要)。
I think, I combine the bytes in android studio not the right way.我认为,我结合 android 工作室中的字节不是正确的方式。 But I have no more idea how can I do this.但我不知道该怎么做。 I tried many, but nothing was successfull.我尝试了很多,但都没有成功。
Does anyone have an idea how can I combine the bytes in android correctly?有谁知道如何正确组合 android 中的字节? Or have I made a mistake by splitting the timestamp on my microcontroller?还是我在微控制器上拆分时间戳犯了一个错误? In the end, I want to display the timestamp in microseconds on my activity as a 32bit unsigned integer.最后,我想以 32 位无符号 integer 的形式显示我的活动的时间戳(以微秒为单位)。
Thankfull about every help...感谢每一个帮助...
EDIT: At the end, I have noticed that I have interchanged my MSB and LSB in my timestamp, too.编辑:最后,我注意到我在我的时间戳中也交换了我的 MSB 和 LSB。 This would be the right order:这将是正确的顺序:
int Timestamp = (NotificationValue[11] << 24) | (NotificationValue[10] << 16) | (NotificationValue[9] << 8) | (NotificationValue[8]);
it's not a one liner but you can try this to get the bytes re-assembled.它不是一个衬里,但你可以尝试这个来重新组装字节。
unsigned int timestamp = NotificationValue[8];
timestamp = (timestamp<<8)+NotificationValue[9];
timestamp = (timestamp<<8)+NotificationValue[10];
timestamp= (timestamp<<8)+NotificationValue[11];
for some reason i feel that the bit shifts you were doing was mangling the data.出于某种原因,我觉得您所做的位移正在破坏数据。
edit:编辑:
didn't know java doesn't use unsigned types.不知道 java 不使用无符号类型。 You could use a long instead, and convert it to unsigned.您可以改用 long,并将其转换为无符号。
long timestamp = NotificationValue[8];
timestamp= (timestamp<<8) + NotificationValue[9];
timestamp= (timestamp<<8)+ NotificationValue[10];
timestamp = (timestamp<<8)+ NotificationValue[11];
timestamp = timestamp & 0xffffffffL; //this should remove the signage
unsigned integer无符号 integer
byte[] NotificationValue = new byte[] { (byte)0xFC, (byte)0xFD, (byte)0xFE, (byte)0xFF };
long Timestamp =
((NotificationValue[0] & 0xFFL) << 24) //00000000_00000000_00000000_00000000_11111111_00000000_00000000_00000000
| ((NotificationValue[1] & 0xFFL) << 16) //00000000_00000000_00000000_00000000_00000000_11111111_00000000_00000000
| ((NotificationValue[2] & 0xFFL) << 8) //00000000_00000000_00000000_00000000_00000000_00000000_11111111_00000000
| (NotificationValue[3] & 0xFFL); //00000000_00000000_00000000_00000000_00000000_00000000_00000000_11111111
signed integer签名 integer
byte[] NotificationValue = new byte[] { (byte)0xFC, (byte)0xFD, (byte)0xFE, (byte)0xFF };
int Timestamp =
(NotificationValue[0] << 24) //11111111_00000000_00000000_00000000
| ((NotificationValue[1] & 0xFF) << 16) //00000000_11111111_00000000_00000000
| ((NotificationValue[2] & 0xFF) << 8) //00000000_00000000_11111111_00000000
| (NotificationValue[3] & 0xFF); //00000000_00000000_00000000_11111111
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.