0

Brand new to the board. I really try to solve my programming conundrums without bogging down a public forum, and stackoverflow has helped with many a problem, but I'm at a loss. I hope you can help.

I am attempting to concatenate/combine, single digit characters in an array into a variable that takes the form of a binary string of format 10101010. There is no arithmetic involved and all characters are single-digit 1s and 0s. I have scoured the Web for a solution and I have tried everything from strcat(), otoi(), sprintf(), etc. I have even tried using a String array[] to construct my variable and then convert type using strtoul(). The code below reflects my latest attempt, which compiles successfully, but appears to produce blank characters. Understanding that I may be way off track with this solution, I am open to any solution that works.

The code:

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>

char digit[48] = "";

char buf[9];
snprintf(buf, sizeof buf, "%s%c", digit[16], digit[17], digit[18], digit[19], digit[20],_ 
digit[21], digit[22], digit[23]);

As you can see, the idea is to be able to take any digits from char digit[48] and combine them into the desired format. Truthfully, I'm not interested in adding the characters into a new array, but I got to the point of trying anything. My preference is to have the result accessible via a single variable to then use for bitwise operations and direct input into integrated components. Than you for any assistance you can provide.

K D
  • 1
  • 2

1 Answers1

1

Take a look at Converting an int into a base 2 cstring/string

and note (if you're using Linux) that I believe the deprecated function name there is _itoa.

Community
  • 1
  • 1
koan911
  • 360
  • 1
  • 3
  • 13
  • I reviewed the suggested link. Thank you. However, what I am trying to do is not exactly converting an integer to a binary string. The number 1 is represented as 00000001 in binary. In my implementation that is of little use to me. I quite literally want to combine the 1s and 0s in a char array into a char or int variable that takes the form of a binary string. For example: int num = 10101010 using the characters in the array. I apologize if I was not clear on that. – K D Apr 11 '15 at 04:18
  • int atob(char *binary) – koan911 Apr 11 '15 at 10:08
  • Very sorry for my careless misinterpretation!! /* This gives the general idea, bit but not ruggedized with error checking, (illegal characters and overflow). */ #include #include using namespace std; uint64_t decode_binary(char *binary) { uint64_t num = 0; for (char *cp = binary; *cp; cp++) { num <<= 1; if (*cp == '1') num |= 1; } return num; } int main (int argc, char *argv[]) { uint64_t num = decode_binary("110011"); cout << "num: " << num << endl; } – koan911 Apr 11 '15 at 10:24
  • Very sorry for my careless misinterpretation!! /* This gives the general idea, bit but not ruggedized with error checking, (illegal characters and overflow). */ #include #include using namespace std; uint64_t decode_binary(char *binary) { uint64_t num = 0; for (char *cp = binary; *cp; cp++) { num <<= 1; if (*cp == '1') num |= 1; } return num; } int main (int argc, char *argv[]) { uint64_t num = decode_binary("110011"); cout << "num: " << num << endl; } – koan911 Apr 11 '15 at 10:32
  • koan, is this solution for C++? I checked out iostream and the information I'm seeing indicates this is specific to C++. I am using C and while I have read that they are similar, that similarity is not intuitive to me. – K D Apr 12 '15 at 01:36
  • Yes, C++, but only for the cout for printing. Replace iostream with stdio.h and the cout with an appropriate printf and the code will compile as C. (Apologies for my awful formatting. Haven't figured it out yet!) – koan911 Apr 13 '15 at 00:08
  • Again, I may have your intent wrong. Not a binary result but a char * result with the text of a binary representation? char buf[9]; int bx; for (bx = 0; bx < sizeof(buf) - 1; bx++) { buf[bx] = digit[16 + bx]; } buf [bx] = '\0'; – koan911 Apr 13 '15 at 00:15
  • The idea was to add 1s and 0s to an array and to be able to use them to construct a number that resembles binary, but isn't really binary. I've done some reading on pointers and if I'm reading the information correctly, an array name is a pointer and a pointer cannot have a value of 0. If that's true, I'm guessing I cannot call an array index that has value 0 and use it in code, making what I'm asking impossible. I don't know. Like I said, I'm clueless on this. If you can shed some light on the pointer question that would be great. If not, I'll close this question and look at alternatives. – K D Apr 13 '15 at 03:45
  • Very good. Let's define some terms: a binary integer is an int with bits (binary digits), a binary string is a text string of characters that are either (the readable) '0' or '1', (8 or 16 bits each). Referring to your example, "int num = 10101010" suggests you want to produce a binary integer, not a binary string? Also of interest is whether you want an indefinitely long binary number or whether it can be limited, e.g., to 32 or 64 bits? Thanks for your patience; answers to the above will dispel the confusion, I think! – koan911 Apr 13 '15 at 04:38
  • Take a look also at: "strncpy(buf, &digit[16], sizeof(buf) - 1);". This will copy 8 consecutive characters from the digit array, starting at index 16, into buf. Take note, though, that it will not put a terminating null character, '\0', into the last byte of buf. You can do that with, "buf[8] = '\0';" &digit[16] can also be written as digit + 16 because, as you alluded, the name of an array is effectively a constant memory address and can be used as a pointer value. (I can write much more on this if you want more clarification.) – koan911 Apr 13 '15 at 10:45
  • That's right, a binary integer, or perhaps a uint8_t number. The number is limited to 8 bits/digits. Your second comment has lost me. I have attempted a couple of different functions that took the `strncpy(buf, &digit[16], sizeof(buf) - 1)` format, but I have had no success and the null character has definitely been throwing me. I think the problem is that when I try integer, I am getting the decimal equivalent and when I use char, I don't know how to handle the conversion to int or the null character. In both cases, when I attempt to concatenate digits, I'm performing arithmetic instead. – K D Apr 17 '15 at 01:51
  • OK, then the code I gave above containing: "num <<= 1; if (*cp == '1') num |= 1;" is the way to go. Take the cout out (replace it with printf) and it should compile under C... (Good luck!) "num <<= 1" shifts the bits up 1 position, vacating the least-significant bit. Then "num |= 1" ORs a 1 into that least-significant bit position. – koan911 Apr 18 '15 at 10:45