在Arduino错误时将4个字节转换为长字节

Convert 4 bytes to long on Arduino error

本文关键字:字节 转换 4个 Arduino 错误      更新时间:2023-10-16

我有一个非常奇怪的不一致
我正在准备阅读Arduino上的二进制文件(对于midi播放器,如果你感兴趣的话)。如果我试图将Arduino上的4个字节组合为一个长字节,它会给我一个错误的结果
然而,如果我在PC上使用等效代码,我会得到正确的值。

输入为:0x12481248(0x12,0x48,0x12,0x148)(实际上是一个随机数)。

Arduino给出:4680。

代码::Blocks给出:306713160。

4680与0x1248相同,当您在Arduino上使用int而不是long(省略了2个字节)时会得到结果。

Arduino代码:

void setup(){
    Serial.begin(57600);
    char read1 = 0x12;
    char read2 = 0x48;
    char read3 = 0x12;
    char read4 = 0x48;
    unsigned long testint = read1<<24|read2<<16|read3<<8|read4;
    unsigned long testint2 = 306713160;
    Serial.println(testint);
    Serial.println(testint2);
}
void loop(){}

testint2是为了表明它不是由Serial.println()引起的。串行监视器的输出确实是:

4680

306713160

C++代码:

#include <iostream>
using namespace std;
int main(){
    char read1 = 0x12;
    char read2 = 0x48;
    char read3 = 0x12;
    char read4 = 0x48;
    unsigned long testint = read1<<24|read2<<16|read3<<8|read4;
    cout << testint;
}

知道发生了什么事吗?

此外,有人知道用Arduino/SD库转换字节的更好/更漂亮的方法吗?

在Arduino上,int大小为16位。

在行中:

unsigned long testint = read1<<24|read2<<16|read3<<8|read4;

即使结果存储在unsigned long(32位)中,也会在int s上执行逐位操作。

将此行更改为:

unsigned long testint = (unsigned long)read1 << 24 
                      | (unsigned long)read2 << 16
                      | (unsigned long)read3 << 8
                      | (unsigned long)read4;

在sizeof(int)=2的任何平台上,我都希望得到4680(=0x1248)的结果,我认为arduino就是这样。

这是因为(read1<<24)被隐式转换为int(不长),所以上面的两个字节会丢失。您应该将read*转换为无符号长第一

您还可以使用以下代码:

uint8_t data[4];
data[0]=read4;
data[1]=read3;
data[2]=read2;
data[3]=read1;
unsigned long testint =*(unsigned long*)(&data);
Here is another way to create 4 bytes to Long value and the reverse. Includes a simple test for these functions.
byte dataj[4];  //4 byte array output
unsigned long testint;
unsigned long testnum, temp;
unsigned long prev_time, cur_time = 0;
void setup() {
  Serial.begin(115200);
  Serial.println(F("********** Initalized +++++++"));
  //   -----4294967295  Max value
  testnum = 4294967295L;  //Start at maximum value
  for (int i = 0; i < 999; i++)
  {
    Serial.print(F("Test #"));
    Serial.print( i + 1);
    Serial.print(F("  Initial value = "));
    Serial.println(testnum);
    prev_time = micros();
    Long_2_Four_Bytes(testnum);

    testint = Four_Bytes_2_Long(dataj[3], dataj[2], dataj[1], dataj[0]);
    cur_time = micros();
    Serial.print(F("         final value   = "));
    Serial.print(testint);
    Serial.print("  time (microsec) = ");  Serial.print(prev_time - cur_time);
    if (testint == testnum)
      Serial.println();
    else
      Serial.println(F("  ERROR  <-----"));
    testnum--;
    delay(50);
  }
}
void loop() {}
//---- Long_2_Four_Bytes ---------------------------
// Fills an array 'dataj[]' with 4 bytes representing
// the 'unsigned long val'.
// Input : unsigned long number - range 0 - 4294967295
// Output : dataj[] where dataj[0] = LSB  & dataj[3] = MSB
//---------------------------------------------------
void Long_2_Four_Bytes(unsigned long val )
{
  unsigned long temp;
  byte data1[] = { 0, 0, 0, 0}; //4 byte array
  temp = val;
  for (byte bytegroup = 0; bytegroup  < 4; bytegroup ++)
  {
    int offset = bytegroup * 8;
    for (byte bit_pntr = 0; bit_pntr < 8; bit_pntr++)
    {
      if ( bitRead(temp, (7 - bit_pntr) + offset) == 1)
        bitSet(data1[bytegroup], (7 - bit_pntr));
    }
  }
  for (byte i = 0; i < 4; i++)
    dataj[i] = data1[i];
}

//-- Four_Bytes_2_Long -------------------------------------
// Creates an unsigned long from  4 bytes where A = MSB and
// D = LSB.  Based upon code by Mehrdad Nazmdar.
// Called by :
// Calls : N/A
// Returns :  unsigned long number - range 0 - 4294967295
//------------------------------------------------------------
unsigned long Four_Bytes_2_Long(byte A, byte B, byte C, byte D)
{
  uint8_t data[4];
  unsigned long testBigint;
  data[0] = D;  //LSB
  data[1] = C;
  data[2] = B;
  data[3] = A; //MSB
  testBigint = *(unsigned long*)(&data);
  return testBigint;
}