Use debugger to see what values are assigned to variables in every step. This will help you in fixing issues easily. Also it is better to have mental model of your code.
Try with "rem = binary % 2" instead of "rem = binary % 10". Just looked at your code, did not test the line, hope it works.