david lightman

Ranch Hand

Posts: 82

posted 12 years ago

How would I pad the length something to be a multiple of 4?

for example I have a variable

int iLength;

and I get the length of a string myString and pass it to iLength, like

iLength = myString.length();

how to I then pad the iLenght to always be a mulitple of 4

thanks!

for example I have a variable

int iLength;

and I get the length of a string myString and pass it to iLength, like

iLength = myString.length();

how to I then pad the iLenght to always be a mulitple of 4

thanks!

posted 12 years ago

Or if you always want the

*minimum*length, you can check to see if it's already divisible by 4 before adding..."We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

david lightman

Ranch Hand

Posts: 82

posted 12 years ago

thanks Marc!

forgive me but to COMPLETELY understand, this code is basically saying

if(i%4 != 0) ---> if this number i is divided by 4 and the remainder is not zero,

then i += (4 - i%4); ---> divide i by 4 and and substract this number from 4

and place in back into the i.

so if i was 17

it would first say no there is a 1 left over..then it would subract the one from the 4 to give it three then add the three to the original 17 giving it a 20.

right?

I think I understand how it works, thanks

forgive me but to COMPLETELY understand, this code is basically saying

if(i%4 != 0) ---> if this number i is divided by 4 and the remainder is not zero,

then i += (4 - i%4); ---> divide i by 4 and and substract this number from 4

and place in back into the i.

so if i was 17

it would first say no there is a 1 left over..then it would subract the one from the 4 to give it three then add the three to the original 17 giving it a 20.

right?

I think I understand how it works, thanks

posted 12 years ago

Yes, with the following addition in bold...

i += (4 - i%4); ---> divide i by 4 and substract this number from 4,

i += (4 - i%4); ---> divide i by 4 and substract this number from 4,

**then add the result to i**and place back into the i.*~Joe Strummer*

sscce.org