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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

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.
int atob(char *binary)
Very sorry for my careless misinterpretation!! <verbatim> /* This gives the general idea, bit but not ruggedized with error checking, (illegal characters and overflow). */ #include <iostream> #include <stdint.h> 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; } </verbatim>
Very sorry for my careless misinterpretation!! <verbatim> /* This gives the general idea, bit but not ruggedized with error checking, (illegal characters and overflow). */ #include <iostream> #include <stdint.h> 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; }
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.
|

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.