0

I know that this question seems familiar with this Conversion of Char to Binary in C, but is not exactly the same. I am converting an array of characters to binary integers. As a second step I am trying to concatenate them in an array of integers. I am converting the integers back to characters so I can concatenate them. The script seems to be working fine, but for some reason that I can not understand when I print the whole string it produces a not printable character at the beginning of the string.

Sample of code:

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

int main(void) {

  char *temp;
  char str[2];
  char final[32];

  for (temp = "LOCL"; *temp; ++temp) {
    int bit_index;
    for (bit_index = sizeof(*temp)*8-1; bit_index >= 0; --bit_index) {
      int bit = *temp >> bit_index & 1;
      printf("%d ", bit);

      snprintf(str, 2, "%d", bit);
      printf("This is test: %s\n",str);
      strncat(final , str , sizeof(final) );
    }
    printf("\n");
  }
  printf("This is the array int: %s\n",final);

  return 0;
}

Can someone help me understand where I am going wrong?

Thanks in advance for the time and effort to assist me.

5
  • 1
    Do you know the relative precedence of >> and & in *temp >> bit_index & 1? Does the compiler know? Are you both in agreement? I strongly recommend the use of parentheses, for the benefit of those who come after even if you know the answer straight off. Separately, maybe you should show the output you get and the output you want. It may make it easier for people to help you. Commented Oct 13, 2014 at 21:39
  • Note that you are telling strncat() that it is OK to write up to sizeof(final) characters after the end of whatever string is already stored in final. The interface to strncat() is even worse than the interface to strncpy(); it is very difficult to get the code correct, and you have to know how long the string in the target is before you can say how much space there is left, which really makes it pointless (you could use memmove() or memcpy() or strcpy() or perhaps strncpy() instead). I'd forget that strncpy() exists -- I only work with it on Stack Overflow when people ask. Commented Oct 13, 2014 at 21:45
  • This line: char final[32]; should be char final[32] = {'\0'}; so the string is always terminated with a null. Commented Oct 15, 2014 at 7:11
  • this line: char str[2]; should be char str[3]; so there is room for the terminating null. Each iteration should re-initialize the str array to all '\0' Commented Oct 15, 2014 at 7:12
  • This line: strncat(final , str , sizeof(final) ); will almost always fail because it will keep putting characters from str AND final until a null is found or 32 characters are added. suggest changing to strncat(final , str , 2 ); Commented Oct 15, 2014 at 7:15

1 Answer 1

5

You just forgot to initialise final, so you're concatenating your binary string onto whatever garbage happens to be in final when you run the code. You also need to allow for one extra char in final (to hold the '\0' terminator). Change:

char final[32];

to:

char final[33] = "";
Sign up to request clarification or add additional context in comments.

5 Comments

final[32]; may be too small by 1.
@PaulR - after creating char final[33]; would it work to do final[0]=0; on the next line?, (or just use memset() )
@ryyker: you can do it that way if you like, although it's perhaps stylistically a little better to write final[0] = '\0';. memset seems like overkill though.
@PaulR OMG...you are absolutely right. Worked perfectly it is the only thing that I did not tested. I was under the impression that I was modifying somehow the binary data. So simple and I have tried 5 different solutions and non of them worked. Thanks a million. The reason that I have not added the \0 character at the end is that I am planning to concatenate this string with another after so I need it to be exactly 32 bits. Thank you again for your time and effort to assist me.
@Thanos: glad it's working for you - it's very common when debugging to find that you are chasing the "wrong" problem, usually due to a bad assumption. This happens to me too, and I've been doing this stuff for over 30 years (you'd think I'd have learned by now!).

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.