简体   繁体   English

在 android 工作室中将 4 个 integer 字节组合为 32 位无符号整数

[英]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.

相关问题 我可以使用32位操作系统虚拟化,但不能使用Android Studio - I can use 32bit OS virtualization but not Android Studio 如何为Windows 7、32位安装android studio - how do I install android studio for window 7, 32bit 有什么方法可以在32位系统上为Android Studio安装cmake以允许C ++支持吗? - Is the any way to install cmake for Android studio on a 32bit system to allow C++ support? 如何在32位系统上继续开发Android Studio x64项目? - How to continue development of an Android Studio x64 project on a 32bit system? Android Studio使用32位重新构建第三方jar文件 - Android Studio re-build third-party jar file with 32bit 使用 64 位或 32 位版本的 Android Studio 时,Gradle 卡在“build”或“assembleDebug” - Gradle gets stuck at either 'build' or 'assembleDebug' when using the 64bit or 32bit version of Android Studio VirtualBox中的Android-Win 7具有32位色深 - Android in VirtualBox - Win 7 with 32bit color depth 带有AArch64和32位应用程序的Android内核 - Android kernel with AArch64 and 32bit apps Android SDK 3模拟器无法在Windows 7 32bit上启动 - android sdk 3 emulator fail to launch on windows 7 32bit &#39;armeabi&#39;是32位还是64位? - Is 'armeabi' 32bit or 64bit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM