Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

im working on an assmebler project that i have and i need to translate binary machine code that i have to a "weird" 4 base code for example if i get binary code like this "0000-10-01-00" i should translate it to "aacba"

00=a

01=b

10=c

11=d

i have managed to translate the code to 4 base code but i dont know how to continue from there or if this is the right way to do it,...

adding my code below

void intToBase4 (unsigned int *num)
{
  int d[7];
  int j,i=0;
  double x=0;
  while((*num)>0)
  {
    d[i]=(*num)%4;
    i++;
    (*num)=(*num)/4;
  }
  for(x=0,j=i-1; j>=0; j--)
  {
    x += d[j]*pow(10,j);
  }
  (*num)=(unsigned int)x;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

I've included a little 32-bit to num to letter converter for you to grasp the basics. It works a single "32-bit number" at a time. You could use this as a basis for an array based solution like you have half way done in your example, or change the type to be bigger, or whatever. It should show you roughly what you need to do:

void intToBase4 (uint32_t num, char *outString)
{
  // There are 16 digits per num in this example
  for(int i=0; i<16; i++)
  { 
    // Grab the lowest 2 bits and convert to a letter.
    *outString++ = (num & 0x03) + 'a';

    // Shift next 2 bits low
    num >>= 2;
  }
  // NUL terminate string.
  *outString = '';
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...