byte

Different interpretation of hex strings in python

老子叫甜甜 提交于 2020-04-18 06:08:40
问题 In the past few days, I've been struggling to understand why this piece of codes behaves in such a way: code: file1 = open("input.txt","r") M = file1.read() file1.close() print(M) print(M.encode("latin")) print(type(M.encode("latin"))) print("\n-----------------------------\n") t = "\xAC\x42\x4C\x45\x54\x43\x48\x49\x4E\x47\x4C\x45\x59" print(t) print(t.encode("latin")) print(type(t.encode("latin"))) file "input.txt" content: \xAC\x42\x4C\x45\x54\x43\x48\x49\x4E\x47\x4C\x45\x59 output: \xAC

Different interpretation of hex strings in python

吃可爱长大的小学妹 提交于 2020-04-18 06:08:25
问题 In the past few days, I've been struggling to understand why this piece of codes behaves in such a way: code: file1 = open("input.txt","r") M = file1.read() file1.close() print(M) print(M.encode("latin")) print(type(M.encode("latin"))) print("\n-----------------------------\n") t = "\xAC\x42\x4C\x45\x54\x43\x48\x49\x4E\x47\x4C\x45\x59" print(t) print(t.encode("latin")) print(type(t.encode("latin"))) file "input.txt" content: \xAC\x42\x4C\x45\x54\x43\x48\x49\x4E\x47\x4C\x45\x59 output: \xAC

Calculate the memory usage and improve computation performance

喜欢而已 提交于 2020-04-18 03:48:50
问题 I would like to optimize this code in order to save memory as much as possible. My questions are: How many bytes are currently occupied for this? How many bytes can be saved by new changes? and what are those changes? The /Ox compiler option enables a combination of optimizations that favor speed. In some versions of the Visual Studio IDE and the compiler help message, this is called full optimization, but the /Ox compiler option enables only a subset of the speed optimization options enabled

Calculate the memory usage and improve computation performance

狂风中的少年 提交于 2020-04-18 03:47:14
问题 I would like to optimize this code in order to save memory as much as possible. My questions are: How many bytes are currently occupied for this? How many bytes can be saved by new changes? and what are those changes? The /Ox compiler option enables a combination of optimizations that favor speed. In some versions of the Visual Studio IDE and the compiler help message, this is called full optimization, but the /Ox compiler option enables only a subset of the speed optimization options enabled

How to convert Hex char into bytes like \x90\x90?

邮差的信 提交于 2020-03-26 04:46:22
问题 I am searching a way to convert hex char to bytes like \x90\x0d\x41 and when I use printf() , binary data are printed? char *hex = "909090904241"; when I need to get \x90\x90\x90\x90\x42\x42 and when I print I get binary data. 回答1: Loop through the string and append to a new string a piece with \x added infront like this: const char *hex = "909090904241"; char* result = malloc((strlen(hex) * 2 + 1)* sizeof(char)); result[0] = 0; char piece[] = "\\x00"; for (int i = 0; i < strlen(hex); i+=2) {

Python: Transferring two byte variables with a binary file

帅比萌擦擦* 提交于 2020-03-25 05:51:54
问题 Let's say I have two bytearray, b = bytearray(b'aaaaaa') b1 = bytearray(b'bbbbbb') file_out = open('bytes.bin', 'ab') file_out.write(b) file_out.write(b1) this code will create a .bin file which contains two bytearrays how to read this file and store those two variables also decode them back to string? my goal is to transfer these bytes for other programs to read by making a file. I'm not sure if this bytearray + appending is a good idea. Thanks 回答1: Pythons pickle is meant for storing and

How compile time constant will work internally in java

本小妞迷上赌 提交于 2020-03-23 14:33:15
问题 My question is how compile time constant works internally so we didn't get an error in Below statement. final int a = 10; byte b = a; And why am I getting error in this statement. int a = 10; byte b = a; 回答1: This is because not all ints will fit into a byte. In your first example, the value of a is known and cannot change. The compiler knows that it will fit into a byte. In your second example, because a is not final, it's possible that it could have been changed (though not in your example)

Fastest Method to Split a 32 Bit number into Bytes in C++

笑着哭i 提交于 2020-03-16 06:37:27
问题 I am writing a piece of code designed to do some data compression on CLSID structures. I'm storing them as a compressed stream of 128 bit integers. However, the code in question has to be able to place invalid CLSIDs into the stream. In order to do this, I have left them as one big string. On disk, it would look something like this: +--------------------------+-----------------+------------------------+ | | | | | Length of Invalid String | Invalid String | Compressed Data Stream | | | | | +--

Python bytes literal has extra characters that aren't hex, but alter the value of the string

老子叫甜甜 提交于 2020-03-10 04:58:46
问题 I am used to the python byte literal syntax representing bytes as hex values such as b'\x7a' is the hex value 0x7a . I however have run into an issue that I don't know how it is working. I am using the ssl library for the first time and creating random values with ssl.RAND_bytes(...) It is returning strings with characters that alter the value (say when doing an int.from_bytes(...) ) Example strings I have received: b'\x12\x1f)\x8b\xe0\xd7LD' b'\x808\x8a(\x02\xb3S\xc9\xabW_\n\xf3\xbb\x80o' b'

convert a java.net.InetAddress to a long

自闭症网瘾萝莉.ら 提交于 2020-02-22 05:06:12
问题 I would like to convert a java.net.InetAddress and I fight with the signed / unsigned problems. Such a pain. I read convert from short to byte and viceversa in Java and Why byte b = (byte) 0xFF is equals to integer -1? And as a result came up with: final byte [] pumpeIPAddressRaw = java.net.InetAddress.getByName (pumpeIPAddressName).getAddress (); final long pumpeIPAddress = ((pumpeIPAddressRaw [0] & 0xFF) << (3*8)) + ((pumpeIPAddressRaw [1] & 0xFF) << (2*8)) + ((pumpeIPAddressRaw [2] & 0xFF)