Just My Life & My Work

最近要讀取藍牙裝置回傳的資料來解析,需要將4 bytes轉成integer,才能知道訊號強度相對值。太久沒搞底層東東,有些生疏,好在嘗試一下就得到正確結果!

bluetooth Central Peripheral

程式碼大致如下:

/**
 Theme: Convert 4 Bytes to Integer
 IDE: Xcode 9
 Language: C
 Date: 107/04/03
 Author: HappyMan
 Blog: https://cg2010studio.com/
 */

#include
#include 

int buffToInteger(unsigned char *buffer)
{
    int a = (int)(buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]);
    return a;
}

int main()
{
    unsigned char data[] = {28, 91, 0, 0, 216, 91, 0, 0, 157, 92, 0, 0, 107, 93, 0, 0, 46, 94, 0, 0, 236, 94, 0, 0, 171, 95, 0, 0, 105, 96, 0, 0, 30, 97, 0, 0, 203, 97, 0, 0, 120, 98, 0, 0, 32, 99, 0, 0, 214, 99, 0, 0, 154, 100, 0, 0, 89, 101, 0, 0, 25, 102, 0, 0, 212, 102, 0, 0, 154, 103, 0, 0, 113, 104, 0, 0, 64, 105, 0, 0, 10, 106, 0, 0, 215, 106, 0, 0, 172, 107, 0, 0, 109, 108, 0, 0, 50, 109, 0, 0, 4, 110, 0, 0, 222, 110, 0, 0, 177, 111, 0, 0, 131, 112, 0, 0, 100, 113, 0, 0, 80, 114, 0, 0, 46, 115, 0, 0, 246, 115, 0, 0, 197, 116, 0, 0, 151, 117, 0, 0, 109, 118, 0, 0, 61, 119, 0, 0, 27, 120, 0, 0, 251, 120, 0, 0, 181, 79, 0, 0, 125, 80, 0, 0, 82, 81, 0, 0, 36, 82, 0, 0, 246, 82, 0, 0, 171, 83, 0, 0, 97, 84, 0, 0, 22, 85, 0, 0, 211, 85, 0, 0, 140, 86, 0, 0, 93, 87, 0, 0, 23, 88, 0, 0, 210, 88, 0, 0, 145, 89, 0, 0};
    unsigned char buffer[] = {0, 0, 0, 0};

    for (int i = 0; i < sizeof(data); i++) {

        if (i % 4 == 0) {
            buffer[3] = data[i];
        }
        if (i % 4 == 1) {
            buffer[2] = data[i];
        }
        if (i % 4 == 2) {
            buffer[1] = data[i];
        }
        if (i % 4 == 3) {
            buffer[0] = data[i];
            printf("[%d]: %d, %f(s)\n", i/4, buffToInteger(buffer),  buffToInteger(buffer)/250.0);
        }

    }
    return 0;
}

需要以每四個bytes來轉integer。

注意方向,例如:[28, 91, 0, 0]要擺成[0, 0, 91, 28]來轉換喔~

列印出來:

[0]: 23324, 93.296000(s)

[1]: 23512, 94.048000(s)

[2]: 23709, 94.836000(s)

[3]: 23915, 95.660000(s)

[4]: 24110, 96.440000(s)

[5]: 24300, 97.200000(s)

[6]: 24491, 97.964000(s)

[7]: 24681, 98.724000(s)

[8]: 24862, 99.448000(s)

[9]: 25035, 100.140000(s)

[10]: 25208, 100.832000(s)

[11]: 25376, 101.504000(s)

[12]: 25558, 102.232000(s)

[13]: 25754, 103.016000(s)

[14]: 25945, 103.780000(s)

[15]: 26137, 104.548000(s)

[16]: 26324, 105.296000(s)

[17]: 26522, 106.088000(s)

[18]: 26737, 106.948000(s)

[19]: 26944, 107.776000(s)

[20]: 27146, 108.584000(s)

[21]: 27351, 109.404000(s)

[22]: 27564, 110.256000(s)

[23]: 27757, 111.028000(s)

[24]: 27954, 111.816000(s)

[25]: 28164, 112.656000(s)

[26]: 28382, 113.528000(s)

[27]: 28593, 114.372000(s)

[28]: 28803, 115.212000(s)

[29]: 29028, 116.112000(s)

[30]: 29264, 117.056000(s)

[31]: 29486, 117.944000(s)

[32]: 29686, 118.744000(s)

[33]: 29893, 119.572000(s)

[34]: 30103, 120.412000(s)

[35]: 30317, 121.268000(s)

[36]: 30525, 122.100000(s)

[37]: 30747, 122.988000(s)

[38]: 30971, 123.884000(s)

[39]: 20405, 81.620000(s)

[40]: 20605, 82.420000(s)

[41]: 20818, 83.272000(s)

[42]: 21028, 84.112000(s)

[43]: 21238, 84.952000(s)

[44]: 21419, 85.676000(s)

[45]: 21601, 86.404000(s)

[46]: 21782, 87.128000(s)

[47]: 21971, 87.884000(s)

[48]: 22156, 88.624000(s)

[49]: 22365, 89.460000(s)

[50]: 22551, 90.204000(s)

[51]: 22738, 90.952000(s)

[52]: 22929, 91.716000(s)

參考:Convert four bytes to Integer using C++

廣告

發表留言

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

標籤雲

%d 位部落客按了讚: