在 android studio 中将 4 个整数字节合并为一个 32 位无符号整数
Combine 4 integer bytes to a 32bit unsigned int in android studio
我想从我的 stm32wb55 发送一个 32 位时间戳到我自己的 android 应用程序。
这就是我在微控制器上将时间戳分成 4 个字节并通过 BLE 将它们发送到我的 phone:
的方式
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" 包括从微控制器启动以来的时间(以微秒为单位),并将在我的油灰控制台上正确显示!
这就是我在 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]);
我在 activity 中这样显示时间戳:
((TextView) findViewById(R.id.Time_TextView)).setText("Timestamp = "+ Timestamp );
但是我 activity 中的值从来没有意义。这些值是错误的并且通常是负数(时间戳永远不需要)。
我认为,我在 android studio 中组合字节的方式不正确。但我不知道我该怎么做。我尝试了很多,但没有成功。
有谁知道如何正确组合 android 中的字节?还是我在微控制器上拆分时间戳记错了?最后,我想在 activity 上将时间戳以微秒为单位显示为 32 位无符号整数。
感谢每一个帮助...
编辑:最后,我注意到我也在我的时间戳中交换了我的 MSB 和 LSB。这将是正确的顺序:
int Timestamp = (NotificationValue[11] << 24) | (NotificationValue[10] << 16) | (NotificationValue[9] << 8) | (NotificationValue[8]);
它不是单行的,但你可以试试这个来重新组装字节。
unsigned int timestamp = NotificationValue[8];
timestamp = (timestamp<<8)+NotificationValue[9];
timestamp = (timestamp<<8)+NotificationValue[10];
timestamp= (timestamp<<8)+NotificationValue[11];
出于某种原因,我觉得您所做的位移正在破坏数据。
编辑:
不知道 java 不使用无符号类型。您可以改用 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
无符号整数
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
有符号整数
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
我想从我的 stm32wb55 发送一个 32 位时间戳到我自己的 android 应用程序。 这就是我在微控制器上将时间戳分成 4 个字节并通过 BLE 将它们发送到我的 phone:
的方式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" 包括从微控制器启动以来的时间(以微秒为单位),并将在我的油灰控制台上正确显示! 这就是我在 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]);
我在 activity 中这样显示时间戳:
((TextView) findViewById(R.id.Time_TextView)).setText("Timestamp = "+ Timestamp );
但是我 activity 中的值从来没有意义。这些值是错误的并且通常是负数(时间戳永远不需要)。
我认为,我在 android studio 中组合字节的方式不正确。但我不知道我该怎么做。我尝试了很多,但没有成功。
有谁知道如何正确组合 android 中的字节?还是我在微控制器上拆分时间戳记错了?最后,我想在 activity 上将时间戳以微秒为单位显示为 32 位无符号整数。
感谢每一个帮助...
编辑:最后,我注意到我也在我的时间戳中交换了我的 MSB 和 LSB。这将是正确的顺序:
int Timestamp = (NotificationValue[11] << 24) | (NotificationValue[10] << 16) | (NotificationValue[9] << 8) | (NotificationValue[8]);
它不是单行的,但你可以试试这个来重新组装字节。
unsigned int timestamp = NotificationValue[8];
timestamp = (timestamp<<8)+NotificationValue[9];
timestamp = (timestamp<<8)+NotificationValue[10];
timestamp= (timestamp<<8)+NotificationValue[11];
出于某种原因,我觉得您所做的位移正在破坏数据。
编辑:
不知道 java 不使用无符号类型。您可以改用 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
无符号整数
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
有符号整数
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