Hex to UTF-8 Conversion Issue C++

Hi all,

I am converting hex to UTF-8 during that i am facing issue.

Input: %3d%22BoatsGreat%22

Output: =?oatsGreat"

Output should be : ="BoatsGreat"


Code:
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>

void show(char* str)
{
while(str && *str)
if(*str != '%')
putwchar(btowc(*str++));
else
putwchar(strtoul(str+1, &str, 16));
}

int main()
{
setlocale(LC_ALL, "");
char str[] = "%3d%22BoatsGreat%22";
show(str);
}
Last edited on
B is a valid hexadecimal digit so strtoul will read all three characters "22B".
Last edited on
@Peter87 any other option by which i can convert hex to UTF-8? as i dont want to convert B also other characters like A,B, C but it should decode.
I don't think there is a similar function that let you specify the number of digits, but I don't know, I'm no expert on C string functions. It shouldn't be that hard to create your own. If you want to use strtoul you could copy the two digits to a null-terminated char array before using the function and then manually increment the str pointer by 2.

Note that UTF-8 uses 8-bit code units so when you're using putwchar, which outputs wide characters, your program is not really outputting UTF-8.
@Peter87 Yes its not outputting UTF-8 conversion really. If you know any code/function can you please tell me by which i can get UTF-8 conversion from Hex?
The title is misleading: based on the desired input/output, you're not working with UTF-8 in any capacity. You're simply reading a byte string where some bytes are encoded as three-byte sequences %xx where xx is hex.

Must it be in C? Here's a convenient way to set it up in C++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
    
void show(const std::string& str)
{
    std::string out;
    parse(str.begin(), str.end(),
          +('%' >> qi::int_parser<int,16,2,2>() | qi::char_), out);
    std::cout << out << '\n';
}

int main()
{
    show("%3d%22BoatsGreat%22");
    show("%22%E7%8C%AB%F0%9F%90%B1%22"); // some UTf-8 for you
}
="BoatsGreat"
"猫🐱"

live demo: http://melpon.org/wandbox/permlink/cASmXQvlpTD997nm

Last edited on
C just uses scanf.

scanf( "%x%x%s", intx, inty, str);

reads into an integer, another integer, and a string from input that is of the format 0x1 0x2 whateverrandomtext

Your input does not make sense to me, so I can't comment on the logic you need.

Last edited on
@cubbi Can you please show me how i can take input from user in your above code?

Because when i am doing it, giving me segmentation fault. its a large string of hex(hex string size greater than 4096) and while taking in cin it does not accept string greater than 4096.
Last edited on
does cin.getline(var, MAXALLOWED) work?
Topic archived. No new replies allowed.