【问题标题】:Convert String in to MAC address [duplicate]将字符串转换为MAC地址[重复]
【发布时间】:2018-02-12 19:56:32
【问题描述】:

在 SPIFFS 的文件中,我以“XX:XX:XX:XX:XX:XX”的形式保存了有关 mac 地址的信息。 当我读取文件时,我需要将它从 STRING 转换为十六进制值的数组。

uint8_t* str2mac(char* mac){
  uint8_t bytes[6];
  int values[6];
  int i;
  if( 6 == sscanf( mac, "%x:%x:%x:%x:%x:%x%*c",&values[0], &values[1], &values[2],&values[3], &values[4], &values[5] ) ){
      /* convert to uint8_t */
      for( i = 0; i < 6; ++i )bytes[i] = (uint8_t) values[i];
  }else{
      /* invalid mac */
  } 
  return bytes;
}

wifi_set_macaddr(STATION_IF, str2mac((char*)readFileSPIFFS("/mac.txt").c_str()));

但我在某处的代码中有错误

当我将AA:00:00:00:00:01 放入文件时,我的ESP8266 设置为29:D5:23:40:00:00

我需要帮助,谢谢

【问题讨论】:

    标签: c++ arduino esp8266 esp32


    【解决方案1】:

    您正在返回一个指向“本地”变量的指针,即函数完成时生命周期结束的变量。使用这样的指针就是 UB,例如,它可能是您所看到的行为。

    要克服这个问题,您可以将数组作为参数传递;然后调用者负责内存管理。 顺便说一句:您可以使用格式 %hhx 直接读入 8 位无符号数据类型:

    int str2mac(const char* mac, uint8_t* values){
        if( 6 == sscanf( mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",&values[0], &values[1], &values[2],&values[3], &values[4], &values[5] ) ){
            return 1;
        }else{
            return 0;
        }
    }
    
    int main() {
    
        uint8_t values[6] = { 0 };
        int success = str2mac("AA:00:00:00:00:01", values);
        if (success) {
            for (int i=0; i<6; i++) {
                printf("%02X:",values[i]);
            }
        }
    }
    

    【讨论】:

    • 对不起,我总是得到 0 变量“成功”
    • 你能在printf("'%s'",mac)里面str2mac吗?
    • printf("'%s'",mac) 没有返回任何东西
    • 那么很明显该函数返回0。请检查输入,可能使用调试器。
    • 对不起,这是一个微控制器问题forum.arduino.cc/index.php?topic=437982.0感谢您的努力
    【解决方案2】:

    您的代码似乎与wifi_set_macaddr 不兼容(我查阅了 API 文档)。它需要一个指向 mac 地址的 uint8 指针,这意味着您编写它的方式不起作用(返回指向局部变量的指针等)。这是一个您应该能够适应您的目的的示例:

    #include <iostream>
    #include <fstream>
    
    // mock up/print result
    bool wifi_set_macaddr(uint8_t index, uint8_t *mac) 
    {
        std::cout << "index: " << (int)index << " mac: "; 
        for (int i = 0; i < 6; ++i)
            std::cout << std::hex << (int)mac[i] << " ";
        std::cout << std::endl;
    
        return true;
    }
    
    
    // test file
    void writeTestFile()
    {   
        std::ofstream ofs("mac.txt");
        if (!(ofs << "AA:00:00:00:00:01" << std::endl))
        {
            std::cout << "File error" << std::endl;
        }
        ofs.close();
    }
    
    int main() 
    {
        writeTestFile();
    
        uint8_t mac[6];
        int i = 0, x;
    
        std::ifstream ifs("mac.txt");
        for (; i < 6 && ifs >> std::hex >> x; ++i)
        {
            mac[i] = static_cast<uint8_t>(x);
            ifs.ignore();
        }
    
        if (i < 6)
        {
            std::cout << "File error or invalid MAC address" << std::endl;
            return 1;
        }
    
        wifi_set_macaddr(0x00, mac);
    
        return 0;
    }
    

    http://coliru.stacked-crooked.com/view?id=d387c628e590a467

    【讨论】:

      猜你喜欢
      • 2019-05-13
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2017-10-07
      • 1970-01-01
      相关资源
      最近更新 更多