binary-data

C++ read binary file and convert to hex

佐手、 提交于 2019-12-22 08:17:12
问题 I'm having some problems reading a binary file and converting it's bytes to hex representation. What I've tried so far: ifstream::pos_type size; char * memblock; ifstream file (toread, ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); memblock = new char [size]; file.seekg (0, ios::beg); file.read (memblock, size); file.close(); cout << "the complete file content is in memory" << endl; std::string tohexed = ToHex(memblock, true); std::cout << tohexed << std::endl; }

Java: read from binary file, send bytes over socket

ぐ巨炮叔叔 提交于 2019-12-21 20:43:33
问题 This should be easy, but I can't get my head around it right now. I wanna send some bytes over a socket, like Socket s = new Socket("localhost", TCP_SERVER_PORT); DataInputStream is = new DataInputStream(new BufferedInputStream(s.getInputStream())); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream())); for (int j=0; j<40; j++) { dos.writeByte(0); } That works, but now I dont want to writeByte to the Outputstream, but read from a binary file, then write it

How to print float value from binary file in shell?

风流意气都作罢 提交于 2019-12-21 17:18:40
问题 I've binary file consisting double float (8 bytes) or just float (4 bytes) value which was generated in the following way: $ python -c $'from struct import pack\nwith open("file.bin", "wb") as f: f.write(pack("<d", 0.123))' $ xxd file.bin 00000000: b072 6891 ed7c bf3f .rh..|.? which I could print it back from Python via: $ python -c $'from struct import unpack\nwith open("file.bin", "rb") as f: print(unpack("<d", f.read(8)))' (0.123,) The same for 4-byte float, just change <d into <f

What is the difference between a Binary and a Bitstring in Erlang?

我是研究僧i 提交于 2019-12-21 11:26:08
问题 In the Erlang shell, I can do the following: A = 300. 300 <<A:32>>. <<0, 0, 1, 44>> But when I try the following: B = term_to_binary({300}). <<131,104,1,98,0,0,1,44>> <<B:32>> ** exception error: bad argument <<B:64>> ** exception error: bad argument In the first case, I'm taking an integer and using the bitstring syntax to put it into a 32-bit field. That works as expected. In the second case, I'm using the term_to_binary BIF to turn the tuple into a binary, from which I attempt to unpack

How to best get a byte array from a ClientResponse from Spring WebClient?

五迷三道 提交于 2019-12-21 07:48:06
问题 I'm trying out the new WebClient from Spring 5 (5.0.0.RC2) in a codebase that uses reactive programming and I've had success mapping the JSON response from an endpoint to a DTO in my app, which works very nice: WebClient client = WebClient.create(baseURI); Mono<DTO> dto = client.get() .uri(uri) .accept(MediaType.APPLICATION_JSON) .exchange() .flatMap(response -> response.bodyToMono(DTO.class)); However, now I'm trying to the response body from an endpoint which uses Protocol Buffers (binary

R-How to generate random sample of a discrete random variables?

假如想象 提交于 2019-12-21 07:26:46
问题 In R, I want to generate a random sample of a discrete random variable: X , where: P(X=a)=P(X=-a)=1/2 . I have been searching for a function online, but there seems no direct function doing this. 回答1: I think you are looking to generate samples of a Bernoulli random variable. A Bernoulli random variable is a special case of a binomial random variable. Therefore, you can try rbinom(N,1,p) . This will generate N samples, with value 1 with probability p , value 0 with probability (1-p) . To get

Framework for building structured binary data parsers?

99封情书 提交于 2019-12-21 04:05:04
问题 I have some experience with Pragmatic-Programmer-type code generation: specifying a data structure in a platform-neutral format and writing templates for a code generator that consume these data structure files and produce code that pulls raw bytes into language-specific data structures, does scaling on the numeric data, prints out the data, etc. The nice pragmatic(TM) ideas are that (a) I can change data structures by modifying my specification file and regenerating the source (which is DRY

Meaning of a Common String In Executables?

时光毁灭记忆、已成空白 提交于 2019-12-21 03:53:31
问题 There appear to be some similar-looking long alphanumeric strings that commonly occur in Mach-O 64 bit executables and ELF 64-bit LSB executables among other symbols that are not alphanumeric: cat /bin/bash | grep -c "AWAVAUATSH" has 181 results, and cat /usr/bin/gzip | grep -c "AWAVAUATSH" has 9 results. What are these strings? 回答1: Interesting question. Since I didn't know the answer, here are the steps I took to figure it out: Where in the file does the string occur? strings -otx /bin/gzip

Read binary data from an image and save it with JavaScript

◇◆丶佛笑我妖孽 提交于 2019-12-21 01:58:34
问题 I want to read the binary data of an image and then save it again to my local disk with JavaScript. I wrote a small demo that shows this use-case. To read the file I use readAsBinaryString from the File Reader API (HTML5) to get the binary data. I write the binary string into a textfield from which I then read the data again to write it to a file. If I save the file my images (I tested several JPEGs) are broken so you cannot see anything useful. Can it be that "readAsBinaryString" makes a

Persisting Blob Streams with NHibernate

假装没事ソ 提交于 2019-12-20 14:43:00
问题 If I have a class declared as: public class MyPersistentClass { public int ID { get; set; } public Stream MyData {get;set; } } How can I use NHibernate's mappings to persist the MyData property to and from the database? 回答1: You could use a Stream using a custom type and map it according to your storage needs. But there are some issues with using the Stream object as I mention in my blog series about lazy streaming of BLOBs and CLOBs with NHibernate. What you really need is a Blob object that