Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

simple c program issue

Options
  • 12-12-2023 3:06pm
    #1
    Registered Users Posts: 18


    Hope im in the correct room.


    Hi All

    Ive been wrecking my brain all day to try and check this array and where there exists only one char =>add second char 0 to it. whilst leaving the others alone.

    char usable_MAC[19] = "FF:35:0:E1:80:FF";
    

    Ive shown below my last attempt but no success.

    result => i want to see is where there is "0" should now see "00" whilst leaving the rest along.

    Note-> this single char could be anywhere in this array

    BELOW IS WHAT I WANT as RESULT

    char usable_MAC[19] = "FF:35:00:E1:80:FF";
    


     int desiredSize = 17;


       // Padding character

       char paddingChar = '0';


       // Create a new array with padding

       char padded_MAC[desiredSize];


       // Initialize the padded array with null characters

       memset(padded_MAC, '\0', desiredSize);


       // Copy characters from the original MAC address to the padded array

       int i, j;

       for (i = 0, j = 0; i < strlen(usable_MAC) && j < desiredSize - 1; ++i, ++j) {

           padded_MAC[j] = usable_MAC[i];


           // Check for a single character and pad with '0'

           if (usable_MAC[i] == ':' && usable_MAC[i + 2] == ':' && usable_MAC[i + 1] != '0') {

               padded_MAC[++j] = paddingChar;

           }

       }


       // Null-terminate the padded array

       padded_MAC[desiredSize - 1] = '\0';


       // Display the padded MAC address

       printf("Padded MAC: %s\n", padded_MAC);


    Thanks in advance



Comments

  • Registered Users Posts: 311 ✭✭ThreeGreens


    I'm struggling to understand exactly what you're trying to do.


    But I suspect your problem is that putting a zero into the char array creates a null terminator.


    So if your array is FF:35:00:E1:80:FF but the first "0" isn't the number 0 but rather the value 0, then your printf statement sees it as a null terminator and even though digits are in the subsequent locations, they aren't printed.


    The way to check this would be to put a break point where your printf statement is and examine the contends of padded_mac one by one to see what it contains in the subsequent memory locations.



  • Registered Users Posts: 3,407 ✭✭✭monkeybutter


    you need to iterate J manually for a start if you find a :Char: situation



  • Registered Users Posts: 18 mike33


    Hi guys thanks for getting back.


    Bacically Im reciving  the following into my code =>FF:35:0:E1:80:FF
    

    But if u look across the array one of the values is 0 but i needs it to be 00.

    SO before i continue i need to format this arry from FF:35:0:E1:80:FF to FF:35:00:E1:80:FF
    However this could happen anywhere in the array (Call it an error if u will)
    
    So i need to be able to check this array each time and make sure it contains to digits in each loacation.
    Where there may be one then I pad with "0".
    
    Hope im making sense.
    




  • Registered Users Posts: 19,057 ✭✭✭✭Donald Trump


    strtok on the colon. Simples. Figure the rest out yourself. Either pointer arithmetic or scan from the string as hex and print back out at hex with a formatted print that will pad.


    Or just work backwards through the string. Easy peasy



  • Registered Users Posts: 6,804 ✭✭✭timmyntc


    if (usable_MAC[i] == ':' && usable_MAC[i + 2] == ':' && usable_MAC[i + 1] != '0') {

    Is bolded a typo in your post?



  • Advertisement
  • Registered Users Posts: 19,057 ✭✭✭✭Donald Trump


    I did up a solution for the craic. Less than 40 lines for complete program. Works on strings even when there are no numbers between the colons.

    E.g

    char usable_MAC[MACSTRSIZE] = "::1:23::";

    Will print out

    00:00:01:23:00:00


    Only thing it's missing is that it doesn't check for more than 2 digits. That wasn't in the requirements though.



  • Registered Users Posts: 18 mike33


    All sorry for delay in replying ... Got caught up in Xmass stuff.

    Just to say, sorted the problem => went back to drawing board and started again.

    As stated above focused on the colons and worked from there.

    Thanks all for your time and ideas.



Advertisement