How many bytes will a string take up?

From my article on strings: In the current implementation at least, strings take up 20+(n/2)*4 bytes (rounding the value of n/2 down), where n is the number of characters in the string. The string type is unusual in that the size of the object itself varies. The only other classes which do this (as far … Read more

convert Byte Array to Secret Key

You need to use the new keyword to call the constructor and create the object. SecretKey originalKey = new SecretKeySpec(encodedKey, 0, encodedKey.length, “AES”); When you try to call it without new, the compiler thinks it might be a method you’ve defined inside that class, hence your error message.

How to download Postgres bytea column as file

One simple option is to use COPY command with encode to hex format and then apply xxd shell command (with -p continuous hexdump style switch). For example let’s say I have jpg image in bytea column in samples table: \copy (SELECT encode(file, ‘hex’) FROM samples LIMIT 1) TO ‘/home/grzegorz/Desktop/image.hex’ $ xxd -p -r image.hex > … Read more

How do I ‘declare’ an empty bytes variable?

Just use an empty byte string, b”. However, concatenating to a string repeatedly involves copying the string many times. A bytearray, which is mutable, will likely be faster: msg = bytearray() # New empty byte array # Append data to the array msg.extend(b”blah”) msg.extend(b”foo”) To decode the byte array to a string, use msg.decode(encoding=’utf-8′).

Why does a byte only have 0 to 255?

Strictly speaking, the term “byte” can actually refer to a unit with other than 256 values. It’s just that that’s the almost universal size. From Wikipedia: Historically, a byte was the number of bits used to encode a single character of text in a computer and it is for this reason the basic addressable element … Read more

“TypeError: a bytes-like object is required, not ‘str'” when handling file content in Python 3

You opened the file in binary mode: with open(fname, ‘rb’) as f: This means that all data read from the file is returned as bytes objects, not str. You cannot then use a string in a containment test: if ‘some-pattern’ in tmp: continue You’d have to use a bytes object to test against tmp instead: … Read more

Converting char array into byte array and back again

Conversion between char and byte is character set encoding and decoding.I prefer to make it as clear as possible in code. It doesn’t really mean extra code volume: Charset latin1Charset = Charset.forName(“ISO-8859-1”); charBuffer = latin1Charset.decode(ByteBuffer.wrap(byteArray)); // also decode to String byteBuffer = latin1Charset.encode(charBuffer); // also decode from String Aside: java.nio classes and java.io Reader/Writer classes … Read more

How to translate “bytes” objects into literal strings in pandas Dataframe, Python3.x?

You can use vectorised str.decode to decode byte strings into ordinary strings: df[‘COLUMN1’].str.decode(“utf-8”) To do this for multiple columns you can select just the str columns: str_df = df.select_dtypes([np.object]) convert all of them: str_df = str_df.stack().str.decode(‘utf-8’).unstack() You can then swap out converted cols with the original df cols: for col in str_df: df[col] = str_df[col]