Friday, December 22, 2006

ASP utf-8 encoder from Windows string

I am posting this up, in case someone out there needs the code for encoding windows string to UTF-8.

It's so great to have it working, and it will happily convert your windows string (unicode UTF-16LE) to it's equivalent UTF-8 format.

Here it is. If you feel it's working, please put some comments:

function EncodeUTF8(s)
dim i
dim c
dim baseNum
baseNum = 32768

i = 1

'Response.Write "len(s)=" & len(s) & "
"

do while i <= len(s)
c = clng(ascW(mid(s,i,1))) 'returns the character code.

'if character code point is more than 32767, it's in negative value. So some adjustment.
if c < 0 then
c = (baseNum + c + 1) + 32767
end if

Response.Write "c=" & c & "
"

'normal algorithm to convert from UNICODE code points, to it's equivalent UTF-8 format.
if c < &H80 then
i = i + 1

elseif c < &H0800 then
s = left(s, i-1) + _
chr(RightShift(c,6) OR &HC0) + _
chr(c AND &H3F OR &H80) + _
mid(s,i+1)

i = i + 2

elseif c < &H10000 then
s = left(s, i-1) + _
chr(RightShift(c,12) OR &HE0) + _
chr(RightShift(c,6) AND &H3F OR &H80) + _
chr(c AND &H3F OR &H80) + _
mid(s,i+1)

i = i + 3
else
s = left(s, i-1) + _
chr(RightShift(c,18) OR &HF0) + _
chr(RightShift(c,12) AND &H3F OR &H80) + _
chr(RightShift(c,6) AND &H3F OR &H80) + _
chr(c AND &H3F OR &H80) + _
mid(s,i+1)

i = i + 4
end if
loop
EncodeUTF8 = s

end function


function RightShift(inputVal, position)

RightShift = fix(inputVal / 2^position )

end function

Tuesday, June 20, 2006

Think Big Projects

Alrite, this advice is for all the IT grads out there. You are fresh, and you are eager to show your talents to the world. And you landed yourself quite a decent job in the MIS department. But you found your job function quite the opposite of what you really wanted to do. Your role is....PC support job, endless troubleshooting MS office applications...for computer idiots staff. Or, if you got some programming job, it's debugging some previous bloated hideous ugly code written by an intern last few months. And no documentation.

So what do you do?

Now, this scenario is common when you work at a big company. If you are in a smaller company, you won't be flooded with so many boring stuff...

I know, cause I was there before. My other colleagues were just doing uncool stuff, like programming ABAP programs for SAP. Or managing some computer hardware stuff. It's just boring, for me.

Now, even when you are stuck with day to day mindless jobs, try to fit in your mind, BIG projects. Now, where can you get such inspiration to start one? Just look at your company's business, and see if there's any problems, that you could solve by putting some IT work into it.

And why would you want to trouble yourself, and take more jobs on your shoulder? Actually the benefits of doing this, has more to do with you.

First of all, the next time you want to job hop, you will have to update your resume. And which one is better? Writing big stuff? or writing the smaller mundane stuff? The big stuff will make you a better marketable person.

Then, if your boss, sees that you are pro active in thinking about problems, harder than anyone else, you stand a chance for better raise, or compensation. But nevertheless, this should not be your main target. Money in fact is not the main target.

So what is?

So that you could do the cool stuff. Even if you are working for a company. If you could pull the resources for your cool project, it would be much more fun.

For programmers, we want to be cracking harder problems, using the latest, or whatever tools we have to pull it off. Hack into whatever necessary to do things. These are the cool stuff.

For a record, I did in fact manage to pull out 2, 3 major projects in my career so far. It's big enough, that I could just go to my next employer, and confidently get the job.

So, if you are stuck in the rut with boring stuff, it's time to think big, and solve the next big problem.

Friday, June 16, 2006

Impractical Thinking

I overheard my partner that day, telling an intern to be practical. This guy was doing some web development using Ruby On Rails. I had just asked him to try it out, to see whether this platform could really speed things up.

Now, impractical thinking might not apply to all programmers, but sometimes, you just got to be the opposite. I have a habit of throwing off whatever I have known, and try to figure out, whether there are newer ways to solve the problem.

Telling programmers to be practical is like limiting their capabilities to think further ahead. It's like telling an artist to paint a portrait using a limited color set. He can't go on explore mode using his/her imagination.

Problems are aplenty, but how you solve it requires creativity. And creativity does not come easy, or just when think you want to be productive. In fact, creativity and productivity are opposite of each other. If you want to know what productive means, take a look at the factory. I used to work at one, manufacturing air conditioners. And I had observed how engineers will try to make a process faster. The main thing, is how you handle the assembly line, how fast you can get your raw materials coming in. If you can manage these well, productivity kicks in.

But creativity is a whole new ball game. It requires thinking of solving problems, using methods that needs exploration, and which are, might be totally new to yourself.

For eg, I was thinking of this web UI which list down a list of file names, and those file names can be changed via the web interface. I had done it using the previous method, which is the user clicks on the link 'Rename' and this will fetch another page with the rename functionality.

I did played with some ajax, made some calls to the server, and had it work. So I was thinking, could i just rename the file from the web page instead? Just like when you rename a file in windows explorer?

So, off I went, figuring out how to do this using ajax. And the results were good. I had a working interface where you could just rename the file by clicking on the filename(which is a text input), and once the focus changes from the input text, a request is sent via ajax to rename it.



