Тёмный

Text String Tutorial in C Code! Tutorial using Arduino, copy/move/parsing/etc... 

Kevin Darrah
Подписаться 106 тыс.
Просмотров 36 тыс.
50% 1

Just my 2 cents in how to handle and manipulate text strings (Character Arrays) in C code... all shown using Arduino, but is universal on embedded systems coded in C. Very useful stuff in here that can be used in many projects!
It's a doozy, so here are the timestamps:
2:13 Intro, Size, Length, and Null
8:10 Concatenate
10:33 Copy and Move
16:09 Comparing two strings
17:09 Find string in another string
21:06 Parsing a string, like data separated by commas
24:50 Convert string to integers, long, float and back to string
28:00 Pass and return strings with functions
29:25 Parse data from a serial port - real example
THE CODE:
www.kevindarrah.com/download/...
Check out my Tindie store (trigBoard is available) www.tindie.com/stores/kdcircu...
Thanks to all the Patrons for dropping a few bucks in the tip jar to help make these videos happen!
/ kdarrah
Twitter: / kdcircuits
For inquiries or design services:
www.kdcircuits.com

Наука

Опубликовано:

 

17 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 110   
@thepike100
@thepike100 3 года назад
Now that folks is how to do a tutorial. No waffle, just the facts. One of the best I've seen - on any subject. Thanks a lot.
@Kevindarrah
@Kevindarrah 3 года назад
thanks - yea I make these vids for the future version of me that forgot how to do this
@MadScienceWorkshoppe
@MadScienceWorkshoppe 4 года назад
Great tutorial. This is one of the best Arduino tutorials I've seen. Very clear and thorough. I was having some weird bugs with the display for a testing device I've been working on. It turns out I wasn't accounting for the need for the null character! It was very fortunate that for me that you posted this and I watched it earlier this week!
@davidburbach5072
@davidburbach5072 3 года назад
Very well done! After looking through more than a dozen tutorials it was awesome to find yours. This gives me the tools to build what I need for serial data pass.
@eccentricOrange
@eccentricOrange 4 года назад
I had been looking for a concise (yet detailed enough for practical value) courses on string manipulation. This video provides almost exactly the topics I needed (basic theory, copying, parsing conversion and passing through functions). Thanks a ton!
@jon_raymond
@jon_raymond 4 года назад
I've watched this video several times since your released it. I've got it bookmarked as everytime I pick up something new. It has been really helpful. Thank you!
@hopelessnerd6677
@hopelessnerd6677 4 года назад
The best string tutorial I've seen so far, and I've seen gobs. Thanks.
@DevinCarpenter23
@DevinCarpenter23 2 года назад
Super helpful! Your examples are great, and everything is very sectioned out and builds up your understanding piece by piece so it's not overwhelming. Thank you!
@EricDalgetty
@EricDalgetty 4 года назад
This is super helpful, and just when I needed it. Thanks!
@randomname3894
@randomname3894 4 года назад
Really informative and in a good teaching speed! Hope to see more! There are a lot of pitfalls when people want to step up from simple blink sketches to working with different datatypes and more „special“ functions. If you’ve never heard about it, you can’t use it.
@AbcDef-hl2ic
@AbcDef-hl2ic 4 года назад
THANK YOU SO MUCH 20 years of hobby programming and having so much hard time with characters, especially those last years with Mqtt messages. And now every thing is clear to me! THANK YOU AGAIN!
@HowardAlison
@HowardAlison 3 года назад
I've been playing with Arduino for some time now. New project that uses strings and am all at sea. This video was super helpful. Well worth the investment in time. Thanks Kevin.
@jdkato1778
@jdkato1778 4 года назад
Loved the video. Very helpful to know these functions, which when I tried Googling some of them to see what was also out there on them, I could not immediately find them online. ..You explain them well one line at a time and one lesson at a time, then putting it all together at the end. I think now I know them well enough I will be able to apply them to my own coding.
@WayneRiesterer
@WayneRiesterer 28 дней назад
Brilliant presentation. Strings get me every time I come back to coding in C/C++. Your tutorial is really helpful. Thanks!
@misterobotique
@misterobotique 4 года назад
Your video [Cheap and Easy WiFi (IoT) Tutorial Part 2 - ESP8266 Arduino Code ] was extremely useful when I was tring to make a WIFI connected IR remote for my air-conditioning. RU-vid recommended me this video and thanks to that suggestion I found back your channel. Your way of explaining and demonstrating stuff is in perfect sync with the way I learn. You earn a new follower and please more of this kind of tutorial.
@chetankatageri5845
@chetankatageri5845 2 года назад
I had been looking for it.... and here it is.... Well explained and many thanks for the code.
@mikegenco9646
@mikegenco9646 Год назад
A very high quality tutorial. He shows commands I have never found in the Arduino reference.
@johneagle4384
@johneagle4384 2 года назад
Succinct and yet comprehensive tutorial. Thank you. It answered a lot of questions I had.
@cursoderobotica
@cursoderobotica 4 года назад
very nice tutorial!!! thank you! I have used something similar with Python (of course we can not compare c++ with python) but this is the basics to understand how it works on high level languages. thank you
@mataloger
@mataloger Год назад
Thanks for making such a well paced, well put together video, with very helpful explanations.
@mmdnaderi7183
@mmdnaderi7183 4 года назад
great and useful video again, thanks I didn't know "Strings" are such big deal
@hollensted
@hollensted 4 года назад
Awesome in depth tutorial. Thanks!
@avrphreak9121
@avrphreak9121 3 года назад
Thank you! Very useful stuff to use on my MQTT project!
@CodyBanks10
@CodyBanks10 Год назад
This is an excellent video, it covered most of what I was looking for.
@MrGlocksrfun
@MrGlocksrfun 2 года назад
I appreciate this video's high quality. Subscribed and bell'd up.
@johnacsyen
@johnacsyen 4 года назад
I also struggle with string manipulation on arduino. Thanks for this video.
@zyghom
@zyghom 2 года назад
this is AMAZING basics for the newcomers - seriously! The biggest issue in the online tutorials for Arduino is: almost everybody jumps directly to String class and people start using it without knowing the background of how strings and characters work in C/C++. Then programs crash etc. Thank you again!
@humbertoleal7683
@humbertoleal7683 3 года назад
This is an excellent video, thank you very much for sharing your knowledge, I was able to solve a problem I had. Your video explains in a very deep and clear way a topic that few videos do, on the subject. Cordial greetings from Bogota / Colombia.
@gabrielgraf2521
@gabrielgraf2521 4 года назад
Very nice and Clear Video. Lesson 9 helps me alot
@sriramuelango2827
@sriramuelango2827 3 года назад
useful Video, after a long surf at internet. I appreciate your work man.
@jcadams6
@jcadams6 Год назад
THANK YOU thank you THANK YOU So much for this. Beautifully given lesson. Easy to understand and follow. got me up and running!
@SuburbanDon
@SuburbanDon 9 месяцев назад
Great job. I wish i had found this 6 months ago. Thanks for making the code available.
@BryanByTheSea
@BryanByTheSea 4 года назад
Wow, excellent tutorial video. Very informative
@ateebhassan4655
@ateebhassan4655 2 года назад
Great tutorial man. That's what I was exactly looking for. Thanks for sharing.
@savetrott4193
@savetrott4193 3 года назад
GREAT Tutorial, easy to understand and helps a lot. Thank You 👍🏻
@kisssys5414
@kisssys5414 4 года назад
I would love to see more of this kind of video, especially if it could be searched easily for each lesson. If you showed a index of lesson's and had a small number on each video frame to show what lesson your on it would be easy to fast forward to a lesson topic. Very well done.
@jfzaremba1
@jfzaremba1 5 месяцев назад
Thank you, Informative, great for a hobbyist like me, Lesson plans r awesome, Bless u 4 putting this info on RU-vid!
@Stawustawu
@Stawustawu Год назад
Thoroughly explained topic. Thanks for the video.😀
@WistrelChianti
@WistrelChianti 4 года назад
Thanks! Really useful exploration and a great way to do it. It's bad I know, but I've always been mildly suspect of string functions and classes (not covered here) because I didn't really know what they were doing (whereas I do if I stick to my own code) but this is really useful to know what is out there and that the functions are, for the most part, just nice little helpers so long as you know what they are doing and use them carefully (those checks FTW). You also solved one of my problems. I'd hit the "ah, compiler directive" point before, took one look at about half a page of them all mashed together and thought "life is too short" - just multiplied the values into ints instead (which worked fine - but was a bit of a hack)
@hooh125
@hooh125 2 года назад
Thank you so much!! this content is so valuable, keep it up!
@wahidsalah4545
@wahidsalah4545 4 года назад
Aaaaww. Watched it when released , but today the video was very very illustrative and helpful. I copied many lines in my project I’d you don’t mind. Thanks
@user-qd6cc3km3l
@user-qd6cc3km3l 8 месяцев назад
Excellent tutorial, learnt loads. Thanks
@johnborton4522
@johnborton4522 2 года назад
What a GREAT string tutorial. Thanks,
@salvatorebarbaro5862
@salvatorebarbaro5862 2 года назад
Great JOB !! complete and clear, a Wiki to save and always keep in mind. THANK YOU
@101appsCoZa
@101appsCoZa 4 года назад
Great video. Thanks! More would be great.
@jbbotha
@jbbotha 4 года назад
This is a very good video. Thanks.
@Beatfreak19831
@Beatfreak19831 4 года назад
Very useful! Thanks!! :)
@dbryars
@dbryars 4 года назад
Great job... as usual.
@williammiller7543
@williammiller7543 6 месяцев назад
Hi Kevin, I've watched you and subscribed years ago, even bought a couple trig boards, but then lost track of you. I've recently tried to work with character data coming back from auto authorize app and had been looking for a tutorial on explaining strings. Yours in the first that ties all the options. Otherwise I've had to look at many examples to try to get all the options available to read back and decipher the data sent back. Thanks and I'll start fallowing you again.
@davidverheyen6635
@davidverheyen6635 2 года назад
Very usefull video, that I'm gonna need to see a few more times... Thanks..
@CommonManDreams27
@CommonManDreams27 3 года назад
Thank you sir awesome tutorial.. Got what I want
@SentinelThe
@SentinelThe 3 года назад
At last! Now I have something I can use. First rate video for a non beginner
@jonathanmcdonald1101
@jonathanmcdonald1101 3 года назад
Great video, super helpful.
@geoffpanchaud967
@geoffpanchaud967 2 года назад
Great tutorial, thanks.
@andreaswolfesberger6140
@andreaswolfesberger6140 4 года назад
that´s what i need - thx a lot
@Gocast2
@Gocast2 3 года назад
Extremely helpful! All I'll ever need for transfering data over UART
@Kevindarrah
@Kevindarrah 3 года назад
thanks - yea one of my favorite videos I reference back to myself for UART comms
@Gocast2
@Gocast2 3 года назад
@@Kevindarrah Serial Input Basics is also very helpful forum.arduino.cc/t/serial-input-basics-updated/382007/3#example-5-receiving-and-parsing-several-pieces-of-data-3
@luisalejandrotoledoruiz9800
@luisalejandrotoledoruiz9800 2 месяца назад
EXELENTE . ESTOY SORPRENDIDO POR COMO COMENTAS TAN RAPIDO Y PRESISO.
@peterfielden-weston7560
@peterfielden-weston7560 4 года назад
I liked this type of instructional video and would like to see more of them. I see you have started a playlist with this video, would this video alway be number 1 in the list or could it's position be altered as required in the future. Thanks PCLFW
@nathiral-anasweh2609
@nathiral-anasweh2609 11 месяцев назад
thank you , it was great help very useful
@jontscott
@jontscott 4 года назад
atoi() ... what a nice function (if you know about it, thank you for introducing me to it!) A month or two ago I was trying to read an int over serial and I ended up writing my own function to parse a string into an integer. My version of the function lacked many safety checks I imagine... Also nice job explaining how to print a float, while I generally avoid floats, it is nice to know how to print them should I need to. Maybe the next part of this should be on adding a CRC or similar check to ensure the data all arrived without hidden errors?
@AbcDef-hl2ic
@AbcDef-hl2ic 4 года назад
I will send a message to the arduino website to ask them to put a link to your video on their wiki.
@MPElectronique
@MPElectronique 4 года назад
Super good... Thanks :)
@hoekbrwr
@hoekbrwr 3 года назад
This is my greatest problem when working with Arduino and alike! This is a much needed piece of information. Till now I was totally confused by *,& and NULL ending chars. I was always angry why don't they use String which is much easier to shuffle around. There are so many instances you cannot use String, like your serial input example where you only get bytes in. I will start playing around and rewind the video a few times. Only thing I did not grab dierectly is searching and using NULL as a next item!
@beetleboy135
@beetleboy135 4 года назад
wow!! In depth explanation of the functions... can be transferred to read from SD card instead of serial monitor by just replacing the Serial.readBytesUntil with file.readBytesUntil if the file is a SD located file?
@Anointingify
@Anointingify 19 дней назад
Great work on char and string manipulations. Could you talk about indexOf , trim and using substring? Many thanks.
@mudsteel
@mudsteel 3 года назад
Thank for briefing so nicest way what strings are all about
@kanishkaranasinghe4405
@kanishkaranasinghe4405 4 года назад
Bless you!!
@williammiller7543
@williammiller7543 6 месяцев назад
Thanks!
@MPElectronique
@MPElectronique 3 года назад
Hey Kevin, does sprintf put a NULL character at the end of the char array ? thanks :) Marc.
@tnisius1
@tnisius1 4 года назад
Thanks for this video tutorial. I’ve struggled with text manipulation and this has explained problems I’ve had. I’ve watched your channel for a number of years and am always impressed with the depth and clarity of your explanations. Have you considered doing a lesson on using Tabs with the Arduino IDE? I’ve tried using them but have ended up with “not declared” errors. I thought it would make a long sketch easier to debug if I kept the main loop short and called functions for most of the code, putting each function in a separate tap. I think my problems come when I try to call one function from another function (e.g., a function to set a clock uses a function to update the display). Should I use local or global variables? Do I create .ino, .cpp, or .h files? I’ve looked at some other videos, but I didn’t get an understanding of why they did what they did or how to decide which approach to take for my designs. Thanks again for your videos. I'd very much like to see a video by you explaining Tabs.
@WistrelChianti
@WistrelChianti 4 года назад
Good question. Say you want one extra "place" (aside from your .ino file) with code in, say called: "myExtraCode". For this you need 2 files: myExtraCode.h and myExtraCode.cpp. You put the full functions in myExtraCode.cpp, and, for each function you put there, you write just its definition (the top line of the function, but with a ; on the end) in myExtraCode.h. The code in your .ino file needs to know where these other functions are though, so to tell it, you'd add #include "myExtraCode.h" near the top of the .ino file. A short video on this might not be a bad idea though, especially if it went into the #ifdef stuff.
@naturheilkunde-kanal9814
@naturheilkunde-kanal9814 4 года назад
Same here. As coming from the Python world I still struggle with the way things work in C/C++, especially strings. Don't I need to include any libraries like ios, iostream etc.?
@ElectrogicsPH
@ElectrogicsPH 3 года назад
Hi thank you for the great tutorial. I have a question in your last lesson. What if the rawData is dynamic ? How to make the parsedStrings[4][20] dynamic that can hold any number of comma separated values.? Thanks
@ElectrogicsPH
@ElectrogicsPH 3 года назад
Can you check if the code below is the best solution to my question above? ``` int count = 0; char *ptr = rawdata; while((&rawData[dataPosition] = strchr(ptr, ',')) != NULL) { count++; ptr++; } ```
@srh2301
@srh2301 Год назад
14:41 In line 36 you set the maximum length as sizeof(stringOne), but you start your copy target at &stringOne[3] . Don't you have to subtract the "3" from the sizeof(stringOne) value, otherwise the strncpy could overrun the target by 3 characters?
@gabrielgraf2521
@gabrielgraf2521 4 года назад
Is it posseble to change the Function Serial.readbytesUntil() with the normal Serial.read(), because I try to connect My Arduino with Processing and will read Data from it and Processing works with Serial.read??
@pareshmhatre4019
@pareshmhatre4019 2 года назад
by the way this tutorial is very good
@rickseiden1
@rickseiden1 4 года назад
In lesson 7 you were parsing strings for data. You used a fixed length. What do you do when you have to handle an unknown length? Like say you're getting input from a web page, and the input is a textarea where the user can put in whatever they want, and you have to parse it out.
@EmpyreanLightASMR
@EmpyreanLightASMR 11 месяцев назад
I don't understand why a null character is added automatically at 3:20 char charString[] = "123456789"; but in all the other surrounding examples, the null character isn't added, so you have to manually do it. What am I missing?
@jhyland87
@jhyland87 8 месяцев назад
I wish I would have found this early on. The idea that strings had to be stored as individual characters in an array seemed so foreign to me when I started learning C++ (coming from JS, PHP, Perl, Python - Interpreted languages). I started using the String lib with Arduino just because it made things easier, but it quickly turned to crap due to how much memory it uses up. I now stay as far away from String as I can, lol.
@tubical71
@tubical71 4 года назад
great stuff....but sadly too late...i already needed to learn it all myself and surely i had a hard time figuring that out. Main reason was that i wanted to code a function which put out a given text, and/or numbers - letter by letter with a slight delay on a 40x2 chars VFD display
@mr.techinventor3463
@mr.techinventor3463 3 года назад
I am constructing long string with int please help
@GhubaidaHassani
@GhubaidaHassani 2 года назад
Hi brother . i need some help how could I contact you?
@muhammadbilal2000
@muhammadbilal2000 3 года назад
At 6:38 why is the size 100? if the length has increased to 109 then why didn't the size? And ok, if the size is fixed then how come it can go beyond its defined limit of 100 to hit a null character? Didn't get that.
@pareshmhatre4019
@pareshmhatre4019 2 года назад
when i am sending "i love you" over serial monitor its ok with code line const char msg = " i love you" but when i am sending the same stuff with the help of String msg = " i love you its giving random stuff, why?
@ismzaxxon
@ismzaxxon 3 года назад
Any lectures on esp8266 12e and esp32 type Arduino code, would be great. For example, how to deal with multiple TCP clients and one client does or takes too long, which then delays or crashes other clients tx tx
@benhetland576
@benhetland576 3 года назад
It seems you generally confuse the NULL character string pointer with the ASCII Nul character string terminator throughout this video. The latter may be written as just the number 0 or the character literal '\0'. Now, this mostly works because NULL often happens to defined as 0 as well, but I believe at least in C (not in C++) it may be defined as (char*)0 or (void*)0 as well. I guess the idea of NULL was to convey the concept of the null pointer, not a specific character value, so this simply breaks that subtle distinction. In other words NULL is a _pointer_ while '\0' is a _char_ type. Around the 33:40 mark this goes wrong when, after you print "token to big" (sic), a null pointer is passed as the source string to strcpy, which actually invokes undefined behavior. You should have passed "" (empty string) or just written the whole statement as "parsedStrings[0][0] = 0;".
@Kevindarrah
@Kevindarrah 3 года назад
good info!
@TheEmbeddedHobbyist
@TheEmbeddedHobbyist 4 года назад
Great vid, strings are always a pain in C, BUT why does the arduino have "atof" but stringf does not have "%f"? They give with one hand and take away with the other 8-)
@WistrelChianti
@WistrelChianti 4 года назад
I was looking into this a while ago in relation to a related function with the same problem (lack of float support). I think I read something that implied it was a space saving decision, but check that (although it seems a plausible/sensible explanation - especially to anyone who as ever run out!)
@TheEmbeddedHobbyist
@TheEmbeddedHobbyist 4 года назад
Hi@@WistrelChianti​, without hardware floating point it is better to do it in fixed point math. We had to do this with a bit of navigation equipment on an aircraft. we could not trust the C compiler to get it right every time as you have no control how it complies and builds the code. if you write your own fixed point routines it is easier to test and document.
@WistrelChianti
@WistrelChianti 4 года назад
Interesting!
@eduardsrudzitis1043
@eduardsrudzitis1043 3 года назад
Note, its better to use ‘\0’ instead of NULL
@robertmacharia3141
@robertmacharia3141 3 года назад
would leave a million likes if i could... very helpful
@tb303wpf1
@tb303wpf1 4 года назад
I've been using assembly for arduino. Would it be worth my time to learn C++?
@superdau
@superdau 4 года назад
If you are using assembly you aren't using Arduino. And yes, learning C is worth the time. C is useful for many microcontroller families and on modern ones (modern meaning the last one or two decades) there's no point for using assembly at all (if for some reason you need assembly for a critically timed sequence you can inline it in C).
@WistrelChianti
@WistrelChianti 4 года назад
but how about making a video on atmel assembly for the rest of us? I can't think of a reason to go that low level for anything I have in mind personally. But if you have any nice ideas it would make a cool video or series.
@superdau
@superdau 4 года назад
@@WistrelChianti There are some operations in an AVR that need to happen within a defined number of clock cycles of each other. Or maybe you want to create a pulse of very defined width, if you are bitbanging some serial protocol for example. Or you have some external device that has a certain setup time for some inputs before/outputs after you change some other pin. Or one part of the machine code that the C compiler creates is just not fast enough (if that happens all the time you should question your programming skills/hardware choice though). There are quite a few cases where knowing assembler is useful or necessary. But there is usually no need to write all the program in it.
@althuelectronics5158
@althuelectronics5158 3 года назад
hi my brother nice ok
@fuzzy1dk
@fuzzy1dk 4 года назад
a trap that can be a real time waster is that things like what is seen as decimal point by functions like atof() is determined by the c "locale"
@WistrelChianti
@WistrelChianti 4 года назад
That's interesting... so I'd always thought a decimal point was universally like: 42.2 What's the "locale" based alternative? Surely not: 42,2 ?
@fuzzy1dk
@fuzzy1dk 4 года назад
it surely is 42,2 ;)
@WistrelChianti
@WistrelChianti 4 года назад
I'd click "like", but really I want to click "ewww!"
@rj44319
@rj44319 4 года назад
I thought your youtube channel was dead?
@ElectroLab0
@ElectroLab0 3 года назад
This is really helpful. Working very well with Serial ports. One problem I'm facing is when I'm using the SoftwareSerial port! I'm using a GSM module to parse some text from an SMS I'll send. But not working here! :( Here's the screenshot of the code portion and the serial output! prnt.sc/wfwv2q @#Kevin_Darrah or anyone here can help, please?
@jfzaremba1
@jfzaremba1 5 месяцев назад
Thank you, Informative, great for a hobbyist like me, Lesson plans r awesome, Bless u 4 putting this info on RU-vid!
Далее
Arduino OOP [40-Minute Crash Course]
42:12
Просмотров 17 тыс.
C Strings in 20 Minutes | C and C++
20:56
Просмотров 6 тыс.
ПРОВЕРИЛ АРБУЗЫ #shorts
00:34
Просмотров 2 млн
Using EEPROM with Arduino - Internal & External
31:54
Просмотров 193 тыс.
How To Read from ESP32 - CLONE/BACKUP Everything
23:57
Arduino - How to Save a String into EEPROM
11:31
Просмотров 16 тыс.
✅Arduino Lessons arrays
9:09
Просмотров 6 тыс.
STM32 Guide #2: Registers + HAL (Blink example)
30:02
Просмотров 187 тыс.
Using Serial.parseInt() with Arduino
15:39
Просмотров 46 тыс.