编码的过程中,只针对UTF-8字符(!至~)进行处理,所以需要实现字符过滤功能筛选出非特殊字符。
//只对非UTF-8字符进行编码
static const char *hex = "0123456789abcdef";
if(c > '!' && c <= '~') {
ret.push_back(c);
} else {
ret.append("\\x");
unsigned char d = c;
ret.push_back(hex[d >> 4]);
ret.push_back(hex[d & 0x0f]);
}