So, to be really creative, you just have to be impractical in your thinking. While these might not apply to all areas of your life, but it's good to throw out old thinking, and put in a set of new ones.

Friday, May 26, 2006

A Function of Life

The reason I started this blog, was to document my exploration into LISP. It's basically a programming language, and is the second oldest programming language after FORTRAN, if I am not mistaken.

Now, I want to state, I am still quite a beginner, but eager to learn, and implement something useful with LISP. So, if there's anything wrong, or you disagree with what I wrote, I am willing to listen.

LISP stands for List Processing. Here are some code samples on how LISP looks like:

(+ 1 2 3)
(* (+ 1 2) (/ 10 20))

Now, from what I have read so far, the essence of LISP is functions. There's a blur distinction between what is a data, and what is a function. A function could be a data that can be passed to a higher level of function.

So, in this way, you could build bigger applications, by writing functions, upon functions. Now, this is not something new. You can write functions in other programming languages, and do the same thing. But what makes LISP different is that functions are treated as data too.

And just by looking at this perspective, you could relate on how the real life is modelled. A high rise building, a car, a society can be broken down into it's sub components, which does something.

That activity can be at the lowest level, or intermediate level. But all those components has to contribute, or does it function in order to support that entity.

I haven't really tackled on how to write programs for LISP. Should I think of all programs as lists? or should I think of them as functions, build from the lowest until the highest level.

So, when you take a walk next time...just think about it. The complexity we have around us, are build upon functions right from the atom, to items that we could see, and wonder.

Saturday, May 13, 2006

Real programmers




Now, we got tons of people into programming, but what really constitute a real programmer? Now I choose the word real to differentiate between mass mediocre ones, and the great ones.

I dare say, real programmers are the ones who understood what is meant by computing. What that word really means. It's not just hammering out some codes on your IDE, and getting some program to work. Yes, of course, for a living, you have little choice but to keep on hammering up some codes, so the rest of the non geek world will pay you, so you could get your next XBox 360.

Now, in college, there was two subjects that was most dreaded by students.First, Theory of Computation, and second Compiler Design. I was initially, as a freshman, figuring in my head, wow...these two subjects must be the killer subjects. It would be hard to score good grades.

But after going through it, in fact, I found the real essence of computing, and how computing machines work, thanks to our father of modern computer, Alan Turing. Basically, computers are good problem solvers by repetitively solving chunks of problems. He had the foresight of inventing a machine that could solve scientific calculations. Now how he figured out that, is by thinking, "could we break down calculations into discrete problems that could be feed into the machine to be solved?".

Another thing that opened up my mind, was when I was studying compiler design. Although I could just finished 2 out of 7 phases of creating my own compiler, but that is enough to make me realise, the beauty of grammars (used to define how to parsed language syntax), and recursion.
In fact, our natural languages like english, has their own grammar (which we all learnt since young). And through this grammar, is how we construct complex sentences.

So back to the real programmer issue. I won't consider a programmer knows his stuff, until he understood turing machines and compiler design. It's not that it's necessary to know, but knowing this brings you to a higher level, where you could see things more clearly.

http://www.turing.org.uk/turing/

Wednesday, May 10, 2006

ASP.NET Love Hate Relationship




Anyone who has done pure scripting server side e.g. PHP ASP, must have gone pretty bonkers when seeing asp.net code for the first time. I was. It was initially thrilling jumping into asp.net, as you would believe all the great things it will bring.

But then when you look at the code, you can't really make head or tail, or figure out where to start?

Now, coming from a traditional web programming school of thought, you always think top to bottom. Yep, that's how the script runs, from the first line on top, and then just all the way down. If anything goes wrong, you know which line to fix. You also know how to add more "if else" just to make sure some path of logic is executed. But in asp.net, it's just not there.

You have to think events and triggers for those. And how to handle them.

And the worst of it, form inputs, are being replaced by proprietery asp.net tags.

I even went to the point of searching "hate dot net" to justify my feelings. And mind you, I had 2 years doing ASP and some vb dll com programming.

But it had to be overcome, because the benefits are just there. You no longer need to deploy dlls the hard way, just dump them into /bin folder. And there's tons of other benefits as well.

One more thing I need to say. I am not really a fan of drag and drop programming. Yes, I know all of you guys might have seen those MVPs (Microsoft Valued Professionals) doing some presentation hype, and trying to convince you, how easy it seems. hahaha...yes, IDEs does make your life better, but does that mean, anyone can just open the IDE, drag and drop some controls and voila, you have a web app?

Coming from the pre asp.net era, we have to hack javascripts, form inputs and much more just to do form validation, form posting, ajax and some DHTML effects. Yet if I were to advised which language should you start first before jumping into web programming, it would be either asp or php. Because this way, you learn the intricacies of how the lower level work. Once you got a solid foundation of how this layer work, asp.net will be a breeze. And better still, you could hack into certain asp.net controls to make it work for you.

And I heard from a guy who is managing a team of developers, mentioned, his top developer still don't really like dot net for projects. Why? Just because you don't have full control on all the logic on your pages. To do really cool stuff with your own forms, you just have to have certain parts of the page logic. ASP.net controls makes it harder, hence why all the hacks are needed on the controls.

ASP.net could really make faster apps, but that doesn't make it a better app. How good the app is, still depends on the programmer.

Wednesday, April 19, 2006

First Posting

Well, this is my first posting. The reason I started this blog is to document my findings in LISP. I just got myself Paul Graham's ANSI Common Lisp, and I assure you, this is not just another programming book.