struct

Python struct.unpack errors with TypeError: a bytes-like object is required, not 'str'

不羁的心 提交于 2019-12-24 16:52:15
问题 Can someone please help with the following line of code and Error? I am unfamiliar with python value conversions. The specific line that generates the error is: value = struct.unpack("<h",chr(b)+chr(a))[0] TypeError: a bytes-like object is required, not 'str' The code fragment is: if packet_code ==0x80: # raw value row_length = yield a = yield b = yield value = struct.unpack("<h",chr(b)+chr(a))[0] The input data is: b'\x04\x80\x02\x00\xb2\xcb\xaa\xaa\x04\x80\x02\x00p\r\xaa\xaa\x04\x80\x02\x00

In Swift, can one use a string to access a struct property?

纵饮孤独 提交于 2019-12-24 16:46:16
问题 I have a struct and I would like to know if I can access variables using bracket syntax. Here is my struct: import UIKit public struct Pixel { public var value: UInt32 public var red: UInt8 public var green: UInt8 public var blue: UInt8 public var alpha: UInt8 } public struct RGBAImage { public var pixels: [ImageProcessor_Sources.Pixel] public var width: Int public var height: Int public init?(image: UIImage) public func toUIImage() -> UIImage? } I would like to access the variable like so

Initializing an array of structs, why does using struct variables not work

假装没事ソ 提交于 2019-12-24 15:26:46
问题 I am extremely new to C. Would appreciate if someone can help understand why code in lines 13,14 and 16 does not work, but lines 17-20 works. With the first option (lines 13, 14 and 16) I get the error error: initializer element is not constant What does this mean? Also, does this mean one cannot use variables of certain type to generate new variables? Thank you. // Define structure for a good 5 struct good { 6 char goodname; 7 double p; //starting proportion 8 int theta; //average utility 9

Possible to make a singleton struct in C++? How?

左心房为你撑大大i 提交于 2019-12-24 15:12:07
问题 I like to experiment around as I learn more about coding. I have a program that would only require a single instance of a struct for the life of it's runtime and was wondering if it's possible to create a singleton struct. I see lot's of information on making a singleton class on the internet but nothing on making a singleton struct. Can this be done? If so, how? Thanks in advance. Oh, and I work in C++ btw. 回答1: Struct and class are in C++ almost the same (the only difference is default

Convert integer to big endian binary file in python

[亡魂溺海] 提交于 2019-12-24 14:37:21
问题 I'm trying to convert a 2D-array composed by integers to a big endian binary file using Python by this way: import struct; fh=open('file.bin','wb') for i in range(width): for j in range(height): fh.write(struct.pack('>i2',data[i,j])) fh.close() when I open it with numpy: a=np.fromfile('file.bin',dtype='>i2') The result is an array with zeros between original data: [266, 0, 267, 0, 268, 0, 272, 0, 268, 0, 264, 0, 266, 0, 264, 0, 263, 0, 263, 0, 267, 0, 263, 0, 265, 0, 266, 0, 266, 0, 267, 0,

Can I use WinDbg to dump structs for other platforms?

风流意气都作罢 提交于 2019-12-24 14:10:59
问题 I'm using WinDbg to dump struct information, for example: lkd> dt nt!_LIST_ENTRY +0x000 Flink : Ptr32 _LIST_ENTRY +0x004 Blink : Ptr32 _LIST_ENTRY The example above shows the 32-bit version of the _LIST_ENTRY struct. The 64-bit version is larger, with Blink at 0x008 . I understand that I can debug remote systems or analyze crash dumps, as long as I load the correct symbols for those Windows versions. My question: Is it possible to dump structs for different Windows versions than the one I am

Trouble referencing class in structure using this keyword

别来无恙 提交于 2019-12-24 13:33:51
问题 Header file: // Free function to use in thread unsigned int __stdcall WorkerFunction(void *); class MyClass { public: int temp; void StartThread(); } typedef struct { MyClass * cls; } DATA; CPP class: void MyClass::StartThread() { temp = 1234; DATA data = {this}; HANDLE hThread = (HANDLE) _beginthreadex(0, 0, &WorkerFunction, &data, 0, 0); // Commented out for now while I address the current problem //CloseHandle(hThread); } unsigned int __stdcall WorkerFunction(void * param0) { MessageBox

C# - struct serialization

我的梦境 提交于 2019-12-24 13:16:47
问题 I have got a small class that I would like to use for serializing structs. I would like to know two things: Performance issue. Since I am passing Object - it passes just a reference, not a copy of the struct? And since I am returning object of type T it also only passes a reference? Correctness issue. Will this serialization work for all structs? I mean - is there a possibility where those methods will not work? public static byte[] ToByteArray(Object obj) { int size = Marshal.SizeOf(obj);

Changing a variable from global to local - C

吃可爱长大的小学妹 提交于 2019-12-24 12:56:28
问题 I have written the following code for a project however it fails a singular test asking for two variables to not be global, and instead be local to main() . Modify structexample1.c so that the variables student and anotherStudent are not global but are local to main. I vaguely understand the local and global concept but I am unsure how to implement what the question is asking into the code I have written. #include <stdio.h> #include <stdlib.h> struct student_s { char* name; int age; double

Pointer to self struct in C

∥☆過路亽.° 提交于 2019-12-24 12:55:34
问题 Given the following code: typedef struct elementT { int data; struct elementT *next; } element; Why is it necessary to do struct elementT *next and I can't do element *next inside the struct declaration itself? Is it because it is not declared yet? 回答1: The typedef only takes place after the struct has been defined. Consider the following code; the syntax is invalid, but it hopes it shows the order/precedence of things: typedef (struct { int field1, field2; }) item; I.e., the struct{...} is