0

I have a uint16_t array and I want to be able to convert it to a string and convert it back. The goal is to save the array with this library and load it back into a variable. What would be the best approach?

I tried to read the json key to the variable

uint16_t *program;
program = json["script"];

but it gives me this error:

no suitable conversion function from "nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double, std::allocator, nlohmann::adl_serializer, std::vector<uint8_t, std::allocator<uint8_t>>>" to "uint16_t *" exists

That's why I thought that maybe I should convert it to another datatype with available conversion first.

Solved:

I used vector<uint16_t> instead of an uint16_t array, which had available conversion in the library.

  • 1
    What have you tried and what problems have you got? – pptaszni May 10 '22 at 07:56
  • Does this answer your question? [Easiest way to convert int to string in C++](https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c) – Jabberwocky May 10 '22 at 07:57
  • 2
    Should just be `nlohmann::json j= array` and ` array= j`. Have you tried it? What problem did you encounter? Did you read the json documentation? – Alan Birtles May 10 '22 at 08:05
  • Can you please post code about what you tried to do? – X99 May 10 '22 at 11:40
  • I tried to read the json key to the variable ```uint16_t *program; program = json["script"];``` but it gives me this error: ```no suitable conversion function from "nlohmann::basic_json>>" to "uint16_t *" exists``` That's why I thought that maybe I should convert it to another datatype with available conversion first. – Tiefseefisch May 10 '22 at 14:37
  • 1
    `nlohmann::json` doesn't support this. It does support vectors though. `std::vector program; program = json["script"].get>();` – ChrisMM May 10 '22 at 16:27

0 Answers0