How to convert a C string (char array) into a Python string when there are non-ASCII characters in the string?
By : Samael
Date : March 29 2020, 07:55 AM
wish helps you 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? , PyString_Decode does this: code :
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;
}
#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;
}
|
ASCII codes => string of characters
By : Ghazi Abidi
Date : March 29 2020, 07:55 AM
I wish this helpful for you You need to turn it into a corresponding byte array and then instantiate new String(byteArray). code :
String [] strings = input.substring(1, input.length()-1).split(",");
byte[] bytes = new byte[strings.length];
int i = 0;
for (String s : strings) bytes[i++] = Byte.parseByte(s);
System.out.println(new String(bytes, "UTF-8"));
|
Convert string with hex ASCII codes to characters
By : IndiAble Rstar
Date : March 29 2020, 07:55 AM
I hope this helps you . You can use Array#pack: code :
["666f6f626172"].pack('H*')
#=> "foobar"
|
Convert ascii character codes into letters in a string containing text and ascii codes in JS
By : Sumit Sharma
Date : November 21 2020, 11:01 PM
like below fixes the issue I am working on an HTML web page, and in the js, I read the end of the URL and place it in a div e.g. if the URL is , Use decodeURIComponent code :
var a = decodeURIComponent('Hello,%20world');
a = 'Hello, world!';
|
Remove all characters with ascii codes 32 - 47 and some more from string
By : TheDogtoy
Date : March 29 2020, 07:55 AM
help you fix your problem I want to remove all characters with ascii codes 32 - 47 and some more from string. Exactly !"#$%&'()*+,-./\~. , To use the characters just include them in a character class: code :
$string = preg_replace(':[\s!"#$%&\'()*+,-./\\\~]:', '', $string);
[\x20-\x2f\x5c\x7e]
[ -/\\\~]
|