0

I have an array of char containing binary string, let's call it binString. I also have another array of character that has a few characters, let's call it asciiString. I want to convert each asciiString character to binary string.

For instance, binString will have: "00001101". And asciiString will be: "ba". Now I want to convert b to binary which is "01100010" and append to binString. Also convert char 'a' to bin and do the same. If there is a character like "2", first need to convert to int, then convert to bin, so "2" should result in "00000010".

Here is my code so far:

 char *asciiToBin(int value, int bitsCount)
 {
         char *output[16];
         int i;
         output[bitsCount - 1] = '\0';
         for (i = bitsCount - 1; i >= 0; --i, value >>= 1)
         {
            output[i] = (value & 1) + '0';
         }
         return output;
 }

Now, from another function:

position = 8; // this is filled up to this character
char *binString[32];
len = 16;
bin16 = asciiToBin(atoi(operand), len);
for (int x = 0; x < len; x++)
{
    char c = bin16[x];
    binString[position + x + 1] = c;
}

Can someone help please?

Thanks,

4
  • shall output really be an array of 16 char*? Commented Apr 14, 2014 at 0:22
  • What problems are you having? What do you need help with? Commented Apr 14, 2014 at 0:46
  • the code above doesn't work. I am not sure what is the issue. Also I think I should not use array pointers? alternatives? Commented Apr 14, 2014 at 0:58
  • Note: Avoid magic numbers like 32 or 16. Suggest len = sizeof(int)*CHAR_BIT + 1; Commented Apr 14, 2014 at 4:31

2 Answers 2

0
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

char *asciiToBin(unsigned char value){
    static char output[CHAR_BIT+1];
    int i;
    for(i = CHAR_BIT -1; i>= 0;--i, value >>= 1){
        output[i] = "01"[value & 1];
    }
    return output;
}

char *convBinAndCat(char *d, const char *s){
    char *out = strchr(d, '\0');
    for(;*s;++s){
    //  unsigned char value = isdigit(*s) ? *s - '0' : *s;
        unsigned char value = isdigit(*s) ? strcspn("0123456789", (char[]){ *s, '\0'}) : *s;
        strcpy(out, asciiToBin(value));
        out += CHAR_BIT;
    }
    return d;
}

int main(){
    char binString[CHAR_BIT*4 + 1] = "00001101";
    printf("%s\n", convBinAndCat(binString, "ba2"));//00001101011000100110000100000010
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

My attempt to clean-up:

int asciiToBin(int value, int bitsCount, char *output)
  {
  int i = bitsCount - 1;

  output[i] = '\0';
  for(; i >= 0; --i, value >>= 1)
     output[i] = (value & 1) ? '1' : '0';

  return(0);
  }

My attempt to boil-down the rest... but I have to admit failure. I guess it would be helpful to know more about the goal...

int   position = 8; // this is filled up to this character
char *binString[32];
int   len = 16;
char  bin16[len+1];

asciiToBin(atoi(operand), len, bin16);

for(int x = 0; x < len; x++)
    binString[position + x + 1] = bin16[x];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.