Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What language is to binary, as Perl is to text?

I am looking for a scripting (or higher level programming) language (or e.g. modules for Python or similar languages) for effortlessly analyzing and manipulating binary data in files (e.g. core dumps), much like Perl allows manipulating text files very smoothly.

Things I want to do include presenting arbitrary chunks of the data in various forms (binary, decimal, hex), convert data from one endianess to another, etc. That is, things you normally would use C or assembly for, but I'm looking for a language which allows for writing tiny pieces of code for highly specific, one-time purposes very quickly.

Any suggestions?

like image 309
Eric Hansander Avatar asked Jun 14 '09 18:06

Eric Hansander


People also ask

What language is Perl like?

Perl is a family of script programming languages that is similar in syntax to the C language. It is an older, open source, general use, interpreted language. Perl was developed with usability in mind. Its efficient design lets developers do a lot with a little bit of code.

What language is binary written?

Explanation: The computer language that is written by 0 and 1 binary code which is understood by the computer system is called the machine language. The machine language also called low-level language while c#,c++ high-level language.

Is Perl language still used?

Perl is not going away even if it tends to be less trendy than other modern languages. It is used in production codebases of many companies, for tasks as diverse as web development, databases access, log analysis or web crawling. It is a core component of most unix-like systems.

Is binary considered a language?

But binary code is a language of sorts – in fact, it is the language that makes it possible for you to be reading this article on your computer, tablet or smartphone. Therefore, it is a fundamental language for any futureproof business to be aware of, even if computer programmers are not on your current payroll.


2 Answers

Things I want to do include presenting arbitrary chunks of the data in various forms (binary, decimal, hex), convert data from one endianess to another, etc. That is, things you normally would use C or assembly for, but I'm looking for a language which allows for writing tiny pieces of code for highly specific, one-time purposes very quickly.

Well, while it may seem counter-intuitive, I found erlang extremely well-suited for this, namely due to its powerful support for pattern matching, even for bytes and bits (called "Erlang Bit Syntax"). Which makes it very easy to create even very advanced programs that deal with inspecting and manipulating data on a byte- and even on a bit-level:

Since 2001, the functional language Erlang comes with a byte-oriented datatype (called binary) and with constructs to do pattern matching on a binary.

And to quote informIT.com:

(Erlang) Pattern matching really starts to get fun when combined with the binary type. Consider an application that receives packets from a network and then processes them. The four bytes in a packet might be a network byte-order packet type identifier. In Erlang, you would just need a single processPacket function that could convert this into a data structure for internal processing. It would look something like this:

processPacket(<<1:32/big,RestOfPacket>>) ->
    % Process type one packets
    ...
;
processPacket(<<2:32/big,RestOfPacket>>) ->
    % Process type two packets
    ...

So, erlang with its built-in support for pattern matching and it being a functional language is pretty expressive, see for example the implementation of ueencode in erlang:

uuencode(BitStr) ->
<< (X+32):8 || <<X:6>> <= BitStr >>.
uudecode(Text) ->
<< (X-32):6 || <<X:8>> <= Text >>.

For an introduction, see Bitlevel Binaries and Generalized Comprehensions in Erlang.You may also want to check out some of the following pointers:

  • Parsing Binaries with erlang, lamers inside
  • More File Processing with Erlang
  • Learning Erlang and Adobe Flash format same time
  • Large Binary Data is (not) a Weakness of Erlang
  • Programming Efficiently with Binaries and Bit Strings
  • Erlang bit syntax and network programming
  • erlang, the language for network programming (1)
  • Erlang, the language for network programming Issue 2: binary pattern matching
  • An Erlang MIDI File Reader/Writer
  • Erlang Bit Syntax
  • Comprehending endianness
  • Playing with Erlang
  • Erlang: Pattern Matching Declarations vs Case Statements/Other
  • A Stream Library using Erlang Binaries
  • Bit-level Binaries and Generalized Comprehensions in Erlang
  • Applications, Implementation and Performance Evaluation of Bit Stream Programming in Erlang
like image 186
none Avatar answered Sep 22 '22 20:09

none


perl's pack and unpack ?

like image 37
toolkit Avatar answered Sep 20 '22 20:09

toolkit