Is there an equivalent to memcpy() in Java?

Use System.arraycopy() System.arraycopy(sourceArray, sourceStartIndex, targetArray, targetStartIndex, length); Example, String[] source = { “alpha”, “beta”, “gamma” }; String[] target = new String[source.length]; System.arraycopy(source, 0, target, 0, source.length); or use Arrays.copyOf() Example, target = Arrays.copyOf(source, length); java.util.Arrays.copyOf(byte[] source, int length) was added in JDK 1.6. The copyOf() method uses System.arrayCopy() to make a copy of the array, … Read more

How can I safely convert a byte array into a string and back? [duplicate]

The absolute safest way to convert bytes to a string and back is to use base64: string base64 = Convert.ToBase64String(bytes); byte[] bytes = Convert.FromBase64String(base64); That way you’re guaranteed not to get “invalid” unicode sequences such as the first half of a surrogate pair without the second half. Nothing’s going to decide to normalize the data … Read more

Byte Array to Hex String

Using str.format: >>> array_alpha = [ 133, 53, 234, 241 ] >>> print ”.join(‘{:02x}’.format(x) for x in array_alpha) 8535eaf1 or using format >>> print ”.join(format(x, ’02x’) for x in array_alpha) 8535eaf1 Note: In the format statements, the 02 means it will pad with up to 2 leading 0s if necessary. This is important since [0x1, … Read more

Fastest way to convert Image to Byte array

There is a RawFormat property of Image parameter which returns the file format of the image. You might try the following: // extension method public static byte[] imageToByteArray(this System.Drawing.Image image) { using(var ms = new MemoryStream()) { image.Save(ms, image.RawFormat); return ms.ToArray(); } }

Convert PIL Image to byte array?

Thanks everyone for your help. Finally got it resolved!! import io img = Image.open(fh, mode=”r”) roi_img = img.crop(box) img_byte_arr = io.BytesIO() roi_img.save(img_byte_arr, format=”PNG”) img_byte_arr = img_byte_arr.getvalue() With this i don’t have to save the cropped image in my hard disc and I’m able to retrieve the byte array from a PIL cropped image.

Byte[] to InputStream or OutputStream

You create and use byte array I/O streams as follows: byte[] source = …; ByteArrayInputStream bis = new ByteArrayInputStream(source); // read bytes from bis … ByteArrayOutputStream bos = new ByteArrayOutputStream(); // write bytes to bos … byte[] sink = bos.toByteArray(); Assuming that you are using a JDBC driver that implements the standard JDBC Blob interface … Read more

hexadecimal string to byte array in python

Suppose your hex string is something like >>> hex_string = “deadbeef” Convert it to a bytearray (Python 3 and 2.7): >>> bytearray.fromhex(hex_string) bytearray(b’\xde\xad\xbe\xef’) Convert it to a bytes object (Python 3): >>> bytes.fromhex(hex_string) b’\xde\xad\xbe\xef’ Note that bytes is an immutable version of bytearray. Convert it to a string (Python ≤ 2.7): >>> hex_data = hex_string.decode(“hex”) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)