MaskOct2Dec



  • Some days I think "this not that pretty", some days I facepalm, and there are days when I say WTF out loud. Today was the last.



    I present you, the maskOct2Dec function:

    int var = atoi(string);
    var = maskOct2Dec(omask);
    
    //-------------------
    
    int maskOct2Dec(int omask)
    {
       int i, dmask = 0;
       int temp, result;
    
       temp = omask;
    
       for (i = 0; i < 4; i++)
       {
          dmask <<= 3;
    
          temp -= (result = (temp / pow(10, (3 - i)))) * pow(10, (3 - i));
    
          dmask |= result;
       }
    
       return dmask;
    }


    (lookup the strtol function if you want a good replacement)


  • @Daid said:

    int var = atoi(string);
    var = maskOct2Dec(omask);

    Why is the call to atoi even there? What is omask?

    @Daid said:

    (lookup the strtol function if you want a good replacement)

    That function does something else entirely.



  • @Lingerance said:

    @Daid said:

    int var = atoi(string);
    var = maskOct2Dec(omask);

    Why is the call to atoi even there? What is omask?

    I suppose it should've been

     

    int var = atoi(string);
    var = maskOct2Dec(var);
    

    @Daid said:
    (lookup the strtol function if you want a good replacement)
    That function does something else entirely.
     

     

    int var = strtol(string,NULL,8); 


  • A. Does this function even compile?
    B. Is it just me or does this line do absolutely nothing? 
    temp -= (result = (temp / pow(10, (3 - i)))) * pow(10, (3 - i));



  • @SuperAnalyst said:

    A. Does this function even compile?
    Think so. Why shouldn't it? 
    B. Is it just me or does this line do absolutely nothing?
    temp -= (result = (temp / pow(10, (3 - i)))) * pow(10, (3 - i));

     

    It sets result to floor(temp/pow(10,3-i)), multiplies result with pow(10,3-i) and subtracts the product from temp, just what it's intended to do.

    I must say, for an utterly braindead thing, it's done in a rather clever way.



  • @SuperAnalyst said:

    A. Does this function even compile?
    B. Is it just me or does this line do absolutely nothing? 
    temp -= (result = (temp / pow(10, (3 - i)))) * pow(10, (3 - i));

    A. Yes it compiles. Sadly enough.
    B. It:
    -Does (in the first run): floor(temp / 1000)
    -Assigns the result to "result"
    -Multiplies that result with 1000
    -And then subtracts that from temp.

    So, it extracts decimals, I hope.



  • @Ilya Ehrenburg said:

    I must say, for an utterly braindead thing, it's done in a rather clever way.

    Perhaps I should clarify: [i]That particular line of code[/i] is done in a rather clever way. The function as a whole

    1. probably shouldn't exist at all,
    2. could be done in a much better way if it is necessary after all.


  •  Why would you want to convert October to December?



  • @pbean said:

     Why would you want to convert October to December?

    'Cause December is cooler? (in the northern hemisphere)


  • @Ilya Ehrenburg said:

    @pbean said:
     Why would you want to convert October to December?
    'Cause December is cooler? (in the northern hemisphere)



  • Pfft, that's doing it the hard way. Here's the right way to convert an octal string to an integer (also found in real-life someone-got-paid-for-this code):

    int FromOctString(char *octnumber)
    {
            char chdigit[1]; int iret; char octnum[3];
            strcpy(octnum, octnumber);
            sprintf(chdigit, "%c", octnum[0]);
            iret = atoi(chdigit) * 64;
            sprintf(chdigit, "%c", octnum[1]);
            iret += atoi(chdigit) * 8;
            sprintf(chdigit, "%c", octnum[2]);
            iret += atoi(chdigit);
            return iret;
    }
    

  • Discourse touched me in a no-no place

    @pbean said:

     Why would you want to convert October to December?

    To change halloween to christmas?



  • @PJH said:

    @pbean said:

     Why would you want to convert October to December?

    To change halloween to christmas?
     

    Funny how that works out. Must be a pagan conspiracy.


  • Discourse touched me in a no-no place

    @Someone You Know said:

    @PJH said:
    @pbean said:
    Why would you want to convert October to December?
    To change halloween to christmas?


    Funny how that works out. Must be a pagan conspiracy.
    The fact that someone got that (these days) makes me happy



    I'm easily pleased. At times.



  • because oct(31)==25?



  • @Indrora said:

    because oct(31)==25?
     

    Thank you for explaining the joke that everybody else got right away because we've all seen it 20,000 times.

     



  • @Aaron said:

    @Indrora said:

    because oct(31)==25?
     

    Thank you for explaining the joke that everybody else got right away because we've all seen it 20,000 times.
    And not even explaining it correctly, for that matter.


  • In that case, I got it the 20,001st time -- must gently lie down and do a ROFL now.

    Wait. This WTF-ee actually grasps the concept of number systems. Not all first-graders can say that.



  • @SuperAnalyst said:

    A. Does this function even compile?

    Some would say, the great thing about compilers is that they aren't bothered by a little insanity.  Or even a lot.  They just care about syntax, and just enough about semantics to make sure something happens - but they don't care at all if that something was what the programmer *wanted*, or merely what the programmer told it to do.

    Others feel this is actually the worst aspect of compilers.

    I'm not certain where I stand on this issue.


Log in to reply