2

I'm Trying to grapple with types between two different libraries.

The gist is that I'm using the ArduinoWebsockets library to receive information coming from my api that I then want to render out to an LED matrix. An example message would look like this:

1
2
4
8
16
32
64
128

It's important to point out that the data here is a list of string version of numbers, not integers.

I.e. if you look at the binary version of 1 from the list it's actually 110001 which is the ascii code for the character 1.

This is because the message coming back from the websocket server is pulled out as a const char *.

The LED matrix function I'm calling has a signature of:

void Write_Max7219(unsigned char address, unsigned char dat)

So I need to send in the values as unsigned chars.

I want to:

  • parse this information line by line
  • convert the ascii character for the number into it's integer equivalent
  • pass the value to a different function to write the data to an LED matrix

And I'm not sure how exactly to do this. I've been grappling with the type const char pointer vs regular char pointer and how to properly parse the return delimited string.

Any help would be greatly appreciated!!

Clarification

Based on some of the comments I don't think I explained the reason I'm trying to do this conversion very well. Here's (hopefully) a better explanation:

The reason I want to convert it is because I'm not displaying the number character as a string, I want to use the binary representation to control the lights in a row of the matrix.

The number 1 or 0b00000001 would light up only the rightmost led on the 8 led row of the matrix.

The idea is that I could send over rows of numbers where each row corresponds to the row on the LED matrix.

e.g.

1   -> 0b00000001
2   -> 0b00000010
4   -> 0b00000100
8   -> 0b00001000
16  -> 0b00010000
32  -> 0b00100000
64  -> 0b01000000
128 -> 0b10000000

But the messages I get from the websocket server are coming through as the string representations of the numbers (ascii or utf) vs the numbers themselves, so I can't just pass the numbers into the function for the led matrix because the underlying binary is different.

The conversion is so that I can use the binary representation of 1 (0b00000001) vs the binary representation of '1' ( 00110001).

UPDATE AND RESOLUTION

So I've been on a big troubleshooting journey. When @ocrdu said:

BTW I would try to get the websocket to send the correct single bytes instead of string representations of numbers, if at all possible.

I thought "yeah, that's the real way of fixing it and understanding what's going on", so I started tracing the issue back. Without going into all of the detail it turns out that the problem I was having was actually starting at the test client.

I use firecamp and websocat as websocket client testing tools. I was trying to send the binary data via both tools, but when I dug in via Wireshark I found that the data in flight coming from the client itself was made up of the hex values for the encoded characters of the 1s and 0s.

wireshark

After researching and asking a separate stackoverflow question I figured out a way of sending the actual binary data from the client, specifically printf.

e.g.:

 printf "\x01\x02\x03\x04\x05\x06\x07\x08" | websocat -b ws://localhost:3002

printf solution

Now that this works I can start going back through the rest of my arduino code and have it parse the binary data directly instead of converting the encoded values to their binary counterpoints.

All of that said, I do appreciate all of the help. I may not have needed to do the conversions for this particular instance, but I want to get better with my C types, conversions, and general understanding so the knowledge gained here is still beneficial. Thanks everyone :bows:

6
  • ASCII code is represented by one byte, which is 8 bits, not 6 bits – jsotola Dec 22 '20 at 5:39
  • it is unclear why you need to convert the ascii character to a number before displaying it – jsotola Dec 22 '20 at 5:42
  • point 3 is 'void' unsigned char is one byte and your bit mask values fit into one byte. – Juraj Dec 22 '20 at 7:04
  • (char) strtoul(strData) cplusplus.com/reference/cstdlib/strtoul – dandavis Dec 22 '20 at 8:39
  • Awesome, thanks @dandavis I'll give that a try. – Chris Schmitz Dec 22 '20 at 13:17
3

Not sure if or why you want to convert char* to int, but if you need to, there are atoi(), atol(), strtol(), strtoul() to consider, or rolling your own function.

Pros and cons are discussed here. Main thing to note is that atoi() and atol() have no error handling, but if the char* you feed them is completely under control and predictable, that need not be a problem.

BTW I would try to get the websocket to send the correct single bytes instead of string representations of numbers, if at all possible.

2
  • a nice thing about atoi() is that it stops at non-digits, so your input can be "dirty" as long as starting with numbers, which makes it nice for using with existing string pointers instead of a small chopped-out char arrays. – dandavis Dec 22 '20 at 18:12
  • I think this may be the solution. I have more detail but I have to wait till after work to update the question. – Chris Schmitz Dec 22 '20 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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