wpa_supplicant 中文SSID連線不上
在wpa_supplicant中scan,獲取的SSID,可以正常顯示中文,通過wpa_cli獲取到SSID,它會把中文GBK編碼轉換成16進位制的字串,
不經過轉換,這時我們配置WIFI,就會把16進位制字串的SSID,傳給wpa_supplicant,就會導致連線不上(因為SSID都不對)
所以我們獲取到中文的SSID,需要轉換,變為GBK編碼
思路:把16進位制字串轉換成字元,然後再組合在一起
程式碼參考:
/* 十六進位制字串轉換為位元組流 * eg:e8(16進位制,2個位元組)---》轉變為e8(一個位元組) */ static void HexStrToByte(const char* source, unsigned char* dest, int sourceLen) { short i; unsigned char highByte, lowByte; for (i = 0; i < sourceLen; i += 2) { highByte = toupper(source[i]); lowByte = toupper(source[i + 1]); if (highByte > 0x39) highByte -= 0x37; else highByte -= 0x30; if (lowByte > 0x39) lowByte -= 0x37; else lowByte -= 0x30; dest[i / 2] = (highByte << 4) | lowByte; } return ; } /* 字串拆開,重新組裝 * eg: ab\xe8\x80\x83\xe5\x8b\xa4轉換為 * ab鑰冨嫟(即ab考勤) */ static void string_reerect(char *src, char *destBuf) { int i = 0; int j = 0; while(src[j] != '\0') { if(strncmp(&src[j], "\\x", 2) != 0) { memcpy(&destBuf[i], &src[j], 1); i = i + 1; j = j + 1; } else { HexStrToByte(&src[j+2], &destBuf[i], 2); i = i+1; j = j+4; } } destBuf[i] = '\0'; return ; }