{"id":3249,"date":"2020-04-30T17:46:57","date_gmt":"2020-04-30T17:46:57","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=3249"},"modified":"2020-04-30T17:47:24","modified_gmt":"2020-04-30T17:47:24","slug":"utf8-string-with-unicode-to-hex","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/strings\/stringstream\/examples\/utf8-string-with-unicode-to-hex","title":{"rendered":"UTF8 string with unicode to hex"},"content":{"rendered":"\n<p>Convert UTF8 string to a hex encoded string sutiable for use as a SSID hex encoded string<\/p>\n\n\n\n<p>Normal characters are converted into 2x hex digits<br>Unicode encoded characters using \\x are retained as hex characters<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/SSID\n\t\/\/output_file &lt;&lt; \"\tssid=\\\"\" &lt;&lt; ssid &lt;&lt; \"\\\"\\n\";\n\t\/\/Using the above will output any unicode characters as UTF8, but wpa_supplicant.conf will not interpret them correctly, it uses UTF16.\n\t\/\/This is a problem as SSID can contain unicode chaaracters, for instance the iPhone will have a SSID of \"Adam\\xE2\\x80\\x99s iPhone\" for \"Adam\u2019s iPhone\".\n\t\/\/When performing \"sudo iwlist wlan0 scan\" you will get \\x values returned, so we have to convert them somehow.\n\t\/\/To support GCC 4.9 without built in UTF8 to UTF16 conversion functions we instead handle this by storing the SSID as hex characters.\n\t\/\/In wpa_supplicant.conf if the SSID is surrounded by quotes \"\" then its dealt with as a string, but otherwise it is dealt with as\n\t\/\/2 digit hex characters.\n\toutput_file &lt;&lt; \"\tssid=\";\n\n\tfor (Index = 0; Index &lt; ssid.length(); Index++)\n\t{\n\t\tif ((ssid&#91;Index] == '\\\\') &amp;&amp; (ssid&#91;(Index+1)] == 'x'))\n\t\t{\n\t\t\tIndex++;\n\t\t\tIndex++;\n\t\t\toutput_file &lt;&lt; (char)ssid&#91;Index++];\n\t\t\toutput_file &lt;&lt; (char)ssid&#91;Index];\n\t\t}\n\t\telse\n\t\t{\n\t\t\toutput_file &lt;&lt; hex &lt;&lt; uppercase &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; (int)ssid&#91;Index];\n\t\t}\n\t}\t\t\n\toutput_file &lt;&lt; \"\\n\";<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Convert UTF8 string to a hex encoded string sutiable for use as a SSID hex encoded string Normal characters are converted into 2x hex digitsUnicode encoded characters using \\x are retained as hex characters<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[157],"tags":[],"class_list":["post-3249","post","type-post","status-publish","format-standard","hentry","category-examples"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=3249"}],"version-history":[{"count":2,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3249\/revisions"}],"predecessor-version":[{"id":3251,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3249\/revisions\/3251"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=3249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=3249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=3249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}