Error “This stream does not support seek operations” in C#

You probably want something like this. Either checking the length fails, or the BinaryReader is doing seeks behind the scenes. HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url); WebResponse myResp = myReq.GetResponse(); byte[] b = null; using( Stream stream = myResp.GetResponseStream() ) using( MemoryStream ms = new MemoryStream() ) { int count = 0; do { byte[] buf = … Read more

Convert a String to a byte array and then back to the original String

I would suggest using the members of string, but with an explicit encoding: byte[] bytes = text.getBytes(“UTF-8”); String text = new String(bytes, “UTF-8”); By using an explicit encoding (and one which supports all of Unicode) you avoid the problems of just calling text.getBytes() etc: You’re explicitly using a specific encoding, so you know which encoding … Read more

C++ int to byte array

You don’t need a whole function for this; a simple cast will suffice: int x; static_cast<char*>(static_cast<void*>(&x)); Any object in C++ can be reinterpreted as an array of bytes. If you want to actually make a copy of the bytes into a separate array, you can use std::copy: int x; char bytes[sizeof x]; std::copy(static_cast<const char*>(static_cast<const void*>(&x)), … Read more

How to convert a Byte Array to an Int Array

You’ve said in the comments that you want four bytes from the input array to correspond to one integer on the output array, so that works out nicely. Depends on whether you expect the bytes to be in big-endian or little-endian order, but… IntBuffer intBuf = ByteBuffer.wrap(byteArray) .order(ByteOrder.BIG_ENDIAN) .asIntBuffer(); int[] array = new int[intBuf.remaining()]; intBuf.get(array); … Read more

How to create python bytes object from long hex string?

Works in Python 2.7 and higher including python3: result = bytearray.fromhex(‘deadbeef’) Note: There seems to be a bug with the bytearray.fromhex() function in Python 2.6. The python.org documentation states that the function accepts a string as an argument, but when applied, the following error is thrown: >>> bytearray.fromhex(‘B9 01EF’) Traceback (most recent call last): File … Read more