Skip to content

Instantly share code, notes, and snippets.

@shawnfeng0
Last active March 29, 2021 07:53
Show Gist options
  • Save shawnfeng0/25dc434e30d05cff5b355cf6ebd36718 to your computer and use it in GitHub Desktop.
Save shawnfeng0/25dc434e30d05cff5b355cf6ebd36718 to your computer and use it in GitHub Desktop.
wpa_ssid_convert_utf8
unsigned char HexNumber(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else {
return 0;
}
}
std::string WifiSsidConvertUtf8(const std::string &raw) {
std::string result;
for (auto i = 0; i < raw.length(); ++i) {
if ((raw[i] == '\\') && (i + 3 < raw.length()) && (raw[i + 1] == 'x')) {
unsigned char num = HexNumber(raw[i + 2]) * 16 + HexNumber(raw[i + 3]);
if (num) {
result += num;
i += 3;
continue;
}
}
result += raw[i];
}
return result;
}
TEST(t, t) {
std::string wpa_ssid = R"(Huawei-\xe6\xb5\x8b\xe8\xaf\x95)";
std::cout << wpa_ssid << std::endl;
auto res = WifiSsidConvertUtf8(wpa_ssid);
std::cout << res << std::endl;
EXPECT_EQ(res, "Huawei-测试");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment