将int8数组解码为utf-8字符串

时间:2019-03-02 20:16:29

标签: python

如果我有一个像这样的int8数组:

array('b', [-20, -107, -124, -21, -123, -107])

我想将此数组解码为utf-8字符

例如,上面的数组为\xec\x95\x88\xeb\x85\x95以十六进制表示(朝鲜语)

解决这个问题的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用program Project1; uses Windows, SysUtils, WinSock; function GetIPFromHostName(const HostName: string): string; var WSAData: TWSAData; R: PHostEnt; A: TInAddr; begin Result := '0.0.0.0'; WSAStartup($101, WSAData); R := GetHostByName(PAnsiChar(AnsiString(HostName))); if Assigned(R) then begin A := PInAddr(R^.h_Addr_List^)^; Result := WinSock.inet_ntoa(A); end; end; function IsConnectedToInternet: Boolean; var WSAData1: TWSAData; SockAddr1: TSockAddr; Socket1: TSocket; begin Result := False; if WSAStartup(MAKEWORD(2, 2), WSAData1) <> 0 then begin Writeln('WinSock error'); Exit; end; Socket1 := Socket(AF_INET, SOCK_STREAM, 0); if Socket1 = INVALID_SOCKET then begin Writeln('socket error'); Exit; end; SockAddr1.sin_family := AF_INET; SockAddr1.sin_addr.S_addr := inet_addr(PAnsiChar(GetIPFromHostName('google.com'))); SockAddr1.sin_port := htons(80); if Connect(Socket1, SockAddr1, SizeOf(SockAddr1)) <> 0 then begin Writeln('connection error #', WSAGetLastError); Exit; end; Result := True; if CloseSocket(Socket1) <> 0 then Writeln('error closing socket'); end; begin IsConnectedToInternet; readln; end.

.tobytes

对于>>> from array import array >>> arr = array('b', [-20, -107, -124, -21, -123, -107]) >>> arr.tobytes() b'\xec\x95\x84\xeb\x85\x95' ,您只需解码字节:

str