Posts

Showing posts with the label Bytearray

C Function To Convert Float To Byte Array

Answer : Easiest is to make a union: #include <stdio.h> int main(void) { int ii; union { float a; unsigned char bytes[4]; } thing; thing.a = 1.234; for (ii=0; ii<4; ii++) printf ("byte %d is %02x\n", ii, thing.bytes[ii]); return 0; } Output: byte 0 is b6 byte 1 is f3 byte 2 is 9d byte 3 is 3f Note - there is no guarantee about the byte order… it depends on your machine architecture. To get your function to work, do this: void float2Bytes(byte bytes_temp[4],float float_variable){ union { float a; unsigned char bytes[4]; } thing; thing.a = float_variable; memcpy(bytes_temp, thing.bytes, 4); } Or to really hack it: void float2Bytes(byte bytes_temp[4],float float_variable){ memcpy(bytes_temp, (unsigned char*) (&float_variable), 4); } Note - in either case I make sure to copy the data to the location given as the input parameter. This is crucial, as local variables will not exist after you return (alt...

Convert InputStream To Byte Array In Java

Answer : You can use Apache Commons IO to handle this and similar tasks. The IOUtils type has a static method to read an InputStream and return a byte[] . InputStream is; byte[] bytes = IOUtils.toByteArray(is); Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray() . It handles large files by copying the bytes in blocks of 4KiB. You need to read each byte from your InputStream and write it to a ByteArrayOutputStream . You can then retrieve the underlying byte array by calling toByteArray() : InputStream is = ... ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[16384]; while ((nRead = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } return buffer.toByteArray(); Finally, after twenty years, there’s a simple solution without the need for a 3rd party library, thanks to Java 9: InputStream is; … byte[] array = is.readAllBytes(); Note also the convenience met...

Convert Binary String To Bytearray In Python 3

Answer : Here's an example of doing it the first way that Patrick mentioned: convert the bitstring to an int and take 8 bits at a time. The natural way to do that generates the bytes in reverse order. To get the bytes back into the proper order I use extended slice notation on the bytearray with a step of -1: b[::-1] . def bitstring_to_bytes(s): v = int(s, 2) b = bytearray() while v: b.append(v & 0xff) v >>= 8 return bytes(b[::-1]) s = "0110100001101001" print(bitstring_to_bytes(s)) Clearly, Patrick's second way is more compact. :) However, there's a better way to do this in Python 3: use the int.to_bytes method: def bitstring_to_bytes(s): return int(s, 2).to_bytes((len(s) + 7) // 8, byteorder='big') If len(s) is guaranteed to be a multiple of 8, then the first arg of .to_bytes can be simplified: return int(s, 2).to_bytes(len(s) // 8, byteorder='big') This will raise OverflowError if len(s) is n...

Bytes Vs Bytearray In Python 2.6 And 3

Answer : For (at least) Python 3.7 According to the docs: bytes objects are immutable sequences of single bytes bytearray objects are a mutable counterpart to bytes objects. And that's pretty much it as far as bytes vs bytearray . In fact, they're fairly interchangeable and designed to flexible enough to be mixed in operations without throwing errors. In fact, there is a whole section in the official documentation dedicated to showing the similarities between the bytes and bytearray apis. Some clues as to why from the docs: Since many major binary protocols are based on the ASCII text encoding, bytes objects offer several methods that are only valid when working with ASCII compatible data and are closely related to string objects in a variety of other ways. In Python 2.6 bytes is merely an alias for str . This "pseudo type" was introduced to [partially] prepare programs [and programmers!] to be converted/compatible with Python 3.0 where there is a ...