Skip to content

Instantly share code, notes, and snippets.

@retif
Created May 20, 2011 01:54
Show Gist options
  • Save retif/982205 to your computer and use it in GitHub Desktop.
Save retif/982205 to your computer and use it in GitHub Desktop.
Python For Delphi SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xf4 in position 0: unexpected end of data
//solved with this functions
function StringToWideStringEx(const S: string; CodePage: Word): WideString;
var InputLength,
OutputLength: Integer;
begin
InputLength := Length(S);
OutputLength := MultiByteToWideChar(CodePage, 0, PChar(S), InputLength, nil, 0);
SetLength(Result, OutputLength);
MultiByteToWideChar(CodePage, 0, PChar(S), InputLength, PWideChar(Result), OutputLength);
end;
function WideStringToStringEx(const WS: WideString; CodePage: Word): string;
var
InputLength,
OutputLength: Integer;
begin
InputLength := Length(WS);
OutputLength := WideCharToMultiByte(CodePage, 0, PWideChar(WS), InputLength, nil, 0, nil, nil);
SetLength(Result, OutputLength);
WideCharToMultiByte(CodePage, 0, PWideChar(WS), InputLength, PChar(Result), OutputLength, nil, nil);
end;
Function StringToUnicode(S:String):WideString;
begin
Result:=StringToWideStringEx(S,GetACP);
end;
Function UnicodeToString(S:WideString):string;
begin
Result:=WideStringToStringEx(S,GetACP);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Result : PPyObject;
wStr : WideString;
uStr : UTF8String;
begin
wStr := StringToUnicode(Memo2.Lines[0]);
uStr := UTF8Encode(wStr);
PythonEngine1.ExecString( uStr );
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment