Search code examples
c++gosocketstcpimgui

ImGui rendering wrong characters (characters received from tcp sockets)


hope everyone is good. I have an imgui frame connected as a client with a tcp socket; The server is done in golang, I use this communication to perform login and everything works successfully. But I have a problem when I need to draw the text I receive when I get the account; Here is how I send the content encoded in base64

// < Connection to socket using net >
conn := (net.Conn)
conn.Write([]byte(b64.StdEncoding.EncodeToString([]byte(account))))

To receive in CPP I use this function

char recv[300];
recv(_conn, (char*)recv, 300, 0);
const char* result = base64_decode(recv).c_str();

And to draw the text on the screen :

void RenderTextColor(ImFont* font, const ImVec2& p_min, const ImVec2& p_max, ImU32 col, const char* text, const ImVec2& align)
{
    PushFont(font);
    PushStyleColor(ImGuiCol_Text, col);
    RenderTextClipped(p_min, p_max, text, NULL, NULL, align, NULL);
    PopStyleColor();
    PopFont();
}

The problem is : I can successfully print the received text in console; or even show a Message Box, everything is received and printed/showed successfully, but when i need to draw the text with imgui here is the result :

result

Only random characters and '?', It looks like an encoding error or something but I couldn't find what's going on.

Thanks in advance

EDIT: Here is the full cpp code :


/* struct */
struct card_data
{
    const char* content;
    const char* label1;
    const char* label2;

    bool operator==(const card_data& other) const {
        return std::wcscmp(label1, other.label1) == 0;
    }
};

std::vector<card_data> texts = { };

/* Receive part */
sprintf(message, "COMMAND|%s\n", request.args.c_str());
send(_conn, (const char*)message, strlen(message), 0)
char drecv[300];
recv(_conn, (char*)drecv, 300, 0)
size_t length = strlen(drecv);
while (length > 0 && isspace(drecv[length - 1])) {
    drecv[--length] = '\0';
}
const char* result = base64_decode(drecv);
std::stringstream ss(result);
std::string token;
texts.clear();
while (std::getline(ss, token, '/')) {
    std::vector<string> dist = splitString(token, '_');

    const char* label1 = dist[0].c_str();
    const char* label2 = dist[1].c_str();
    const char* content = dist[1].c_str();

    std::cout << label1 << std::endl; // Print successfully 
    std::cout << label2 << std::endl; // Print successfully 
    std::cout << content << std::endl; // Print successfully 

    texts.push_back({ content, label1, label2 }); // Here it push unknown characters

    /*
    
    const char* testLabel = "TEST LABEL";
    const char* testLabel2 = "TEST LABEL2";
    const char* testContent = "TEST CONTENT";

    texts.push_back({testContent, testLabel, testLabel2 }); // Here everything works

    */
}

/* Draw part */
<in main loop>
for (const auto& text : texts)
{
    ImGui::LabelledCard(text.content, text.label1, text.label2);
}

and then LabelledCard uses RenderTextColor


Solution

  • It was actually a problem from my struct card_data who used char* instead of char[]