Converting const unsigned Char* to String

Hi,
i'm trying to use a BLE Characteristic to tell my Arduino what to do. The BLE Characteristic is a 20 Byte const unsigned Char pointer.

How can i convert it or read it ?

I'm using the CurieBLE.h on a Arduino 101 to communicate over BLE.

  const unsigned char* tempchar = Choose.value(); // Choose is the BLE Characteristic
  String tempstring = tempchar; // thats the problematic line

invalid conversion from 'const unsigned char*' to 'const char*' [-fpermissive]

How can i convert it

What do you think you need to convert it to?

or read it ?

Way more context is needed for this question. You have a pointer to some memory. You can access the data being pointed to as if the pointer was an array. The nth character is printed using:

    Serial.print("The 3rd character is: ");
    Serial.println(tempchar[3]);

The useless String class deals with signed chars, not unsigned chars.

Why any moron would make their code return character data in a non-conventional data type is the real mystery.

Context :
Data is lying an an sd Card in the arduino. As soon as you want the data you just walk by with your smartphone, connect over BLE and chose one of the folders availible on the SD Card. The Arduino reads the data and tramnsmits it over BLE so you can plot the Data on your smartphone.

the SD.open() Method needs a String, if you want to access a Specificfolder. So i want to convert the const unsigned char* to a String.

the SD.open() Method needs a String,

It most certainly does NOT. It needs a string. Strings and strings are NOT the same thing.

ok corrected: it works with a String. i know the difference.
A String is a con signed char* . Thats why i thougt the conversion should be easy.

Thats why i thougt the conversion should be easy.

YOU decided, for some unknown reason, to treat the data returned by the value() method as signed char. The signature is actually:

 virtual const byte* value() const;

which almost certainly means that the bit pattern in the bytes is actually the same as a char.

Change your variable type from unsigned char * to char *.

I didnt decide anything. The Error said that i was trying to convert const unsigned char* to a const char*.

But However: String tempstring = reinterpret_cast<const char*>(Choose.value());
is working.

I didnt decide anything.

Someone decided that YOUR code should use const unsigned char * as the variable type. If that wasn’t you, who was it?

But However:
Code: [Select]

String tempstring = reinterpret_cast<const char*>(Choose.value());

is working.

What a waste of memory.

momokobele:
A String is a con signed char*

Nope.

The upper-case String refers an Arduino C++ class for managing and manipulating characters. There are lots of reasons to avoid String. Here's one of the best: The Evils of Arduino Strings.

The lower-case string refers to C/C++ character arrays, like char str[10], or double-quoted constants, "like this." Double-quoted string constants are of type const char *. You should use the F macro wherever possible to force them to be used from FLASH memory, instead of copying them to RAM:

    Serial.print( F("This saves RAM") );

The SD open call requires a const char *, so you can pass a character array that you filled out, or you can pass a double-quoted string constant. You cannot pass a String

-dev:
The SD open call requires a const char *, so you can pass a character array that you filled out, or you can pass a double-quoted string constant. You cannot pass a String

There are versions for both const char * and String:

class SDClass {
...
  File open(const char *filename, uint8_t mode = FILE_READ);
  File open(const String &filename, uint8_t mode = FILE_READ) { return open( filename.c_str(), mode ); }

There are versions for both const char * and String:

Eew. That's only in the bundled SD library, not the current SdFat library. I always recommend SdFat over SD.

Holy mackerel, Paul. You're not going to turn momo into the Feds or anything, are you? I tuned in here to find some answers. Instead, I'm reading the interrogation of this poor person. Could we stick to answers and being decent to each other? Cool.

2 Likes

chocolatchips:
Holy mackerel, Paul. You're not going to turn momo into the Feds or anything, are you? I tuned in here to find some answers. Instead, I'm reading the interrogation of this poor person. Could we stick to answers and being decent to each other? Cool.

the dude has more than 80K posts, most with similar tone but usually worse. But folks generally see the gold within the sharks's mouth.

your efforts are commendable but scolding him has proven futile, so jump on the pain train and watch the learning happen!