byte

Byte shift inverse operation

▼魔方 西西 提交于 2019-12-22 08:54:33
问题 I have this code byte[] b = new byte[]{-33,-4,20,30}; System.err.println(Arrays.toString(b)); int x = (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3]; b = new byte[]{(byte)(x >> 24), (byte)(x >> 16), (byte)(x >> 8), (byte)(x)}; System.err.println(Arrays.toString(b)); Output: [-33, -4, 20, 30] [-34, -4, 20, 30] I cannot figure out why this operations are not invers. 回答1: Your problem is unwanted sign extension. Specifically, b[1] is -4 , or 0xfc which is sign-extended to 0xfffffffc then left

Byte shift inverse operation

喜欢而已 提交于 2019-12-22 08:54:22
问题 I have this code byte[] b = new byte[]{-33,-4,20,30}; System.err.println(Arrays.toString(b)); int x = (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3]; b = new byte[]{(byte)(x >> 24), (byte)(x >> 16), (byte)(x >> 8), (byte)(x)}; System.err.println(Arrays.toString(b)); Output: [-33, -4, 20, 30] [-34, -4, 20, 30] I cannot figure out why this operations are not invers. 回答1: Your problem is unwanted sign extension. Specifically, b[1] is -4 , or 0xfc which is sign-extended to 0xfffffffc then left

Sending bytes to serial port from UNIX command line?

孤人 提交于 2019-12-22 08:36:21
问题 i would like to send a stream of bytes to a serial port using the command line. is this possible? my serial port is at /dev/cu.usbserial-A700dYoR on my Mac. for example, if i wanted to send the integer 50 or the string "data" to that serial port, how can i do that? my knowledge of UNIX is very limited. 回答1: #!/bin/bash # Port setting stty -F /dev/cu.usbserial-A700dYoR raw speed 9600 echo 'Hello' > /dev/cu.usbserial-A700dYoR or something like that if I remember correctly... Been a few years.

It is possible to display pdf received bytes from service in web view in Android

那年仲夏 提交于 2019-12-22 07:06:19
问题 I am receiving bytes of data like JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nNVZyY7jNhC991foHCBOVXERBQQBJFu+D9BAfiALkEOAzGV+f15Ra2shqaQz6IFhWzJp8tX+WKIbV19e/qmo+pFw6VnwWTf6+fn36tcfqr/joL4+//nSvb44fwtVze7mq9ffqp+eXDFVr3/8TDy+hAxZcuSpxl3AXYOrFvcdrpju+H7gyuG7pydmeHL4I7OwwZdlvdHRhv0vr3+99K8vnw5BkL3JCYjpdSfPgMGBG2zm5uVbAGhx4fHucH/H+5HazEMlTdlmQG655ycTWdGBTli1AdXgApqYJZ7gbH8Xs/mfHSCKaqUVj18zuvGBb+4S3BA3rqORsOU5NBjTMa

Java byte[] to/from String conversion

不羁的心 提交于 2019-12-22 05:53:31
问题 Why does this junit test fail? import org.junit.Assert; import org.junit.Test; import java.io.UnsupportedEncodingException; public class TestBytes { @Test public void testBytes() throws UnsupportedEncodingException { byte[] bytes = new byte[]{0, -121, -80, 116, -62}; String string = new String(bytes, "UTF-8"); byte[] bytes2 = string.getBytes("UTF-8"); System.out.print("bytes2: ["); for (byte b : bytes2) System.out.print(b + ", "); System.out.print("]\n"); Assert.assertArrayEquals(bytes,

Java byte[] to/from String conversion

你。 提交于 2019-12-22 05:53:11
问题 Why does this junit test fail? import org.junit.Assert; import org.junit.Test; import java.io.UnsupportedEncodingException; public class TestBytes { @Test public void testBytes() throws UnsupportedEncodingException { byte[] bytes = new byte[]{0, -121, -80, 116, -62}; String string = new String(bytes, "UTF-8"); byte[] bytes2 = string.getBytes("UTF-8"); System.out.print("bytes2: ["); for (byte b : bytes2) System.out.print(b + ", "); System.out.print("]\n"); Assert.assertArrayEquals(bytes,

1l for long, 1f for float, 1d for double, what about byte?

我们两清 提交于 2019-12-22 05:33:34
问题 1l for long, 1f for float, 1d for double, what about byte? long l = 1l; float f = 1f; double d = 1d; // byte b = 1?; What's the equivalent for byte ? Does it exist? 回答1: No, there is no suffix you can append to a numeric literal to make it a byte . See 3.10 Literals in the Java Language Specification. 回答2: You need to cast to byte like this: byte b = 1; b = (byte) 5; Since by default these numeric constant are treated as int in Java. 回答3: there is no suffix you can append a numeric literal

MVC audio controls playing song from bytes

霸气de小男生 提交于 2019-12-22 04:05:57
问题 I have my songs stored in database as bytes[]. How do I use these in the <audio> tag. So something like this. Do I need to convert the bytes to something else first? I am not sure. foreach (var item in Model) { <audio controls> <source src=@item.SongBytes type="audio/mp3"/> </audio> } 回答1: One way would be to add a new action in your controller that returns the data: public ActionResult Audio(int someId) { byte[] songBytes; // Add code to get data return new FileStreamResult(songBytes, "audio

MVC audio controls playing song from bytes

懵懂的女人 提交于 2019-12-22 04:05:03
问题 I have my songs stored in database as bytes[]. How do I use these in the <audio> tag. So something like this. Do I need to convert the bytes to something else first? I am not sure. foreach (var item in Model) { <audio controls> <source src=@item.SongBytes type="audio/mp3"/> </audio> } 回答1: One way would be to add a new action in your controller that returns the data: public ActionResult Audio(int someId) { byte[] songBytes; // Add code to get data return new FileStreamResult(songBytes, "audio

Java SHA-1 hash an unsigned BYTE

我的梦境 提交于 2019-12-22 00:36:22
问题 Hy guys! I have the following problem: I need to hash an unsigned byte in Java which is(would be...) between 0-255. The main problem is that java doesnt have an unsigned Byte type at all. I found a workaround for this, and used int instead of byte with a little modification. The main problem is: Java.securitys Messagedigest.digest function only accepts byte array types, but i would need to give it an int array. Anybody has a simpe workaround for this? I was looking for a third party sha-1