简体   繁体   English

如何在 Arduino 库中将十进制转换为十六进制

[英]How to convert decimal to hex in Arduino library

I need to add a conversion from dec to hex in this function but failed.我需要在这个 function 中添加从 dec 到 hex 的转换,但失败了。

    void AB1805::set_minute(const uint8_t value)
{
  _minute = value % MAX_MINUTE;
  write_rtc_register(MINUTE_REGISTER, _minute);
}

I tried我试过了

void AB1805::set_minute(const uint8_t value)
{
  _minute = value % MAX_MINUTE;
  write_rtc_register(MINUTE_REGISTER, dec_hex(_minute,2));
}

uint8_t AB1805::dec_hex(uint8_t tens, uint8_t digits)
{
  uint8_t ret;
  ret = (tens<<4) + digits;
  return ret;
}

Hexadecimal and decimal notation are just two different ways to express the same value.十六进制和十进制表示法只是表示相同值的两种不同方式。

set_minute(255); is the exact same thing as set_minute(0xFF);set_minute(0xFF); or set_minute(0b11111111);set_minute(0b11111111);

You only need to specify a format if you want a string representation of that number in a specific format.如果您想要以特定格式表示该数字的字符串,则只需指定格式。 Not if you want to just use that value in your code.如果您只想在代码中使用该值,则不会。

Typically, RTCs store the data in BCD format通常,RTC 以 BCD 格式存储数据

uint8_t toBCD(uint8_t val) {  // expects a value 0 .. 99 
  return (val%10 & 0x0F) | (val/10 << 4); // returns 0x00 .. 0x99
}

No error checking, as usual in the Arduino world.没有错误检查,就像在 Arduino 世界中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM