# convert string representing a binary number to binary

I have a serial input "1111111" and I would like to convert this to a binary B1111111 that I can send to a led matrix.

I tried to search, but, all the results were misleading, led to full text to binary conversion. Please advise.

edit: This is my sample code where I am missing a step in the conversion part

``````LedControl mat = LedControl(DIN_PIN, CLK_PIN, CS_PIN, MAX_SEG); // MAX7219
void OneLine() {

//input
char text[] = "11111110";
//convert to binary number somehow

//show one line
mat.setRow(0, 0, convertednr);
}
``````
• "I tried to search, but, all the results were misleading", How about start from understanding of ASCII? An ASCII '1' that show on your Serial Monitor is binary 0x31, a binary value of 1 is ASCII '1' - '0' (0x31 - 0x30). Sep 13, 2023 at 14:55
• Have you searched for something like "C++ binary to int"? There is the `strtoi()` function, which also takes a parameter with the number base used (which is 2 for binary). Please try it Sep 13, 2023 at 15:51
• why do you need to convert to binary? ... what will you do with the binary number? Sep 13, 2023 at 17:06
• I receive this as serial input and would like to send it to a 8x8 led display row (max7219) Sep 13, 2023 at 17:34
• why do you need a binary number for that? Sep 13, 2023 at 20:45

As pointed out by @EdgarBonet, I can convert the string to binary number with `strtoul()` and pass this to my led control. Adding this as an answer for future readers:

`````` LedControl mat = LedControl(DIN_PIN, CLK_PIN, CS_PIN, MAX_SEG); // MAX7219
void OneLine() {

//input
char text[] = "11111110";
//convert to number
uint32_t convertednr = strtoul(text, NULL, 2);
//print out
mySerial.println(convertednr);

//show one line
mat.setRow(0, 0, convertednr);
``````
• you can use `mySerial.println(convertednr, BIN);`
– Juraj
Sep 14, 2023 at 13:37
• Re “I can convert the string to decimal number”: Note that `strtoul()` converts the string to a binary (not decimal) number, and that is what you LED control is using (`convertednr` is a binary number). You may be confused by the fact that `mySerial.println(convertednr)` converts this binary number to a decimal string representation, for the purpose of displaying it on the serial port. Sep 14, 2023 at 14:24
• thanks @EdgarBonet, corrected the text Sep 15, 2023 at 14:41