如何将两个Bytes数组转换为unsigned long变量?

coo*_*ter 0 c microcontroller pic mplab

我想将两个字节组合成一个无符号长变量,我当前的代码不起作用.我使用的是MPLAB C18编译器,这是我的代码.

    unsigned long red = 0;
    BYTE t[2];


    t[0]  = 0x12;
    t[1]  = 0x33;

    red = 100 * t[0] + t[1];
    printf("%lu", red);
Run Code Online (Sandbox Code Playgroud)

请让我知道为什么我没有得到1233作为我的输出.

unx*_*nut 5

你是乘t[0]100时,你应该要乘以256.更好的方法是向左移t[0]8位并添加t[1].

red = ( t[0] << 8 ) | t[1];
Run Code Online (Sandbox Code Playgroud)

  • @cookiemonster十六进制仍然不是十进制的. (2认同)