为什么 const_cast 来自和来自 const char * 导致 strcmp != 0
why does const_cast from and to a const char * result in strcmp != 0
我正在使用 Arduino IDE 在 ESP32 上编写蓝牙代码。 const_cast const char * 有点麻烦。读取配对设备值时有一个我想删除的“\n”换行符。所以我将它转换为可编辑的 char * 然后将其转换回 const char *,但不知何故它不是相同的值。
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLEService *pService;
BLECharacteristic *pTxCharacteristic;
BLECharacteristic *pRxCharacteristic;
bool BLE_Paired = false;
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect( BLEServer* pServer ) {
BLE_Paired = true;
};
void onDisconnect( BLEServer* pServer ) {
BLE_Paired = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite( BLECharacteristic *pCharacteristic ) {
std::string rxValue = pCharacteristic->getValue();
}
};
char* _name = "your nm";
const char* C_name = "Rick";
bool name_OK = false;
void setup() {
Serial.begin( 115200 );
BLEDevice::init( "bluetooth" );
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
pService = pServer->createService(SERVICE_UUID);
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY );
pTxCharacteristic->addDescriptor( new BLE2902() );
pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE );
pRxCharacteristic->setCallbacks( new MyCallbacks() );
pService->start();
pServer->getAdvertising()->start();
}
void loop() {
while( BLE_Paired ) {
while( !name_OK ) {
// Receive the response from the Paired Device
pTxCharacteristic->setValue( "Name? " );
pTxCharacteristic->notify();
delay( 100 ); // bluetooth stack can get congested
// read it as const char *, but cast it as just a char *
_name = const_cast<char *>(pRxCharacteristic->getValue().c_str());
// the Paired Device adds a '\n' when the name is entered; remove it
for( int j=0; _name[j] != '[=11=]'; j++ ) {
if( _name[j] == '\n' ) {
_name[j] = NULL; // NULL or '[=11=]'
}
}
// cast name back to a const char *
const char* C_nm = const_cast<const char *>(_name);
Serial.print( "const _names: " );
Serial.print( C_name );
Serial.print( " =? " );
Serial.print( C_nm );
Serial.print( " : " );
Serial.println( strcmp( C_name, C_nm )); // result is -13, not 0
if( strcmp( C_name, C_nm ) == 0 )
name_OK = true;
}
}
}
因此,我配对我的 BLE 设备,输入 Rick,代码将“\n”替换为 NULL,但值与设置为 Rick 的 const char * 变量不同。它是 -13 而不是 0。为什么?
非常感谢。
我想从 const char *
到 char*
的转换与您的问题无关。而是一种使 _name 内容完全可修改的解决方法。
尝试删除并trim 关闭所有不可打印的字符和空格:
if( _name[j] <= ' ' ) _name[j] = 0;
顺便说一句:
我认为修改 const char 是有问题的。
NULL 用于指针,而不是 char 的内容。
但这只是美丽。
我还用“/n”删除了“/r”,缩短了字符串以匹配。感谢 datafiddler.
我正在使用 Arduino IDE 在 ESP32 上编写蓝牙代码。 const_cast const char * 有点麻烦。读取配对设备值时有一个我想删除的“\n”换行符。所以我将它转换为可编辑的 char * 然后将其转换回 const char *,但不知何故它不是相同的值。
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLEService *pService;
BLECharacteristic *pTxCharacteristic;
BLECharacteristic *pRxCharacteristic;
bool BLE_Paired = false;
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect( BLEServer* pServer ) {
BLE_Paired = true;
};
void onDisconnect( BLEServer* pServer ) {
BLE_Paired = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite( BLECharacteristic *pCharacteristic ) {
std::string rxValue = pCharacteristic->getValue();
}
};
char* _name = "your nm";
const char* C_name = "Rick";
bool name_OK = false;
void setup() {
Serial.begin( 115200 );
BLEDevice::init( "bluetooth" );
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
pService = pServer->createService(SERVICE_UUID);
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY );
pTxCharacteristic->addDescriptor( new BLE2902() );
pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE );
pRxCharacteristic->setCallbacks( new MyCallbacks() );
pService->start();
pServer->getAdvertising()->start();
}
void loop() {
while( BLE_Paired ) {
while( !name_OK ) {
// Receive the response from the Paired Device
pTxCharacteristic->setValue( "Name? " );
pTxCharacteristic->notify();
delay( 100 ); // bluetooth stack can get congested
// read it as const char *, but cast it as just a char *
_name = const_cast<char *>(pRxCharacteristic->getValue().c_str());
// the Paired Device adds a '\n' when the name is entered; remove it
for( int j=0; _name[j] != '[=11=]'; j++ ) {
if( _name[j] == '\n' ) {
_name[j] = NULL; // NULL or '[=11=]'
}
}
// cast name back to a const char *
const char* C_nm = const_cast<const char *>(_name);
Serial.print( "const _names: " );
Serial.print( C_name );
Serial.print( " =? " );
Serial.print( C_nm );
Serial.print( " : " );
Serial.println( strcmp( C_name, C_nm )); // result is -13, not 0
if( strcmp( C_name, C_nm ) == 0 )
name_OK = true;
}
}
}
因此,我配对我的 BLE 设备,输入 Rick,代码将“\n”替换为 NULL,但值与设置为 Rick 的 const char * 变量不同。它是 -13 而不是 0。为什么? 非常感谢。
我想从 const char *
到 char*
的转换与您的问题无关。而是一种使 _name 内容完全可修改的解决方法。
尝试删除并trim 关闭所有不可打印的字符和空格:
if( _name[j] <= ' ' ) _name[j] = 0;
顺便说一句: 我认为修改 const char 是有问题的。 NULL 用于指针,而不是 char 的内容。
但这只是美丽。
我还用“/n”删除了“/r”,缩短了字符串以匹配。感谢 datafiddler.