Hello everyone,

I have to write a code to creat a pseudo assembler..I have an input file from which i read the assembly instructions from there i get the register numbers ...i have to convert that into machine code..i.e binary form...
Now my problem is that i can find the register values convert them into integer.....i can convert the integer into binary using itoa.......
but if my register number is 5 i am getting the binary as 101....but i need the binary to be 00101....can anyone please explain how to get the zero padding done......

Hello everyone,

I have to write a code to creat a pseudo assembler..I have an input file from which i read the assembly instructions from there i get the register numbers ...i have to convert that into machine code..i.e binary form...
Now my problem is that i can find the register values convert them into integer.....i can convert the integer into binary using itoa.......
but if my register number is 5 i am getting the binary as 101....but i need the binary to be 00101....can anyone please explain how to get the zero padding done......

for adding those extra zeroes u can just write a function which which check the lenght of the inputted binary string and accordingly will put extra zeroes at the beginning to make it of the particular length that u want.

void zeroPadding(char *bin, char *paddedNum, int requiredLen)
{
    /*paddedNum should have enough memory allocated(==requiredLen+1)*/
    
    int inLen = strlen(bin);
    int i;
    for(i=0;i<requiredLen-inLen;i++){
         *paddedNum = '0'; paddedNum++;
    }
    while(*bin){
           *paddedNum = *bin;
           paddedBin++;
           bin++;
     }
     *paddedNum = 0;//string termination//
     return;
}

use it as

char *bin = "101"; //say
char *newBin = (char *)malloc(6); //e.g. if u want the binary of length 5
zeroPadding(bin, newBin, 5);

hope that helps................

Thanks A Million!!!:cool:

Thanks A Million!!!:cool:

u r welcome.
n plz mark this thread as solved if u think u got your answer :)

Another way using std::string;

#include<iostream>
#include<algorithm>
#include<string>

using std::string;
using std::cout;

int toChar(char c){
	return c + '0';
}

string convertBase10_To(int changeToRadix, int base10Number, int paddingEveryUnits = 4)
{
	string Str = "";
   //Did not want to support radix greater than 10. You do it.
	if(!changeToRadix || changeToRadix > 10)
		return Str;
	//Check if valid paddingNumber
	paddingEveryUnits = paddingEveryUnits < 0 ? 1 : paddingEveryUnits;

	while(base10Number){
		Str += toChar(base10Number % changeToRadix);
		base10Number /= changeToRadix;
	}
		
	while(Str.size() % paddingEveryUnits != 0) //Traditionally its multiple of 4
		Str += '0';
	
	std::reverse(Str.begin(),Str.end());

	return Str;
}
int main()
{
	cout << convertBase10_To(2,15);

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.