Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 68583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:26:21+00:00 2026-05-10T19:26:21+00:00

I have embedded a Python interpreter in a C program. Suppose the C program

  • 0

I have embedded a Python interpreter in a C program. Suppose the C program reads some bytes from a file into a char array and learns (somehow) that the bytes represent text with a certain encoding (e.g., ISO 8859-1, Windows-1252, or UTF-8). How do I decode the contents of this char array into a Python string?

The Python string should in general be of type unicode—for instance, a 0x93 in Windows-1252 encoded input becomes a u'\u0201c'.

I have attempted to use PyString_Decode, but it always fails when there are non-ASCII characters in the string. Here is an example that fails:

#include <Python.h> #include <stdio.h>  int main(int argc, char *argv[]) {      char c_string[] = { (char)0x93, 0 };      PyObject *py_string;       Py_Initialize();       py_string = PyString_Decode(c_string, 1, 'windows_1252', 'replace');      if (!py_string) {           PyErr_Print();           return 1;      }      return 0; } 

The error message is UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 0: ordinal not in range(128), which indicates that the ascii encoding is used even though we specify windows_1252 in the call to PyString_Decode.

The following code works around the problem by using PyString_FromString to create a Python string of the undecoded bytes, then calling its decode method:

#include <Python.h> #include <stdio.h>  int main(int argc, char *argv[]) {      char c_string[] = { (char)0x93, 0 };      PyObject *raw, *decoded;       Py_Initialize();       raw = PyString_FromString(c_string);      printf('Undecoded: ');      PyObject_Print(raw, stdout, 0);      printf('\n');      decoded = PyObject_CallMethod(raw, 'decode', 's', 'windows_1252');      Py_DECREF(raw);      printf('Decoded: ');      PyObject_Print(decoded, stdout, 0);      printf('\n');      return 0; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:26:22+00:00Added an answer on May 10, 2026 at 7:26 pm

    PyString_Decode does this:

    PyObject *PyString_Decode(const char *s,               Py_ssize_t size,               const char *encoding,               const char *errors) {     PyObject *v, *str;      str = PyString_FromStringAndSize(s, size);     if (str == NULL)     return NULL;     v = PyString_AsDecodedString(str, encoding, errors);     Py_DECREF(str);     return v; } 

    IOW, it does basically what you’re doing in your second example – converts to a string, then decode the string. The problem here arises from PyString_AsDecodedString, rather than PyString_AsDecodedObject. PyString_AsDecodedString does PyString_AsDecodedObject, but then tries to convert the resulting unicode object into a string object with the default encoding (for you, looks like that’s ASCII). That’s where it fails.

    I believe you’ll need to do two calls – but you can use PyString_AsDecodedObject rather than calling the python ‘decode’ method. Something like:

    #include <Python.h> #include <stdio.h>  int main(int argc, char *argv[]) {      char c_string[] = { (char)0x93, 0 };      PyObject *py_string, *py_unicode;       Py_Initialize();       py_string = PyString_FromStringAndSize(c_string, 1);      if (!py_string) {           PyErr_Print();           return 1;      }      py_unicode = PyString_AsDecodedObject(py_string, 'windows_1252', 'replace');      Py_DECREF(py_string);       return 0; } 

    I’m not entirely sure what the reasoning behind PyString_Decode working this way is. A very old thread on python-dev seems to indicate that it has something to do with chaining the output, but since the Python methods don’t do the same, I’m not sure if that’s still relevant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 63k
  • Answers 63k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I believe the Java EE Java stack is actually very… May 11, 2026 at 10:25 am
  • added an answer Not being much of a statistics guy, the mode in… May 11, 2026 at 10:25 am
  • added an answer This means that the application is unable to load the… May 11, 2026 at 10:25 am

Related Questions

I have embedded a Python interpreter in a C program. Suppose the C program
If I have an embedded browser in a windows form, is it possible to
Basically, I have an iframe embedded in a page and the iframe has some
I have an embedded webserver that has a total of 2 Megs of space
I have a hidden embedded QuickTime object on my page that I'm trying to
I have a .ico file that is embedded as a resource (build action set
I have a link in a pdf-document (PDF1), which is embedded in a browser
I have a class with many embedded assets. Within the class, I would like
I have a real-time embedded app with the major cycle running at 10KHz. It
I have a web reference for our report server embedded in our application. The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.