byte

Can std::byte replace std::aligned_storage?

自古美人都是妖i 提交于 2020-08-07 20:03:36
问题 C++17 introduced a new type, std::byte , so now we finally have a first-class citizen type to represent bytes in memory. Besides being a novelty in the standard, the C++ rules for object creation, start and end of life, aliasing etc. are fairly complicated an unintuitive most of the times, so whenever I feel std::byte is the right tool I also get nervous and reluctant to use it, for fear of unintentionally summoning the Undefined Behavior Balrogs. One such case is a buffer to be used with

Can std::byte replace std::aligned_storage?

浪尽此生 提交于 2020-08-07 20:01:33
问题 C++17 introduced a new type, std::byte , so now we finally have a first-class citizen type to represent bytes in memory. Besides being a novelty in the standard, the C++ rules for object creation, start and end of life, aliasing etc. are fairly complicated an unintuitive most of the times, so whenever I feel std::byte is the right tool I also get nervous and reluctant to use it, for fear of unintentionally summoning the Undefined Behavior Balrogs. One such case is a buffer to be used with

Reading top nibble and bottom nibble in a byte

走远了吗. 提交于 2020-07-30 11:54:09
问题 What's the correct way to handle two distinct values being stored in one byte of data. I have a byte that contains two nibbles each containing their own data. I want to read the top nibble and the bottom nibble into their own variables. 11110000 = High 4 bits throttle, to be read into $throttle , and should be a value from 0 to 15. 00001111 = Low 4 bits brake, to be read into $brake , and should be a value from 0 to 15. Don't forget , drivers can apply the throttle and the brake at the same

Python Byte doesn't print binary

偶尔善良 提交于 2020-07-19 07:42:31
问题 When I print a program such as this in Python: x = b'francis' The output is b'francis' . If bytes is in 0 's and 1 's why is it not printing it out? 回答1: You seem to be fundamentally confused, in a very common way. The data itself is a distinct concept from its representation , i.e. what you see when you attempt to print it out or otherwise display it. There may be multiple ways to represent the same data. This is just like how if I write 23 (in decimal) or 0x17 (hexadecimal) or 0o27 (octal)

C++ Converting Vector<BYTE> to string where first vector byte is 0

爷,独闯天下 提交于 2020-07-08 00:22:50
问题 I am trying to convert a std::vector of BYTES (or unsigned char) to a std::string. The problem I have is when the first element of the vector is a 0, then this returns an empty string after the conversion. I have tried the 2 methods below. Both string1 and string2 return an empty string. The result I am expecting is a string that starts with 2 x 0s followed by several other characters. // vector of BYTE, contains these 7 elements for example: (0,30,85,160,155,93,0) std::vector<BYTE> data; //

Converting a byte array into a hex string

蹲街弑〆低调 提交于 2020-06-27 06:49:09
问题 Surprisingly (to me), this code does not do what I want: fun ByteArray.toHexString() : String { return this.joinToString("") { it.toString(16) } } Turns out Byte is signed , so you get negative hex representations for individual bytes, which leads to a completely bogus end result. Also, Byte.toString won't pad leading zeroes, which you'd want here. What is the simplest (no additional libraries, ideally no extensions) resp. most efficient fix? 回答1: As I am on Kotlin 1.3 you may also be

Converting a byte array into a hex string

两盒软妹~` 提交于 2020-06-27 06:49:08
问题 Surprisingly (to me), this code does not do what I want: fun ByteArray.toHexString() : String { return this.joinToString("") { it.toString(16) } } Turns out Byte is signed , so you get negative hex representations for individual bytes, which leads to a completely bogus end result. Also, Byte.toString won't pad leading zeroes, which you'd want here. What is the simplest (no additional libraries, ideally no extensions) resp. most efficient fix? 回答1: As I am on Kotlin 1.3 you may also be

Python Convert String to Byte

谁都会走 提交于 2020-05-30 08:06:36
问题 I am trying to do some serial input and output operations, and one of those is to send an 8x8 array to an external device (Arduino). The pySerial library requires that the information that I send be a byte. However, in my python code, the 8x8 matrix is made up of types <class 'str'> . Here's my sending function: import serial import Matrix width = 8 height = 8 portName = 'COM3' def sendMatrix(matrix): try: port = serial.Serial(portName, 9600, timeout = 1000000) port.setDTR(0) print("Opened

Python Convert String to Byte

℡╲_俬逩灬. 提交于 2020-05-30 08:06:31
问题 I am trying to do some serial input and output operations, and one of those is to send an 8x8 array to an external device (Arduino). The pySerial library requires that the information that I send be a byte. However, in my python code, the 8x8 matrix is made up of types <class 'str'> . Here's my sending function: import serial import Matrix width = 8 height = 8 portName = 'COM3' def sendMatrix(matrix): try: port = serial.Serial(portName, 9600, timeout = 1000000) port.setDTR(0) print("Opened

C# byte arrays - signed and unsigned dilemma

放肆的年华 提交于 2020-05-16 02:30:39
问题 I start with a signed byte array and convert to unsigned.. so is the printed result correct? byte[] unsigned = new byte[] {10,100,120,180,200,220,240}; sbyte[] signed = Utils.toSignedByteArray(unsigned); And the print (I just append them with a StringBuilder): signed: [10,100,120,-76,-56,-36,-16] unsigned : [10,100,120,180,200,220,240] where: public static sbyte[] toSignedByteArray(byte[] unsigned){ sbyte[] signed = new sbyte[unsigned.Length]; Buffer.BlockCopy(unsigned, 0, signed, 0, unsigned