Archive for the ‘programming’ Category

How to put a semicolon at the end of each line ;

October 11, 2011

I’m not very fond of copy-paste programming, or copy any large chunk of text into my program and make code out of it.

However, sometimes I have to. When it’s not defendable to write a file parser to parse one file once.. It’s usually when validating something and it’s necessary to get a set of test-data into your program with the least possible effort.

Have you ever gone through a 100-rows or more file and put [ ] ; – { } ( ) \ / in the beginning or end of each line? I have. Lots and lots of times. Until I discovered gvim.

Now I write nice little commands like

%s/asdf/ghij/g	- Replace asdf with ghij in entire file.
%s/$/;/g	- Add semicolon to the end of each line
%s/^ ;//g	- Delete semicolons from previously empty lines..
%s/^/\t/g	- Add tab to beginning of each line
g/asdf/d	- Delete all lines containing asdf
g/asdf/d2	- Delete 2 lines from every line containing asdf.

I don’t remember all pages I read to create these macros but this was probably one of them.

Floating point number standards

November 28, 2010

I got an interesting task to interpret a binary file. The problem was that, in the file, they didn’t use the same standard for the floating point number architecture.

The most widely used standard for binary floating point numbers is IEEE 754-1985. For single precision (32 bits), bit 0-22 is the fraction, 24-30 is exponent and bit 31 is the sign. The floating point value is then: (-1)^sign * 2^(exponent-127) * 1.fraction. 1.fraction is a binary fractional number (1.11 is 1 + 2^(-1) + 2^(-2) or something). It was confusing since I never used that before.

Anyway, this did not comply with the floating point numbers in my file. However, the fraction (might also be called mantissa?) was always the same. And after a little guessing and fiddling around with bits, I came to the conclusion that:

Bit 0-23 is the fraction, bit 24 is the sign bit and 25-31 is the exponent. The floating point value is then (-1)^sign * 2^(exponent-129) * 1.fraction. So the exponent offset is different and the sign and exponent bit switched places. This has worked with all the numbers I tried to far.

What is so annoying is that I don’t find this architecture anywhere! I don’t know where to look, google gives me nothing! According to a document I got with the file the numbers are in the CPM format. What is that?? Anybody?

How to create a csv file in javascript and get the “save as”-dialog

June 17, 2010

Long headline! Anyway, this is a problem I’ve encountered more than once. When exporting data from a webpage to a .csv file I usaually create it on the server side using a servlet.

However, If the data is already stored in the browser, you don’t want an extra server request (especially if you’ve manipulated the data on the client).

The first lead came from this post. It explains how to open a separate window with csv content. But how to get the “Save As” dialog? After some searching I found tips about using “data URL” and finally found the solution.

The essentials of my test code:

function loadLinkButton(){
  var btn = document.getElementById("linkButton");
  var axx = document.getElementById("ax");
  // I used this online encoder to create the data url.
  // axx.href = 'data:text/csv;base64,MTsyOzQ=';  // This was my first test, not having the encoder. 
  axx.href = 'data:text/csv;base64,'+Base64.encode("3;2;1");} // I used the javascript encoder from this page.
}

<body> 
<a id="ax" href="">test</a>
<button id="linkButton" onclick="loadLinkButton();">asdf</button>
</body>

So basically, pressing the “asdf” button creates the data URL and then the link “test” opens the csv (with save-as dialog). Havn’t tried it in IE, but I’ve read that it’s not going to work.

Questions? Feedback? Feel free to comment!