bytearray

LSB/MSB handling in Java

六月ゝ 毕业季﹏ 提交于 2019-12-30 01:12:21
问题 If I have to handle values to be stored in bytes like 0x118, how do I split the LSB and MSB? I was trying the following way... I don't think that it's the right way: value = 0x118; Storing in bytes... result[5] = (byte) value; result[6] = (byte)(value << 8); ... What is the correct way? 回答1: This will do it: result[5] = (byte) (value & 0xFF); // Least significant "byte" result[6] = (byte) ((value & 0xFF00) >> 8); // Most significant "byte" I usually use bit masks - maybe they're not needed.

Failure to read JPEG file from byte[]

半腔热情 提交于 2019-12-29 09:15:07
问题 Has anyone ever had an issue with Loading JPEG files in java? One of our clients is sending files that cannot be resolved, but these same files can be opened in windows. (the other 99% of jpeg files we recieve, process without any problems) I have tried a couple of libraries to read these: itextpdf : com.itextpdf.text.Jpeg (getInstance(imageData) - gives "java.io.IOException: Premature EOF while reading JPG." sanselan : org.apache.sanselan.ImageInfo ( Sanselan.getImageInfo(imageData) gives

How to get a byte array from a drawable resource ?

守給你的承諾、 提交于 2019-12-29 08:49:15
问题 I would like to get a byte array from an jpeg image located in my res/drawable file ? Does anyone know how to do that please ? 回答1: Get a bitmap decodeResource(android.content.res.Resources, int) Then either compress it to ByteArrayOutputStream() or copyPixelsToBuffer and get your array from the buffer. http://developer.android.com/reference/android/graphics/Bitmap.html 回答2: Drawable drawable; Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); ByteArrayOutputStream stream = new

simplest way to send raw Byte-arrays using Ruby's TCPSocket-Class

百般思念 提交于 2019-12-29 06:33:06
问题 i want to send raw bytes using Rubys TCPSocket-Class. Has someone a good example? I've tried it in this way, but it does not work :( require 'socket' host = '192.168.0.80' port = 102 s = TCPSocket.new(host, port) s.write [0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0xC1, 0x02, 0x02, 0x02, 0xC2, 0x02, 0x02, 0x02, 0xC0, 0x01, 0x0A ].pack('C') puts s.read s.close puts "exit" thanks :) 回答1: Try using a "*" after the format directive to eat all the elements in the list: s

GUID to ByteArray

…衆ロ難τιáo~ 提交于 2019-12-29 05:41:06
问题 I just wrote this code to convert a GUID into a byte array. Can anyone shoot any holes in it or suggest something better? public static byte[] getGuidAsByteArray(){ UUID uuid = UUID.randomUUID(); long longOne = uuid.getMostSignificantBits(); long longTwo = uuid.getLeastSignificantBits(); return new byte[] { (byte)(longOne >>> 56), (byte)(longOne >>> 48), (byte)(longOne >>> 40), (byte)(longOne >>> 32), (byte)(longOne >>> 24), (byte)(longOne >>> 16), (byte)(longOne >>> 8), (byte) longOne, (byte

How to create a Drawable from byte[] ? (Android)

自古美人都是妖i 提交于 2019-12-29 04:14:27
问题 I have an array of bytes and I need to convert it into a Android Drawable. How can I perform this conversion? Here is what i tried but without success: byte[] b = getByteArray(); ByteArrayInputStream is = new ByteArrayInputStream(b); Drawable drw = Drawable.createFromStream(is, "articleImage"); drw is always null! EDIT: My byte[] was actually corrupted/incomplete, that was the problem. 回答1: If your byte[] b is contains imagedata then you can also try this, Drawable image = new BitmapDrawable

How do I dump the struct into the byte array without reflection?

一个人想着一个人 提交于 2019-12-29 03:27:28
问题 I already found encoding/binary package to deal with it, but it depended on reflect package so it didn't work with uncapitalized(that is, unexported) struct fields. However I spent a week to find that problem out, I still have a question: if struct fields should not be exported, how do I dump them easily into binary data? EDIT: Here's the example. If you capitalize the name of fields of Data struct, that works properly. But Data struct was intended to be an abstract type, so I don't want to

Convert python long/int to fixed size byte array

陌路散爱 提交于 2019-12-29 03:17:05
问题 I'm trying to implement RC4 and DH key exchange in python. Problem is that I have no idea about how to convert the python long/int from the key exchange to the byte array I need for the RC4 implementation. Is there a simple way to convert a long to the required length byte array? Update : forgot to mention that the numbers I'm dealing with are 768 bit unsigned integers. 回答1: I haven't done any benchmarks, but this recipe "works for me". The short version: use '%x' % val , then unhexlify the

Converting JSON between string and byte[] with GSON

梦想与她 提交于 2019-12-28 17:41:42
问题 I am using hibernate to map objects to the database. A client (an iOS app) sends me particular objects in JSON format which I convert to their true representation using the following utility method /** * Convert any json string to a relevant object type * @param jsonString the string to convert * @param classType the class to convert it too * @return the Object created */ public static <T> T getObjectFromJSONString(String jsonString, Class<T> classType) { if(stringEmptyOrNull(jsonString) ||

How to convert a Java String to an ASCII byte array?

末鹿安然 提交于 2019-12-28 05:06:07
问题 How to convert a Java String to an ASCII byte array? 回答1: Using the getBytes method, giving it the appropriate Charset (or Charset name). Example: String s = "Hello, there."; byte[] b = s.getBytes(StandardCharsets.US_ASCII); (Before Java 7: byte[] b = s.getBytes("US-ASCII"); ) 回答2: If you are a guava user there is a handy Charsets class: String s = "Hello, world!"; byte[] b = s.getBytes(Charsets.US_ASCII); Apart from not hard-coding arbitrary charset name in your source code it has a much