bytearray

Displaying database image (bytes[]) in Razor/MVC3?

帅比萌擦擦* 提交于 2019-12-17 22:23:35
问题 I am returning a dataset from my MS SQL 2008R2 database that contains a datatable that I am already getting data from on my Razor view. I added a byte[] field that contains an image thumbnail that I am trying to display on that view. Since the byte array is relatively tiny, I figured I would try displaying the byte array inline. However, after reading there may be some browser-related issues, I abandoned this. Creating a controller method seemed the way to go, (both methods found here),

Determine if a byte[] is a pdf file

筅森魡賤 提交于 2019-12-17 22:23:26
问题 Is there any way of checking if a byte[] is a pdf without opening? I have some code to display a list of byte[] as pdf thumbnails. I previously knew all the byte[] were pdf's because we filtered the servlet to only return these. Now the requirement has changed and I need to bring all file types back. Is there any way of checking what the byte[] is, or more specifically determining if it isn't, a pdf? 回答1: Check the first 4 bytes of the array. If those are 0x25 0x50 0x44 0x46 then it's most

Inserting bytes in the middle of binary file

拜拜、爱过 提交于 2019-12-17 19:27:24
问题 I want to add some string in the middle of image metadata block. Under some specific marker. I have to do it on bytes level since .NET has no support for custom metadata fields. The block is built like 1C 02 XX YY YY ZZ ZZ ZZ ... where XX is the ID of the field I need to append and YY YY is the size of it, ZZ = data. I imagine it should be more or less possible to read all the image data up to this marker (1C 02 XX) then increase the size bytes (YY YY), add data at the end of ZZ and then add

'str' does not support the buffer interface Python3 from Python2

时间秒杀一切 提交于 2019-12-17 19:14:13
问题 Hi have this two funtions in Py2 works fine but it doesn´t works on Py3 def encoding(text, codes): binary = '' f = open('bytes.bin', 'wb') for c in text: binary += codes[c] f.write('%s' % binary) print('Text in binary:', binary) f.close() return len(binary) def decoding(codes, large): f = file('bytes.bin', 'rb') bits = f.read(large) tmp = '' decode_text = '' for bit in bits: tmp += bit if tmp in fordecodes: decode_text += fordecodes[tmp] tmp = '' f.close() return decode_text The console

Convert bool[] to byte[]

让人想犯罪 __ 提交于 2019-12-17 18:29:24
问题 I have a List<bool> which I want to convert to a byte[] . How do i do this? list.toArray() creates a bool[] . 回答1: Here's two approaches, depending on whether you want to pack the bits into bytes, or have as many bytes as original bits: bool[] bools = { true, false, true, false, false, true, false, true, true }; // basic - same count byte[] arr1 = Array.ConvertAll(bools, b => b ? (byte)1 : (byte)0); // pack (in this case, using the first bool as the lsb - if you want // the first bool as the

Building a 9 patch drawable at runtime

末鹿安然 提交于 2019-12-17 18:24:25
问题 I am successfully building a 9patch drawable at runtime on Android 3.0+ using the excellent gist provided by Brian Griffey (found here). Essentially I load the raw (no patches) graphic file from the network and the filename contains the cap insets that I need to use in order to scale the image accordingly. I then use these values with the class found above and apply the image as a background to a variety of elements (such as TextView , ImageButton , Button , ViewGroup , etc). This works

Hex-encoded String to Byte Array

橙三吉。 提交于 2019-12-17 17:54:34
问题 String str = "9B7D2C34A366BF890C730641E6CECF6F"; I want to convert str into byte array, but str.getBytes() returns 32 bytes instead of 16. 回答1: I think what the questioner is after is converting the string representation of a hexadecimal value to a byte array representing that hexadecimal value. The apache commons-codec has a class for that, Hex. String s = "9B7D2C34A366BF890C730641E6CECF6F"; byte[] bytes = Hex.decodeHex(s.toCharArray()); 回答2: Java SE 6 or Java EE 5 provides a method to do

How to read a file as a byte array in Scala

孤街醉人 提交于 2019-12-17 15:14:28
问题 I can find tons of examples but they seem to either rely mostly on Java libraries or just read characters/lines/etc. I just want to read in some file and get a byte array with scala libraries - can someone help me with that? 回答1: Java 7: import java.nio.file.{Files, Paths} val byteArray = Files.readAllBytes(Paths.get("/path/to/file")) I believe this is the simplest way possible. Just leveraging existing tools here. NIO.2 is wonderful. 回答2: This should work (Scala 2.8): val bis = new

How can I safely convert a byte array into a string and back? [duplicate]

余生颓废 提交于 2019-12-17 10:33:47
问题 This question already has answers here : How do I get a consistent byte representation of strings in C# without manually specifying an encoding? (39 answers) Closed 6 years ago . I don't really care about encoding and stuff, as long as I get back the exact same byte array. So to sum up: How do I convert a byte array into a string, and then that string back into the same byte array I started with? 回答1: The absolute safest way to convert bytes to a string and back is to use base64: string

Easiest way to convert a Blob into a byte array

僤鯓⒐⒋嵵緔 提交于 2019-12-17 09:10:04
问题 what is the easiest way to convert a Blob into a byte array?I am using MYSQL and i want to convert a Blob datatype into a byte array. Iam using java programming language:) 回答1: the mySql blob class has the following function : blob.getBytes use it like this: //(assuming you have a ResultSet named RS) Blob blob = rs.getBlob("SomeDatabaseField"); int blobLength = (int) blob.length(); byte[] blobAsBytes = blob.getBytes(1, blobLength); //release the blob and free up memory. (since JDBC 4.0) blob