byte

C# - Cast a byte array to an array of struct and vice-versa (reverse)

ε祈祈猫儿з 提交于 2019-12-09 03:41:38
问题 I would like to save a Color[] to a file. To do so, I found that saving a byte array to a file using "System.IO.File.WriteAllBytes" should be very efficient. I would like to cast my Color[] (array of struct) to a byte array into a safe way considering: Potential problem of little endian / big endian (I think it is impossible to happen but want to be sure) Having 2 differents pointer to the same memory which point to different type. Does the garbage collection will know what to do - moving

Get nth byte of integer

瘦欲@ 提交于 2019-12-08 20:13:27
问题 I have the following integer: target = 0xd386d209 print hex(target) How can I print the nth byte of this integer? For example, expected output for the first byte would be: 0x09 回答1: You can do this with the help of bit manipulation. Create a bit mask for an entire byte, then bitshift that mask the number of bytes you'd like. Mask out the byte using binary AND and finally bitshift back the result to the first position: target = 0xd386d209 n = 0 # 0th byte goal = 0xFF << (8 * n) print hex(

How Do I Properly Declare a ctype Structure + Union in Python?

送分小仙女□ 提交于 2019-12-08 19:18:35
问题 I'm messing around with making a binary data parser, and while I could fall back on C, I wanted to see if I could use Python for the task. I have some inkling of how to get this going, and my current implementation looks something like this: from ctypes import * class sHeader(Structure): _fields_ = [("CC", c_uint8, 4), ("AFC", c_uint8, 2), ("TSC", c_uint8, 2), ("PID", c_uint16, 13), ("TP", c_uint16, 1), ("PSI", c_uint16, 1), ("TEI", c_uint16, 1), ("SyncByte", c_uint8)] class Header(Union):

Python 3.4 decode bytes

徘徊边缘 提交于 2019-12-08 18:44:59
问题 I am trying to write a file in python, and I can't find a way to decode a byte object before writing the file, basically, I am trying to decode this bytes string: Les \xc3\x83\xc2\xa9vad\xc3\x83\xc2\xa9s into this, which is the original text I'm trying to recover: Les évadés I tried using the .decode('utf-8') and encode('utf-8') but nothing seems to work... I always get Les évadés as a result... I am using python 3.4.3 Anyone can help? 回答1: And if you want a Python 3 solution: b = b'Les

What is the most suitable type of vector to keep the bytes of a file?

旧巷老猫 提交于 2019-12-08 17:38:11
问题 What is the most suitable type of vector to keep the bytes of a file? I'm considering using the int type, because the bits "00000000" (1 byte) are interpreted to 0! The goal is to save this data (bytes) to a file and retrieve from this file later. NOTE: The files contain null bytes ("00000000" in bits)! I'm a bit lost here. Help me! =D Thanks! UPDATE I: To read the file I'm using this function: char* readFileBytes(const char *name){ std::ifstream fl(name); fl.seekg( 0, std::ios::end ); size_t

how to get byte size of type in generic list?

人盡茶涼 提交于 2019-12-08 16:19:56
问题 I have this generic list and I want to get the byte size of the type like if T is string or int etc., I tried both ways as written in getByteSize(), and just to let you know I am using only one way at a time ... but when I try to compile, it gives an error saying "Error: The type or namespace name 'typeParameterType' could not be found (are you missing a using directive or an assembly reference?)" public class iList<T> : List<T> { public int getByteSize () { // way 1 Type typeParameterType =

Length of byte[] array

徘徊边缘 提交于 2019-12-08 15:53:39
问题 String str = "123456789"; byte[] bytes = str.getBytes(); I want to make the following loop for (int j = 0; j < bytes.length(); j++) { b = bytes[j]; } b will store each byte of my array, but I can't seem to get the length of the array correctly. Error:cannot find symbol Problem solved: bytes.length instead of bytes.length() 回答1: Use bytes.length without the () 回答2: See the JLS - 10.7. Array Members: The members of an array type are all of the following: The public final field length , which

How to find a specific byte in many bytes?

爱⌒轻易说出口 提交于 2019-12-08 14:06:36
问题 I readed a file using Java and use HexDump to output the data. It looks like this: The first and second line: one:31 30 30 31 30 30 30 31 31 30 30 31 30 31 31 31 two: 30 31 31 30 30 31 31 30 31 31 30 30 31 31 30 31 I want to print the data between first "31 30 30 31"and the second "31 30 30 31".My ideal ouput is 31 30 30 31 30 30 30 31 31 30 30 31 30 31 31 31 30 31. But the real output is wrong,I think my code can not find the 31 30 30 31 in the data1.How to figure it out? I Use jdk 1.7 and

How do you combine two bytes in python?

安稳与你 提交于 2019-12-08 13:57:38
问题 Say you have b'\x04' and b'\x00' how can you combine them as b'\x0400' ? 回答1: Using python 3: >>> a = b'\x04' >>> b = b'\x00' >>> a+b b'\x04\x00' 回答2: 0x01 << 8 = 0x0100 0X0100 | 0X01 = 0X0101 You can use this two operation. << 8 for shift 8 bit or one byte | for merging. a = b'0x04' a << 8 b'0x0400' 回答3: It has a simple solution like this: 0x0400 = 0x04 × 256 + 0x00 回答4: In my application I am receiving a stream of bytes from a sensor. I need to combine two of the bytes to create the integer

boto3 S3 Object Parsing

旧街凉风 提交于 2019-12-08 10:50:33
问题 I'm trying to write a Python script for processing audio data stored on S3. I have an S3 object which I'm calling using def grabAudio(filename, directory): obj = s3client.get_object(Bucket=bucketname, Key=directory+'/'+filename) return obj['Body'].read() Accessing the data using print(obj['Body'].read()) yields the correct audio information. So its accessing the data from the bucket just fine. When I try to then use this data in my audio processing library (pydub), it fails: audio =