byte

C++ offsetof char* arithmetic

删除回忆录丶 提交于 2019-12-12 10:32:33
问题 In this answer, int8_t* is used for (byte) pointer arithmetic: std::size_t offset = offsetof(Thing, b); Thing* thing = reinterpret_cast<Thing*>(reinterpret_cast<int8_t*>(ptr) - offset); I've always used char* in the past but the comments are really confusing, and nobody responded, so I posted this separate question. Is char* valid, and the preferred way of doing these calculations? 回答1: You must use char* : the behaviour on using a reinterpret_cast with int8_t* on a pointer to something that

Fields in a struct skipping bytes

送分小仙女□ 提交于 2019-12-12 08:56:32
问题 I have a struct I have written which is supposed to represent an entire UDP packet, with the ethernet header and all. Here it is: #pragma pack(1) struct UDPPacket { // an array to hold the destination mac address of the packet unsigned char dstmac[6]; // an array to hold the source mac address of the packet unsigned char srcmac[6]; // two bytes to hold the packet type, this is almost always IP (08 00) WORD ethtype; // each of the subfields of this take up 4 bits. ver, the first half, // is

Int16 - bytes capacity in.net?

帅比萌擦擦* 提交于 2019-12-12 08:07:45
问题 Why does : short a=0; Console.Write(Marshal.SizeOf(a)); shows 2 But if I see the IL code i see : /*1*/ IL_0000: ldc.i4.0 /*2*/ IL_0001: stloc.0 /*3*/ IL_0002: ldloc.0 /*4*/ IL_0003: box System.Int16 /*5*/ IL_0008: call System.Runtime.InteropServices.Marshal.SizeOf /*6*/ IL_000D: call System.Console.Write The LDC at line #1 indicates : Push 0 onto the stack as int32 . So there must been 4 bytes occupied. But sizeOf shows 2 bytes... What am I missing here ? how many byte does short actually

Python write string of bytes to file

十年热恋 提交于 2019-12-12 07:17:52
问题 How do I write a string of bytes to a file, in byte mode, using python? I have: ['0x28', '0x0', '0x0', '0x0'] How do I write 0x28, 0x0, 0x0, 0x0 to a file? I don't know how to transform this string to a valid byte and write it. 回答1: Map to a bytearray() or bytes() object, then write that to the file: with open(outputfilename, 'wb') as output: output.write(bytearray(int(i, 16) for i in yoursequence)) Another option is to use the binascii.unhexlify() function to turn your hex strings into a

PHP convert KB MB GB TB etc to Bytes

情到浓时终转凉″ 提交于 2019-12-12 07:15:12
问题 I'm asking how to convert KB MB GB TB & co. into bytes. For example: byteconvert("10KB") // => 10240 byteconvert("10.5KB") // => 10752 byteconvert("1GB") // => 1073741824 byteconvert("1TB") // => 1099511627776 and so on... EDIT: wow. I've asked this question over 4 years ago. Thise kind of things really show you how much you've improved over time! 回答1: Here's a function to achieve this: function convertToBytes(string $from): ?int { $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; $number =

Bits to Byte in C++

假如想象 提交于 2019-12-12 06:45:45
问题 I'm trying to: convert a group of 8 integers, all of value 0 or 1, into a byte reverse the bit order of that byte print the value of that byte (in what format?) ( i can guess until i have it right here ) Also, I'm not allowed to use the STL for this problem. 回答1: So, you want to reverse the bits in a byte. That is, the bits should move so: from: 7 6 5 4 3 2 1 0 to: 0 1 2 3 4 5 6 7 This code will do it, inelegantly - you can find much better algorithms if you search. Can you see how it works

Keep Track of Number of Bytes Read

风格不统一 提交于 2019-12-12 05:39:24
问题 I would like to implement a command line progress bar for one of my programs IN PYTHON which reads text from a file line by line. I can implement the progress scale in one of two ways: (number of lines / total lines) or (number of bytes completed / bytes total) I don't care which, but "number of lines" would seem to require me to loop through the entire document (which could be VERY large) just to get the value for "total lines" . This seems extremely inefficient. I was thinking outside the

Convert bytes to float values

时光总嘲笑我的痴心妄想 提交于 2019-12-12 05:32:27
问题 I need to convert 28 byte value to a 7 float values, each 4 bytes denote 1 float value. I have to implement this in C#. I searched for lot of solutions, but most of them were in C/Java. 回答1: Use System.BitConverter.ToSingle(..) Like this: float f1,f2,f3,f4,f5,f6,f7; f1 = System.BitConverter.ToSingle(bytearr,0); f2 = System.BitConverter.ToSingle(bytearr,4); f3 = System.BitConverter.ToSingle(bytearr,8); ... f7 = System.BitConverter.ToSingle(bytearr,24); 回答2: Like so: byte b; float f; f = (float

How can I convert little endian to big endian using htonl

江枫思渺然 提交于 2019-12-12 05:27:22
问题 I have struct with the following elements. Plus the structure is complety padded. typedef struct { uint16_t a; uint16_t b; uint8_t c; uint8_t d; uint8_t e[6]; } ad; This structure is a little endian. I mean when I print this structure on my big endian machine I get the following if c=1 , d=2, e[0] =3, e[1]=4. I get c=4, d=3, e[0] = 2 and e[1]=1. a and b are swapped. further, e[1] is swapped with c and e[0] is swapped with d. I am using htonl function like the following. but, it is not working

Fix progress report event in this method

ⅰ亾dé卋堺 提交于 2019-12-12 04:55:41
问题 I've tried to write a method that splits a file into smaller parts (I call those parts 'chunks'), I use a buffer of 1 MB to read/write the chunks and I'm reporting the operation progress percentage and the current chunk progress percent. The problem is this, I have a big file of 6,74 GB and I will split it into chunks of 1 GB each one, the progress report works as expected but not for the last chunk where the progress up only to 75% 'cause of course the last part is around 750 mb and not 1 GB