【问题标题】:Print only 7 last numbers from long long int仅打印 long long int 的最后 7 个数字
【发布时间】:2020-01-22 20:32:05
【问题描述】:

我有十六进制值 ex。 0300E0678C,我将它转换为 long long int 12899608460 但在这个阶段我必须只打印来自 long long int - 9608460 的最后 7 个数字,所以我尝试使用 %lld 进行 sprintf 但它什么也不返回。 有什么想法吗?

#include <SoftwareSerial.h>
#include <ID20Reader.h>
#include <PriUint64.h>


int rx_pin = 3;
int tx_pin = 2;
char output[16];
long long int numer;
char buf[50];

ID20Reader rfid(rx_pin, tx_pin);

void setup() {
    Serial.begin(9600);
    tone(4, 3400, 1000);
     pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  rfid.read();

  if(rfid.available())
  {
    digitalWrite(LED_BUILTIN, HIGH); 
    String code = rfid.get();
    Serial.println(code);
    tone(4, 4000, 500);
    char bufor[12];
    code.toCharArray(bufor,12);
    Serial.println(bufor);
    numer = hexToDec(bufor);
    Serial.println(PriUint64<DEC>(numer));
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
  }
}


long long hexToDec(String hexString) {

  long long decValue = 0;
  int nextInt;

  for (int i = 0; i < hexString.length(); i++) {

    nextInt = int(hexString.charAt(i));
    if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
    if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
    if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
    nextInt = constrain(nextInt, 0, 15);

    decValue = (decValue * 16) + nextInt;
  }
   return decValue;
}

【问题讨论】:

  • 在 avr-gcc 文档中,您可以找到“the ll length modifier will to abort the output, as this realization does not operate long long arguments.”“microchip.com/webdoc/AVRLibcReferenceManual/…”(顺便说一句,您为什么要进行这种奇怪的转换?)
  • 我从阅读器那里得到 HEX,但在 arduino 旁边我有其他设备,它只需要从十六进制转换而来的 DEC 的最后 7 位数字。
  • 运气不好。您需要 all 输入文本中的十六进制数字来提取最后七个十进制数字,因此 uint64_t 是正确的中间二进制数据类型,不幸的是 avr-gcc sprintf 不支持。 (如果您使用真正的 Arduino)

标签: arduino long-long


【解决方案1】:

我愿意:

...

byte myData[16];
int i = 0;

...


 numer = hexToDec(bufor);
    i=0;
   do
    {
    byte y = numer % 10;
    myData[i] = y;
    numer = numer / 10;
    i++;
  }
  while (numer != 0);

  for (int i = 6; i >= 0; i--)
  {
    Serial.print(myData[i]);
  }

第一次测试是乐观的;)

【讨论】:

    【解决方案2】:

    尝试这样的方法应该可以解决问题:

        #include <iostream>
        #include <string>
    
        int main()
        {
            // Example given
            constexpr long long int p = 0x300E0678C;
            std::printf("Example: %lld\n", p);
    
            // Create new string with p
            std::string tempStr(std::to_string(p), 0);
            long long int pLast7 = std::atoll( // Convert string to long long integer
                tempStr.substr(tempStr.length() - 7, 7) // cut the string 7 characters from the end, and have the next 7 characters returned
                .c_str()); // convert it to C-style string (const char*) for use with atoll
            std::printf("Last 7: %lld\n", pLast7);
        }
    

    这里是 cpp.sh 链接,如果您想自己尝试一下!

    cpp.sh/53evd

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多