开发者

How to convert a string to binary?

I'm working on a C project and I have some instructions of type:

add $1 $2 $3

So I'm reading the line as a string, parsing through it and have a corresponding integer for add, say - 2. Could anyone please tell me how I could convert this to binary in order to write it to a file?

The registers are 5 bits and the operation is 6 bits. The total will be 32 (the last 10 bits are unused).

So the registers are stored in say op[] = "2", char r1[] = "1", char r2[] = "2" etc (note that register number can be as high as 31). Could anyone give me an example for a f开发者_运维问答unction that would convert this to binary in the format 000010 00001 00010 00011 0000000000


The easiest way will be using a bit field:

struct code {
    unsigned opcode : 6;
    unisgned operand1 : 5;
    unisgned operand2 : 5;
    unisgned operand2 : 5;
} test_code;

Now you can simply assign to the different members:

test_code.opcode = 0x02;
test_code.operator1 = 0x01;
test_code.operator2 = 0x02;
test_code.operator3 = 0x03;


atoi(op) will give you 2, so you can just string it together

As far as putting it into that structure you want, just create a structure that has bitfields in it and place it in a union with a 32 bit unsigned integer, and you can take the value directly.


Quick pseudo code

const char* int32_to_bin(int32_t value) {
  int pos = 0;
  char output[33];
  while(value > 0) {
    if (value & 1) output[pos++] = '1';
    else           output[pos++] = '0';
    value >>= 1;
  }
  output[pos] = 0;
  return output;
}


What you're asking is a C question but you tag as objective-c so I'll cover both.

C:

These variables such as op[].. are really defined as like char op[..] (not sure if your length), which are C strings of course.

So the operation is 6 bit and each register is 5 bits, that's 15 + 6 = 21 bit word. I'll assume the top 11 bits are zeroes.

What you need are 4 more variables that are integers:

int opint; int r0int; int r1int; int r2int;

You want the integer value of those strings to go in to those integers. You can use atoi() to achieve this, such as opint = atoi(op);

Now that you've got your integers derived from strings, you need to create the 32 bit word. The easiest way to do this is to first create one integer that holds those bits in the right place. You can do it (assuming 32 bit integers) like this:


int word = 0;
word |=    ((opint & 0x3f) << (21 - 6))) |
            (r0int & 0x1f) << (21 - 11)) |
            (r1int & 0x1f) << (21 - 16))
            (r2int & 0x1f)); 

Where the << is shifting in to place. After this, you should have the word integer properly formed. Next, just turn it in to a binary representation (if that's even necessary? Not sure on your application)

Objective-C

The only difference is that I assume those strings start as NSString *op; etc. In this case, get the integers by opint = [op intValue];, then form the word as I describe.


This code will convert a string to binary

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* stringToBinary(char* s) ;
void binchar(char output[],  char character);
void itoa(int value, char* str, int base);

int main(int argc, char const *argv[])
{
    printf("%s\n", stringToBinary("asdf") );
}


char* stringToBinary(char* s) {
    if(s == NULL) return 0; /* no input string */
    size_t len = strlen(s);
    char *binary = malloc(len*8 + 1); // each char is one byte (8 bits) and + 1 at the end for null terminator
    int i =0;

    char output[9];
    for(i=0; i< len; i++){
        binchar(output,  s[i]);
        strcat(binary,output);
    }
    return binary;
}


void binchar(char output[],  char character)
{
    //char output[9];
    itoa(character, output, 2);
}



// since GCC is not fully supporting itoa function here is its implementaion
// itoa implementation is copied from here http://www.strudel.org.uk/itoa/

void itoa(int value, char* str, int base) {

    static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";

    char* wstr=str;

    int sign;




    // Validate base

    if (base<2 || base>35){ *wstr='\0'; return; }



    // Take care of sign

    if ((sign=value) < 0) value = -value;




    // Conversion. Number is reversed.

    do *wstr++ = num[value%base]; while(value/=base);

    if(sign<0) *wstr++='-';

    *wstr='\0';



    // Reverse string
void strreverse(char* begin, char* end);
    strreverse(str,wstr-1);

}

void strreverse(char* begin, char* end) {

    char aux;

    while(end>begin)

        aux=*end, *end--=*begin, *begin++=aux;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