bytestring

How do I convert a Python 3 byte-string variable into a regular string?

a 夏天 提交于 2019-12-02 14:34:34
I have read in an XML email attachment with bytes_string=part.get_payload(decode=False) The payload comes in as a byte string, as my variable name suggests. I am trying to use the recommended Python 3 approach to turn this string into a usable string that I can manipulate. The example shows: str(b'abc','utf-8') How can I apply the b (bytes) keyword argument to my variable bytes_string and use the recommended approach? The way I tried doesn't work: str(bbytes_string, 'utf-8') Toby Speight You had it nearly right in the last line. You want str(bytes_string, 'utf-8') because the type of bytes

How do I convert a 24-bit integer into a 3-byte array?

可紊 提交于 2019-12-02 03:29:10
问题 Hey Im totally out of my depth and my brain is starting to hurt.. :( I need to covert an integer so that it will fit in a 3 byte array.(is that a 24bit int?) and then back again to send/receive this number from a byte stream through a socket I have: NSMutableData* data = [NSMutableData data]; int msg = 125; const void *bytes[3]; bytes[0] = msg; bytes[1] = msg >> 8; bytes[2] = msg >> 16; [data appendBytes:bytes length:3]; NSLog(@"rtn: %d", [[[NSString alloc] initWithData:data encoding

Haskell: How to use attoparsec in order to read a nested list from a ByteString

为君一笑 提交于 2019-12-01 11:05:15
I have a text file (~ 300 MB large) with a nested list, similar to this one: [[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94, 95], [4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94],[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 85, 87, 92, 93, 94, 95]] Here is my program to read the file into a haskell Integer list: import qualified Data.ByteString as ByteStr main :: IO () -- HOW to do the same thing but using ByteStr.readFile for file access? main = do fContents <- readFile filePath let numList = readNums fContents

Show hex value for all bytes, even when ASCII characters are present

青春壹個敷衍的年華 提交于 2019-12-01 09:21:22
In Python (3) at least, if a binary value has an ASCII representation, it is shown instead of the hexadecimal value. For instance, the binary value of 67 which is ASCII C is show as follows: bytes([67]) # b'C' Whereas for binary values without ASCII representations, they are shown in hex. I.E. b'\x0f' Is there a way to force Python to show the binary values in their binary-hex form (if this is what it is called), even when there are ASCII representations? Edit: By this I mean, something that starts with b'\x' ,. This would make debugging easier when you are looking for specific bytes to be

Haskell: How to use attoparsec in order to read a nested list from a ByteString

妖精的绣舞 提交于 2019-12-01 08:37:40
问题 I have a text file (~ 300 MB large) with a nested list, similar to this one: [[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94, 95], [4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94],[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 85, 87, 92, 93, 94, 95]] Here is my program to read the file into a haskell Integer list: import qualified Data.ByteString as ByteStr main :: IO () -- HOW to do the same thing but using ByteStr

How to read a utf-8 encoded binary string in tensorflow?

帅比萌擦擦* 提交于 2019-12-01 08:29:16
问题 I am trying to convert an encoded byte string back into the original array in the tensorflow graph (using tensorflow operations) in order to make a prediction in a tensorflow model. The array to byte conversion is based on this answer and it is the suggested input to tensorflow model prediction on google cloud's ml-engine. def array_request_example(input_array): input_array = input_array.astype(np.float32) byte_string = input_array.tostring() string_encoded_contents = base64.b64encode(byte

vb.net - Encode string to UTF-8

故事扮演 提交于 2019-12-01 07:44:07
问题 I've made a class to encode a string Public Class UTF8 Public Shared Function encode(ByVal str As String) Dim utf8Encoding As New System.Text.UTF8Encoding Dim encodedString() As Byte encodedString = utf8Encoding.GetBytes(str) Return encodedString.ToString() End Function End Class Return encodedString.ToString() always returns "System.Byte[]". How could I get the real UTF-8 String? 回答1: Use UTF8.GetString(Byte[]) method. 回答2: We can check if a string is UTF-8 by examining the string BOM value.

Haskell How to convert Char to Word8

本小妞迷上赌 提交于 2019-11-30 16:27:48
问题 I want to split ByteString to words like so: import qualified Data.ByteString as BS main = do input <- BS.getLine let xs = BS.split ' ' input But it appears that GHC can't convert a character literal to Word8 by itself, so I got: Couldn't match expected type `GHC.Word.Word8' with actual type `Char' In the first argument of `BS.split', namely ' ' In the expression: BS.split ' ' input Hoogle doesn't find anything with type signature of Char -> Word8 and Word.Word8 ' ' is invalid type

Making a single function work on lists, ByteStrings and Texts (and perhaps other similar representations)

折月煮酒 提交于 2019-11-30 14:59:05
问题 I'm writing a function that does some searching in a sequence of arbitrary symbols. I'd like to make it generic enough so that it works on lists, Foldable s as well on ByteString s and Text s. Generalizing it to Foldable is simple. But how to include ByteString s and Text s? Sure I could convert ByteString into a list and then call my function, but I'd lose all the advantages ByteString s. To have a concrete example let's say we want to make a histogram function: import Control.Monad.State

Making a single function work on lists, ByteStrings and Texts (and perhaps other similar representations)

∥☆過路亽.° 提交于 2019-11-30 13:06:29
I'm writing a function that does some searching in a sequence of arbitrary symbols. I'd like to make it generic enough so that it works on lists, Foldable s as well on ByteString s and Text s. Generalizing it to Foldable is simple. But how to include ByteString s and Text s? Sure I could convert ByteString into a list and then call my function, but I'd lose all the advantages ByteString s. To have a concrete example let's say we want to make a histogram function: import Control.Monad.State import qualified Data.Foldable as F import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map