Printf the hard way



  • I just found this in some vendor-supplied demo code. Language is Delphi (pascal). I've kept the original indenting, line spacing and non-existent comments (modulo Community Server behaviour).

    function HexWrdToStr(Dval : integer) : string;
    var i : integer;
    retstr : string;
    begin
    retstr := '';
    i := (Dval AND $F000) DIV $1000;
    case i of
       0 : retstr := retstr + '0';
       1 : retstr := retstr + '1';
       2 : retstr := retstr + '2';
       3 : retstr := retstr + '3';
       4 : retstr := retstr + '4';
       5 : retstr := retstr + '5';
       6 : retstr := retstr + '6';
       7 : retstr := retstr + '7';
       8 : retstr := retstr + '8';
       9 : retstr := retstr + '9';
       10 : retstr := retstr + 'A';
       11 : retstr := retstr + 'B';
       12 : retstr := retstr + 'C';
       13 : retstr := retstr + 'D';
       14 : retstr := retstr + 'E';
       15 : retstr := retstr + 'F';
    end;
    i := (Dval AND $F00) DIV $100;
    case i of
    {same 16-line case as above}
    end;
    i := (Dval AND $F0) DIV $10;
    case i of
    {same 16-line case as above}
    end;
    i := Dval AND $F;
    case i of
    {same 16-line case as above}
    end;
    HexWrdToStr := retstr;
    end;

    That's 84 lines and some very untidy string manipulation, and there's a similar one for bytes. Yes, Delphi does have a printf equivalent.



  •  Or, of course, you could use the built-in IntToHex() function



  • @gramie said:

     Or, of course, you could use the built-in IntToHex() function

    That's the one !

    A secondary WTF is that they're in a unit where they have no relationship at all to the the unit's function (which is a wrapper to a dll). They're only used in the mickey-mouse demo app, so if he had to have them why not just dump them in with the rest of the app code? I've just written a component based on this guy's library, and now I discover that that also is crap and I have to write my own library as well. [/rant]



  • @SenTree said:

    A secondary WTF is that they're in a unit where they have no relationship at all to the the unit's function (which is a wrapper to a dll). They're only used in the mickey-mouse demo app, so if he had to have them why not just dump them in with the rest of the app code? I've just written a component based on this guy's library, and now I discover that that also is crap and I have to write my own library as well. [/rant]

    Sounds like a case of Developmenstruction to me.



  • @SenTree said:

    ... I've just written a component based on this guy's library, and now I discover that that also is crap and I have to write my own library as well. [/rant]

    You were surprised? If I had found that piece of crap, I would have assumed everything else written by him crap as well.



  • @SQB said:

    @SenTree said:
    ... I've just written a component based on this guy's library, and now I discover that that also is crap and I have to write my own library as well. [/rant]

    You were surprised? If I had found that piece of crap, I would have assumed everything else written by him crap as well.

    I tried the library first, then investigated the code when the problems arose. In fairness, this vendor's core products are excellent, I think this demo was thrown together in a hurry, possibly by someone not too familiar with Delphi.



  • @Indrora said:

    Sounds like a case of Developmenstruction to me.

    I doubt it's even that.

    @Indrora said:

    I'm pretty sure calling a demo a micky-mouse demo makes you a furry.

    You say that like it's a bad thing ! In UK English, applying the term 'mickey-mouse' to something suggests shoddiness, unfitness for purpose, or similar connotations.



  • @SenTree said:

    In UK English, applying the term 'mickey-mouse' to something suggests shoddiness, unfitness for purpose, or similar connotations.

    Indeed, but only God knows why!



  • @Thief^ said:

    @SenTree said:

    In UK English, applying the term 'mickey-mouse' to something suggests shoddiness, unfitness for purpose, or similar connotations.

    Indeed, but only God knows why!

    Well, He should do. God is an Englishman, after all.



  • @SenTree said:

    @Thief^ said:

    @SenTree said:

    In UK English, applying the term 'mickey-mouse' to something suggests shoddiness, unfitness for purpose, or similar connotations.

    Indeed, but only God knows why!

    Well, He should do. God is an Englishman, after all.

    No she's not

Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.