Asymmetric Hex Encoding



  • I was scanning through a coworkers code and found the following pair of functions.

    They are beautifully asymmetric in the amount of coding paranoia and sensitivity to character code ordering.


     

    DWORD htoi( char *pText)
    {
        DWORD    RetVal = 0;


        //    Add up the characters.

        while( *pText)
        {
            const char    *p = strchr( "01234567890abcdefABCDEF", *pText);


            //    Check the value is a valid hex character.

            if( !p)
            {
                return RetVal;
            }


            //    Else multiply the total so far by 16.

            RetVal *= 16;


            //    Else add the correct value to the return value.

            switch( *p)
            {
            case    '0':
                break;

            case    '1':
                RetVal += 1;
                break;

            case    '2':
                RetVal += 2;
                break;

            case    '3':
                RetVal += 3;
                break;

            case    '4':
                RetVal += 4;
                break;

            case    '5':
                RetVal += 5;
                break;

            case    '6':
                RetVal += 6;
                break;

            case    '7':
                RetVal += 7;
                break;

            case    '8':
                RetVal += 8;
                break;

            case    '9':
                RetVal += 9;
                break;

            case    'A':
            case    'a':
                RetVal += 10;
                break;

            case    'B':
            case    'b':
                RetVal += 11;
                break;

            case    'C':
            case    'c':
                RetVal += 12;
                break;

            case    'D':
            case    'd':
                RetVal += 13;
                break;

            case    'E':
            case    'e':
                RetVal += 14;
                break;

            case    'F':
            case    'f':
                RetVal += 15;
                break;
            }


            //    Increment the text pointer.

            pText++;
        }


        //    Return the return values.

        return RetVal;
    }

    char itoh( BYTE b)
    {
        if( b <= 9)
        {
            return b + '0';
        }
        else
        {
            return b + '0' + 7;
        }
    }


  • :belt_onion:

    @mikedjames said:

     

            // Check the value is a valid hex character.
            if (!p)
            {
                return RetVal;
            }

     

    And then my head exploded. Seriously, why would you return a partial result instead of an error code indicating failure (or even better, an exception)?




  • :belt_onion:

    @mott555 said:

    This tag made my head explode. WTF is that first character and why would anybody anywhere find it useful and think it belongs in our character sets?

     

    I'm assuming it's not one character but way too many combining characters.

     



  • @mott555 said:

    @heterodox said:

    Filed under: ‧̴̵̶̷̸̡̢̧̨̛̖̗̘̙̜̝̞̟̠̣̤̥̦̩̪̫̬̭̮̯̰̱̲̳̹̺̻̼̀́̂̄̃̅̆̇̈̉̊̋̌̍̎̏̐̑̒̓̔̽̾̿̀́͂̓̈́̕̚͠͡ͅ‧̴̵̶̷̸̡̢̧̨̛̖̗̘̙̜̝̞̟̠̣̤̥̦̩̪̫̬̭̮̯̰̱̲̳̹̺̻̼̀́̂̄̃̅̆̇̈̉̊̋̌̍̎̏̐̑̒̓̔̽̾̿̀́͂̓̈́̕̚͠͡ͅ, *flamebait
     

    This tag made my head explode. WTF is that first character and why would anybody anywhere find it useful and think it belongs in our character sets?

     

     

    It is the ultimate composite character — it includes all the combining diacriticals, etc. That's actually two of them in succession.

     



  • @Someone You Know said:

    It is the ultimate composite character
     

    So the first one is... the penultimate composite character?


  • :belt_onion:

    @dhromed said:

    So the first one is... the penultimate composite character?

    Well played, sir. And in any case, to me it looks like something that's exploded (like my head). And I am SHOCKED to see that we are off-topic.

     



  • @heterodox said:

    @mikedjames said:

     

            // Check the value is a valid hex character.
            if (!p)
            {
                return RetVal;
            }

     


    And then my head exploded. Seriously, why would you return a partial result instead of an error code indicating failure (or even better, an exception)?

    You read the comment rather than the code. A typical junior mistake :-)

     


  • BINNED

    @heterodox said:

    And then my head exploded. Seriously, why would you return a partial result instead of an error code indicating failure (or even better, an exception)

    IIRC, atoi does it the same way, i.e. atoi("123asd") returns 123. So consistency with old C crap.
    Also, C doesn't have exceptions, only return values and errno. (And considering exceptions in C++ in comparison to other languages, this might actually be a Good Thing)

     



  • @dhromed said:

    @Someone You Know said:

    It is the ultimate composite character
     

    So the first one is... the penultimate composite character?

     

    Your mom is the penultimate composite character.

     



  • @topspin said:

    And considering exceptions in C++ in comparison to other languages, this might actually be a Good Thing

    What's wrong with exceptions in C++?



  • @Someone You Know said:

    Your mom is the penultimate composite character.
     

    Highly dubious.



  • @topspin said:

    @heterodox said:

    And then my head exploded. Seriously, why would you return a partial result instead of an error code indicating failure (or even better, an exception)

    IIRC, atoi does it the same way, i.e. atoi("123asd") returns 123. So consistency with old C crap.
    Also, C doesn't have exceptions, only return values and errno. (And considering exceptions in C++ in comparison to other languages, this might actually be a Good Thing)

     

    I think atoi's behaviour with invalid input is undefined.



  •  All I did was typed in a tag, and then changed my mind, selected the text and typed the string "C++",using the "select text and type replacement text is text replacement" paradigm.

    As I did it I could see some strange stuff flickering in the tag line. 

    But as what I saw when it settled was what I meant it to say, I hit submit. Sorry.

    Back on topic- being exposed to these kinds of code snippets, along with code in VB6 (too old, unsupportable) , Borland C++ builder 5 dialect of C++ (bizarre restrictions) , Managed C++ from MS (not-quite-C++ presented as C++)   has made me doubt my C++ sanity. 

    I ended up having to wash my mind out by going away and reading a C++ primer to remind me of how code is  meant to be written in C++. 

    And getting over having a cold helped.

     

     



  • Yes, they meant strtol, obviously.

    Anyway, I wonder which language this is in. It must be some kind of C, only without any libraries.



  • @Hatshepsut said:

    I think atoi's behaviour with invalid input is undefined.

    But "123asd" is valid input for atoi, which “convert[s] the initial portion of the string.”

     


  • Discourse touched me in a no-no place

    @Hatshepsut said:

    I think atoi's behaviour with invalid input is undefined.
    Nope - it's well defined. What isn't is if the result of the conversion cannot be stored in an int: @'97 draft said:
    7.14.1 String conversion functions


    1 The functions atof, atoi, atol, and atoll need not affect the value of the
    integer expression errno on an error. If the value of the result cannot be
    represented
    , the behavior is undefined.



  • @Someone You Know said:

    It is the ultimate composite character — it includes all the combining diacriticals, etc.
    Not anymore - Unicode 6.0 added more combining characters, as you can see in my tags (note: tag may not display properly on older operating systems and or browsers).



  • @ender said:

    (note: tag may not display properly on older operating systems and or browsers).
     

    The tag doesn't display properly on modern systems either.



  • @dhromed said:

    The tag doesn't display properly on modern systems either.
    Works on Windows 7 in Internet Explorer 9 and Firefox 8. It also used to work in Opera, but looks like something broke in 12 alpha.



  • @ender said:

    (note: tag may not display properly on older operating systems and or browsers)

    How do I know if it's displaying properly or not, when I don't have any reference material against which to compare it?



  • @too_many_usernames said:

    How do I know if it's displaying properly or not, when I don't have any reference material against which to compare it?
    Here's a reference rendering:
    ‧̴̵̶̷̸̡̢̧̨̛̖̗̘̙̜̝̞̟̠̣̤̥̦̩̪̫̬̭̮̯̰̱̲̳̹̺̻̼͇͈͉͍͎̀́̂̄̃̅̆̇̈̉̊̋̌̍̎̏̐̑̒̓̔̽̾̿̀́͂̓̈́͆͊͋͌̕̚ͅ͏͓͔͕͖͙͚͐͑͒͗͛ͣͤͥͦͧͨͩͪͫͬͭͮͯ͘͜͟͢͝͞͠͡



  •  what made MY head explode was the way the Penultimate character loaded - first, it was a one-liner. By the time i got to your post, it was a two-line character. By the time i scrolled up and found it in the post where it was displayed the first time, it was a full-blown (pun intended) 10-line madness. really, wtf?



  • Hint: approximately center the character in browser window, then switch to another program (that covers the entire browser), and then back to the browser.



  • @ender said:

    @too_many_usernames said:
    How do I know if it's displaying properly or not, when I don't have any reference material against which to compare it?
    Here's a reference rendering:
     

    So, are there really that many more above-the-character combining marks than below-the-character marks, or is that an illusion? Conceptually, you'd think that it should be possible to put any mark in either location, even if no language actually uses it that way.


  • Garbage Person

    @Someone You Know said:

    @ender said:

    @too_many_usernames said:
    How do I know if it's displaying properly or not, when I don't have any reference material against which to compare it?
    Here's a reference rendering:
     

    So, are there really that many more above-the-character combining marks than below-the-character marks, or is that an illusion? Conceptually, you'd think that it should be possible to put any mark in either location, even if no language actually uses it that way.

    We can update Unicode to support hypothetical languages when we encounter aliens using languages that Unicode doesn't account for.

    ... And I just came up with a cool little subplot for my novel, which is set in the adjustment phase post-first-contact.

  • BINNED

    @tdb said:

    @topspin said:
    And considering exceptions in C++ in comparison to other languages, this might actually be a Good Thing

    What's wrong with exceptions in C++?

    It's quite late here and I've had too much hot wine at the company xmas party, so I can't quite put a finger on it. Search the FQA for some inspiration...

    It may be just that (as C++ is insanely complex to begin with) it's quite hard to write exception safe code, if you haven't read through most of the books from Sutter et al. Reminds me of a quote about including a smart pointer template in the standard, because writing one yourself is "so hard, even Scott Meyers can't get it right". (Whose bugs might or might not have been about exception safety - I don't remember, but it's a reasonable guess)

    At least I've seen a lot of shit from my coworkers who seem to think they know exceptions work (or maybe just don't care), yet they don't.

     



  • @topspin said:

    Also, C doesn't have exceptions, only return values and errno.
    Initially misread as "only return values and emo." It seemed appropriate somehow.



  • @ender said:

    @dhromed said:
    The tag doesn't display properly on modern systems either.
    Works on Windows 7 in Internet Explorer 9 and Firefox 8. It also used to work in Opera, but looks like something broke in 12 alpha.
     

    IT WAS A JOKE.

    Because it looks like something Cthulhu would shit out, see.

     



  • @dhromed said:

    Because it looks like something Cthulhu would shit out, see.

    Now, let me tell you that I check  Cthulhu's stools very day and it doesn't look like that at all.  I also check the stools of every other dark god so you are very wrong in your asumption.



  • @serguey123 said:

    @dhromed said:

    Because it looks like something Cthulhu would shit out, see.

    Now, let me tell you that I check  Cthulhu's stools very day and it doesn't look like that at all.  I also check the stools of every other dark god so you are very wrong in your asumption.

     

    Not sure if worst job or most epic job.

     



  • @dhromed said:

    @serguey123 said:

    @dhromed said:

    Because it looks like something Cthulhu would shit out, see.

    Now, let me tell you that I check  Cthulhu's stools very day and it doesn't look like that at all.  I also check the stools of every other dark god so you are very wrong in your asumption.

     

    Not sure if worst job or most epic job.

    A bit of both I guess, it can get a bit messy when any of them have the runs



  • @serguey123 said:

    @dhromed said:
    @serguey123 said:
    @dhromed said:
    Because it looks like something Cthulhu would shit out, see.
    Now, let me tell you that I check  Cthulhu's stools very day and it doesn't look like that at all.  I also check the stools of every other dark god so you are very wrong in your asumption.
    Not sure if worst job or most epic job.
    A bit of both I guess, it can get a bit messy when any of them have the runs

    Ugh, that must drive you insane!


    although I have to ask: What are the health benefits like?



  • @Xyro said:

    @serguey123 said:
    @dhromed said:
    @serguey123 said:
    @dhromed said:
    Because it looks like something Cthulhu would shit out, see.
    Now, let me tell you that I check  Cthulhu's stools very day and it doesn't look like that at all.  I also check the stools of every other dark god so you are very wrong in your asumption.
    Not sure if worst job or most epic job.
    A bit of both I guess, it can get a bit messy when any of them have the runs
    Ugh, that must drive you insane!


    although I have to ask: What are the health benefits like?

    You get an immortal life (your soul gets dammed in the process) but they add free minions so all in all, pretty good but let me tell you that the severance package is very bad


Log in to reply