0

I am reading a PNG file from a website:

    String zoom = "5";   // Test Values
    String xmap = "30";  // Need to be adjusted
    String ymap = "30";
    String serverPathMap = "http://tile.openweathermap.org/map/precipitation_new/" + 
    zoom + "/" + xmap + "/" + ymap + ".png?" + "APPID=" + openWeatherMapApiKey;

I'm using this to get the PNG file into the String JSONBuffer.

    jsonBuffer = httpGETRequest(serverPathMap.c_str());

I verified the PNG file is correct by the correct received header. However, I need it in HEX formatted data.
This is an Example of the format of a file created directly by the compiler.

    const LV_ATTRIBUTE_MEM_ALIGN uint8_t ui_img_clock_face_150x150_fixed_png_data = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, ...

I have the code set to have the website read every hour, or at the press of a button. Currently the PNG file is stored in a String "JSONBuffer". I can get the size easily by JSONBuffer.length();

I'm trying to display the image in a Squareline Studio Image placeholder:

    //lv_image_set_src(ui_OpenWeatherMap, "Image from the Buffer");

    ui_OpenWeatherMap //is the name of the image placeholder.

My question is, how to convert to my JSonBuffer to HEX. I have seen it done in python, but I'm not good enough in python to convert it into C++.

Any help would be greatly appreciated.

I have tried several things.

Displayes the entire buffer. Not in HEX

    Serial.println("jsonBuffer: ");
    Serial.println(jsonBuffer);

Only displays the 1st 10 or so HEX numbers.

    snprintf(tempMap, bufferMapStringSize, "%s", jsonBuffer.c_str(), HEX);
    Serial.println("jsonBuffer: ");
    Serial.println(jsonBuffer);

Trying to insert the buffer directly doesn't work either.

   lv_image_set_src(ui_OpenWeatherMap, jsonBuffer.c_str());

I have tried many other things as well.

7
  • 3
    It sounds like you only want the image data (and not the meta data) from the PNG file converted, correct? And please clarify what you mean by HEX? You mean a byte array of colour values? I think that is what you want, but no C++ programmer would call that HEX. Hex implies text data, and I think you want binary data. Commented Dec 26, 2024 at 14:59
  • 2
    PNG is a binary image format, why are you downloading it as a String (let alone a JSON string)? And you can't treat a binary image as a null-terminated string as you are, as there are likely to be 0x00 bytes in the image data. I don't think you understand what lv_image_set_src() is actually expecting from you. Commented Dec 26, 2024 at 16:02
  • 1
    "This is an Example of the format of a file created directly by the compiler." -- That example is expressed in hex, but the data is binary, just like all data processed by a computer. (As an example, you could change each 0xFF to 255 -- that is, change the hexadecimal representation to the equivalent decimal representation -- without changing the compiled result.) Commented Dec 26, 2024 at 17:29
  • "but the data is binary, just like all data processed by a computer". The Russians built a ternary computer in the late 50's. Obviously never caught on though. Commented Dec 26, 2024 at 18:03
  • @RemyLebeau It could be base-64 encoded, I guess. Of course then it also needs decoding into bytes. Commented Dec 27, 2024 at 0:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.