I have an error message that was built using PyUnicode_FromString. I need to pass this message to PyErr_SetString. Is there an equivalence to PyBytes_AsString for unicode to convert it back to an Ascii character string?
Since you have an object for the message, use PyErr_SetObject() instead.
Related
Python 3
I want to send a binary string over a socket. I am using
socket.send(b"Hello from {0}",client_id)
however I find the it is not properly substitution clientid.
what am I doing wrong? I do I accomplish this in a binary string
{0} is a placeholder for the str.format method, which you should be using, but bytes objects do not have a format method; instead, you can format it as a string first and then convert it to bytes with the encode method:
socket.send("Hello from {0}".format(client_id).encode())
I need to convert a protobuf message to JSON string in java. For this I am using the below API as recommended by the docs (https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/JsonFormat.Printer.html)
String jsonString = JsonFormat.printer().includingDefaultValueFields().print(protobufMessage);
This is working fine for a simple string, however, when my string contains special characters like &, single quote etc. the gson.toJson() method inside JsonFormat is converting special characters to octal format. For example "A&BC" is converted to "A\u0026BC". Also, the resultant string has an extra backslash appended.
So finally "A&BC" is converted to the string "A\\u0026BC".
If it were "A\u0026BC" then I could have converted to a byte array and formed a string with it. But because of the additional backslash I am not able to do so.
Currently I am using protobuf version 3.7.1 and I tried to upgrade and check if any latest API is available, but it did not help. I searched online but did not find any references (a similar issue was reported for JSONFormat.printToString but this API is removed in a later version. https://github.com/carlomedas/protobuf-java-format/issues/16). Can someone please help here if you have come across this issue.
I think the problem might be that you're using that string to pass along, and it's getting parsed a 2nd time. If you use the printer, it will convert "A&BC" to "A\u0026BC". Then when Jackson parses that, it will append the 2nd backslash. To avoid this, you can use #JsonRawValue annotation to avoid being parsed with the 2nd backslash.
Im having trouble concatenating these Unicode emojis with strings in Python3 (to send in Pushwoosh notifications).
Im defining emojis as Unicode variables:
stick_out_tongue = u'U+1F61C'
And then concatenating the string as such:
message = ' Message here...'
message = stick_out_tongue + message
But the output looks turns out like :
'U+1F61C Message here...'
Plz hlp.
Like #lenz said, you are probably looking for "\U0001f61c." That is a specific unicode character code. When you write "u'U+1F61C'" it simply takes the text "U+1F61C" and encodes it in unicode characters. You specify a unicode character code (as apposed to unicode text) by using a "\U." See this tutorial for more information.
In java, we can use the method of String : byte[] getBytes(Charset charset) .
This method Encodes a String into a sequence of bytes using the given charset, storing the result into a new byte array.
But how to do this in GO?
Is there any similar way in Go can do this?
Please let me know it.
The standard Go library only supports Unicode (UTF-8, UTF-16, UTF-32) and ASCII encoding. ASCII is a subset of UTF-8.
The go-charset package (found from here) supports conversion to and from UTF-8 and it also links to the GNU iconv library.
See also field CharsetReader in encoding/xml.Decoder.
I believe here is an answer: https://stackoverflow.com/a/6933412/1315563
There is no way to do it without writing the conversion yourself or
using a third-party package. You could try using this:
http://code.google.com/p/go-charset
I have a php script creating an encoded value, for example:
m>^æ–S[J¯vÖ_ÕÚuÍÔ'´äœÈ‘ ®#M©t²#÷[Éå¹UçfU5T°äÙ“©”ˆÇVÝ] [’e™a«Ã°7#dÉJ>
I then need to decode this in a vb.net application
The problem is that value above can have any characters. And VB.net can't handle it:
dim strCryptedString As String = 'm>^æ–S[J¯vÖ_ÕÚuÍÔ'´äœÈ‘ ®#M©t²#÷[Éå¹UçfU5T°äÙ“©”ˆÇVÝ] [’e™a«Ã°7#dÉJ>"
So any suggestions how to deal with that value?
Try base64encode and base64decode. That may be all that you need!
If you actually need to have it written out in your VB.net source code, you could try base64 encoding it:
dim strCryptedString As String = Base64Decode('bT5ew6bigJNTW0rCr3bDll/DlcOadcONw5QnwrTDpMWTw4jigJggwq5ATcKpdMKyI8O3W8OJw6XCuVXDp2ZVNVTCsMOkw5nigJzCqeKAncuGw4dWw51dIFvigJll4oSiYcKPwqvDg8KwNyNkw4lKPg==');
I'm not sure what the library functions' real names are.
When you read the string, read it into a byte array instead of a string. Then use the numeric value for the characters when you do the decoding.