2

I'm grappling with types between two different libraries.

The gist is that I'm using theArduinoWebsockets 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:

1248163264128

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

I.e. if you look at the binary version of1 from the list, it's actually110001 which is the ASCII code for the character1.

This is because the message coming back from the websocket server is pulled out as aconst 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 asunsigned chars.

I want to:

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

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   -> 0b000000012   -> 0b000000104   -> 0b000001008   -> 0b0000100016  -> 0b0001000032  -> 0b0010000064  -> 0b01000000128 -> 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 usefirecamp andwebsocat 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 the1s and0s.

wireshark

After researching andasking a separate stackoverflow question I figured out a way of sending the actual binary data from the client, specificallyprintf.

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:

ocrdu's user avatar
ocrdu
1,7953 gold badges12 silver badges24 bronze badges
askedDec 22, 2020 at 5:02
Chris Schmitz's user avatar
5
  • ASCII code is represented by one byte, which is 8 bits, not 6 bitsCommentedDec 22, 2020 at 5:39
  • it is unclear why you need to convert the ascii character to a number before displaying itCommentedDec 22, 2020 at 5:42
  • point 3 is 'void'unsigned char is one byte and your bit mask values fit into one byte.CommentedDec 22, 2020 at 7:04
  • (char) strtoul(strData)cplusplus.com/reference/cstdlib/strtoulCommentedDec 22, 2020 at 8:39
  • @jsotola re: ascii byte length: I know it's 8 bits, I typed out the ascii without leading zeros. 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, so 1 or 0b00000001 would light up only the rightmost led on the 8 led row of the matrix. The conversion is so that I can use the binary representation of 1 (0b00000001) vs the binary representation of '1' (00110001)CommentedDec 22, 2020 at 13:20

1 Answer1

3

Not sure if or why you want to convertchar* toint, but if you need to, there areatoi(),atol(),strtol(),strtoul() to consider, or rolling your own function.

Pros and cons are discussedhere. Main thing to note is thatatoi() andatol() have no error handling, but if thechar* 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.

answeredDec 22, 2020 at 13:22
ocrdu's user avatar
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.CommentedDec 22, 2020 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.CommentedDec 22, 2020 at 21:41

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.