byte

convert from boolean to byte in java

為{幸葍}努か 提交于 2019-12-21 07:06:32
问题 I need to set byte value as method parameter. I have boolean variable isGenerated , that determines the logic to be executed within this method. But I can pass directly boolean as byte parameter this is not allowed and can't be cast in java. So the solution I have now looks like this: myObj.setIsVisible(isGenerated ? (byte)1 : (byte)0); But it seems odd for me. Maybe some better solution exists to do this? 回答1: your solution is correct. if you like you may avoid one cast by doing it the

Reading image header info without loading the entire image

落花浮王杯 提交于 2019-12-21 06:39:14
问题 I have a .net 3.5 application that will be dealing with a large number of images. I need to check that the image extension is correct, the image height and width, and the PPI. I do not want to load the entire image into a .net image or bitmap, this will take to long and be to resource intensive. I can not use third party plug-ins or dlls, and of course it needs to be done yesterday. So, I am reading the initial bytes of the files, checking the "magic" numbers to make sure the image extension

Is there an explanation for the behavior of this Java ByteBuffer?

青春壹個敷衍的年華 提交于 2019-12-21 05:22:39
问题 I need to convert numerical values into byte arrays. For example, to convert a long to a byte array, I have this method: public static byte[] longToBytes(long l) { ByteBuffer buff = ByteBuffer.allocate(8); buff.order(ByteOrder.BIG_ENDIAN); buff.putLong(l); return buff.array(); } It's pretty straightforward - take a long, allocate an array that can hold it, and throw it in there. Regardless of what the value of l is, I will get an 8 byte array back that I can then process and use as intended.

How do I concatenate 2 bytes?

落爺英雄遲暮 提交于 2019-12-21 04:15:12
问题 I have 2 bytes: byte b1 = 0x5a; byte b2 = 0x25; How do I get 0x5a25 ? 回答1: It can be done using bitwise operators '<<' and '|' public int Combine(byte b1, byte b2) { int combined = b1 << 8 | b2; return combined; } Usage example: [Test] public void Test() { byte b1 = 0x5a; byte b2 = 0x25; var combine = Combine(b1, b2); Assert.That(combine, Is.EqualTo(0x5a25)); } 回答2: Using bit operators: (b1 << 8) | b2 or just as effective (b1 << 8) + b2 回答3: A more explicit solution (also one that might be

nodejs write 64bit unsigned integer to buffer

懵懂的女人 提交于 2019-12-21 04:09:01
问题 I want to store a 64bit (8 byte) big integer to a nodejs buffer object in big endian format. The problem about this task is that nodejs buffer only supports writing 32bit integers as maximum (with buf.write32UInt32BE(value, offset)). So I thought, why can't we just split the 64bit integer? var buf = new Buffer(8); buf.fill(0) // clear all bytes of the buffer console.log(buf); // outputs <Buffer 00 00 00 00 00 00 00 00> var int = 0xffff; // as dezimal: 65535 buf.write32UInt32BE(0xff, 4); //

How can I bit-reflect a byte in Delphi?

ぐ巨炮叔叔 提交于 2019-12-21 03:37:27
问题 Is there an easy way to bit-reflect a byte variable in Delphi so that the most significant bit (MSB) gets the least significant bit (LSB) and vice versa? 回答1: In code you can do it like this: function ReverseBits(b: Byte): Byte; var i: Integer; begin Result := 0; for i := 1 to 8 do begin Result := (Result shl 1) or (b and 1); b := b shr 1; end; end; But a lookup table would be much more efficient, and only consume 256 bytes of memory. function ReverseBits(b: Byte): Byte; inline; const Table:

Determine number of Bytes in ByteBuffer

不打扰是莪最后的温柔 提交于 2019-12-21 03:33:28
问题 I have a ByteBuffer that can hold a maximum of (4 + size ) bytes (that is, an integer followed by size characters). However, the number of characters written to the ByteBuffer , may be smaller than size . So I was wondering, is there anyway to determine how many characters were written to the ByteBuffer and not just the total size of it? limit , position and such don't SEEM to be what I am after. Thanks for your help! 回答1: After you've written to the ByteBuffer, the number of bytes you've

How can I turn an int into three bytes in Java?

一世执手 提交于 2019-12-20 11:35:32
问题 I am trying to convert an int into three bytes representing that int (big endian). I'm sure it has something to do with bit-wise and and bit shifting. But I have no idea how to go about doing it. For example: int myInt; // some code byte b1, b2 , b3; // b1 is most significant, then b2 then b3. *Note, I am aware that an int is 4 bytes and the three bytes have a chance of over/underflowing. 回答1: To get the least significant byte: b3 = myInt & 0xFF; The 2nd least significant byte: b2 = (myInt >>

Joining byte list with python

女生的网名这么多〃 提交于 2019-12-20 11:04:24
问题 I'm trying to develop a tool that read a binary file, makes some changes and save it. What I'm trying to do is make a list of each line in the file, work with several lines and then join the list again. This is what I tried: file = open('myFile.exe', 'r+b') aList = [] for line in f: aList.append(line) #Here im going to mutate some lines. new_file = ''.join(aList) and give me this error: TypeError: sequence item 0: expected str instance, bytes found which makes sense because I'm working with

Bytes to Binary in C

怎甘沉沦 提交于 2019-12-20 10:44:04
问题 I'm trying to simply convert a byte received from fget into binary. I know the value of the first byte was 49 based on printing the value. I now need to convert this into its binary value. unsigned char byte = 49;// Read from file unsigned char mask = 1; // Bit mask unsigned char bits[8]; // Extract the bits for (int i = 0; i < 8; i++) { // Mask each bit in the byte and store it bits[i] = byte & (mask << i); } // For debug purposes, lets print the received data for (int i = 0; i < 8; i++) {