简体   繁体   English

在 C++ 中将 HEX 字符串转换为 long

[英]Convert a HEX string to long in C++

I'm writing my first BLE system for Ardunio and I'm building it on top of an already working system for controlling RGB strip, and currently, I'm using the HEX numbers to know what I'm setting where.我正在为 Ardunio 编写我的第一个 BLE 系统,并且我正在将它构建在一个用于控制 RGB 条的已经工作的系统之上,目前,我正在使用 HEX 数字来知道我在哪里设置了什么。

OK reworking the question,好的重新处理问题,

#define BLE_RX 5
#define BLE_TX 4

SoftwareSerial BLE(BLE_RX, BLE_TX);

// set the startup color to white
unsigned long color = 0xFFFFFF;
// hold the last color set so we can off / on without losing prior settings
unsigned long lastColor = 0x000000;

void setup() 
{
  Serial.begin(9600);
  BLE.begin(9600);

  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);
  Serial.println("hello world");
}


void loop() {
  long r = ((color >> 16) & 0xFF);
  long g = ((color >> 8) & 0xFF);
  long b = (color & 0xFF);
  char data[100];
  analogWrite(RED_LED, r);
  analogWrite(GREEN_LED, g);
  analogWrite(BLUE_LED, b);

  readBLE();
}

long hstol(String recv){
  char c[6];
  recv.toCharArray(c, 6);
  return strtol(c, NULL, 16);
}

void readBLE(){
  if(BLE.available() > 0){
    String data = BLE.readString();
    if(data.length() == 6){
      lastColor = color;
      Serial.println(data);
      Serial.println(hstol(data));
      color = hstol(data);
    }
  }
}

When I first run my CPP the LED Strip is white as it should be if via BLE I send FF0000 (red), my LED's go green, this is my output当我第一次运行我的 CPP 时,如果通过 BLE 我发送FF0000 (红色),我的 LED 的 go 绿色,这是我的 output

arduino 的串行输出

When FF0000 should Equal 16711680, 00FF00 = 65280 and 0000FF = 255FF0000应该等于 16711680 时, 00FF00 = 65280 和0000FF = 255

Source of error in your posted code: you are using an array of 6 characters while it should be 7 to hold the whole recv string.您发布的代码中的错误来源:您使用的是 6 个字符的数组,而它应该是 7 来保存整个recv字符串。 Your code is currently showing an undefined behavior because strtol searches for null/whitespace character to finish parsing which is never present within bounds in char c[6] .您的代码当前显示出未定义的行为,因为strtol搜索空/空白字符以完成解析,该字符从未出现在char c[6]的范围内。

I would recommend using this type of approach.我建议使用这种方法。 It is just a single line thing, without requiring extra memory for some temporary character array.它只是单行的东西,不需要额外的 memory 用于一些临时字符数组。 (Also saves the time, otherwise wasted in copying the hex string). (也节省了时间,否则浪费在复制十六进制字符串上)。

void setup() {
  Serial.begin(9600);
  
  String x1 = "FF0000";
  long y1 = strtol(x1.c_str(), NULL, 16); // <--
  Serial.print(y1); // prints 16711680
  Serial.println();
  
  String x2 = "0xFF0000";
  long y2 = strtol(x2.c_str(), NULL, 0); // <--
  Serial.print(y2); // prints 16711680
  Serial.println();
  
  long y3 = strtol(x2.c_str() + 2, NULL, 16); // <--
  Serial.print(y3); // prints 16711680
  Serial.println();
}

void loop() {
    
}

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

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