0

I am looking for a solution how to get a filled spot in the string when I send the 0x00 value.

as an example, by sending the value hexToASCII("6765656b73") I get the string "geeks". But when I send the value hexToASCII("0065006b73") I get only the string "eks", the 00 do not even count. Is there something I can add to fill the spots in the string, which has the same value as ASCII NUL? Thanks a lot in advance..

// C++ program to convert hexadecimal
// string to ASCII format string
#include <bits/stdc++.h>
using namespace std;
string hexToASCII(string hex)
{
    // initialize the ASCII code string as empty.
    string ascii = "";
    for (size_t i = 0; i < hex.length(); i += 2)
    {
        // extract two characters from hex string
        string part = hex.substr(i, 2);
 
        // change it into base 16 and
        // typecast as the character
        char ch = stoul(part, nullptr, 16);
 
        // add this char to final ASCII string
        ascii += ch;
    }
    return ascii;
}
 
// Driver Code
int main()
{
    // print the ASCII string.
    cout << hexToASCII("6765656b73") << endl;
 
    return 0;
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
Ramles
  • 5
  • 3
  • 3
    The problem is not that the null characters are missing, it's that they don't map to a printable character. Consider yourself lucky, in many contexts they'll truncate the string prematurely. – Mark Ransom Jul 02 '21 at 15:04
  • When your output routine detects a `'\0'` character, have it emit whatever character(s) you want, such as perhaps `"(nul)"`. – Eljay Jul 02 '21 at 15:05
  • There are several ASCII values that have no printable representation. Have you chosen a visual representation that you want to see when these values are encountered? – Drew Dormann Jul 02 '21 at 15:13
  • *"I get only the string "eks""* -- no, you get the string `"\0e\0ks"`. Check the length of the string you produce; it is 5. *(You could also try initializing a `std::string` with the string literal `"\0e\0ks"` and seeing what happens when you stream that to `std::cout`. Don't jump to conclusions on the basis of a single experiment.)* – JaMiT Jul 02 '21 at 15:17
  • see [Why should I not `#include `?](https://stackoverflow.com/q/31816095/995714), [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Jul 02 '21 at 15:35
  • @JaMiT Thats right, I just checked the length and it is 5. The problem actually is that I want to send the string to a buffer which saves the string through uart. When I send a string which contains \0, the \0 is not to find when I read what the buffer has received through UART.. And this is actually my main problem now with this character.. – Ramles Jul 02 '21 at 16:52
  • @Ramles That is your main problem, and yet the question you asked does not address it. – JaMiT Jul 02 '21 at 17:32
  • @JaMiT Well answering this question would solve the problem that I have. You can read more about it if you want. https://stackoverflow.com/questions/68199985/how-can-i-send-the-first-and-second-ascii-characters-as-null-character-in-a-stri – Ramles Jul 02 '21 at 17:51
  • 1
    @Ramles Oh thank you. I have a reading suggestion for you as well: [XY problem](https://en.wikipedia.org/wiki/XY_problem). Answering a question based upon a false assumption is unlikely to solve a real problem. (Also, an answer for C++ is not necessarily an answer for C. Especially once `std::string` and internal null characters enter the picture.) – JaMiT Jul 02 '21 at 17:59
  • @JaMiT well I did try the XY Problem but it didn't help me much. Thats why I tried to asked another question that is related to my problem. Moreover, using this c++ example and asking this question helped me understand some new stuff. For example the length of the string even though you dont see the NULL character in the string :D – Ramles Jul 04 '21 at 11:20

0 Answers0