-1

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);
}
12
  • 2
    "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).
    – hcheung
    Sep 13, 2023 at 14:55
  • 1
    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
    – chrisl
    Sep 13, 2023 at 15:51
  • 1
    why do you need to convert to binary? ... what will you do with the binary number?
    – jsotola
    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)
    – vilmarci
    Sep 13, 2023 at 17:34
  • 1
    why do you need a binary number for that?
    – jsotola
    Sep 13, 2023 at 20:45

1 Answer 1

1

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);
3
  • 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
    – vilmarci
    Sep 15, 2023 at 14:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.