C programming- pointer problem

Thread Starter

varden

Joined Jul 11, 2016
38
Hi. I am working on a bluetooth circuit that gets data and writes it to SD card and hope you guys can help me. The problem is I need to know how many digits message has in order to write it to SD. If there is any other ways to do this without knowing digits, I sadly don't know.(I use mikro c and couldn't find anything in help section.) Is there any way that I can transfer number of digits to pointer or something else after entering message?
 

Papabravo

Joined Feb 24, 2006
21,226
There are several ways to do this. Which method you use depends on how much flexibility you have in constructing the original message.
  1. You can embed the length of the message in the message as is done with the popular Intel Hex file format.
  2. You can use a terminator character, like a C string, so you can count the characters before you send them to the SD card.
  3. You can make every frame contain the same number of digits and pad a frame with "null" characters if there too few.
Those are three methods I can think of off the top of my head, but I think we might need more details on how the messages are constructed in the first place. Final question: why do you need to know how much data you have in order to write it to the SD card?
 

Thread Starter

varden

Joined Jul 11, 2016
38
Thank you Papabravo. At mikro c library, function also needs how many character would you like to write. If you say something more than your message, message go wrong. And I understand the logic but can you give examples? Or link that I can study(if not forbidden to share link :D).
 

Thread Starter

varden

Joined Jul 11, 2016
38
I send data with Bluetooth terminal program from my phone to hc-05 Bluetooth module and take it into the array. How can I fill blank space with null characters?
 

Papabravo

Joined Feb 24, 2006
21,226
OK -- sorry I deleted my post trying to figure out how to get rid of !@#$%^&*() line numbers. Why TF can't there be an easy straightforward way to do this?

@arrden you use the same structure but instead of a literal '\0' character you get the next character from the terminal application. One of two things will happen:
  1. You will put ARRAY_LENGTH characters into the array and wite the array to the SD card, OR
  2. You will receive and End of Message from the terminal application and you will pad out the array with nulls and write the data to the SD card.
 

odm4286

Joined Sep 20, 2009
265
There are several ways to do this. Which method you use depends on how much flexibility you have in constructing the original message.
  1. You can embed the length of the message in the message as is done with the popular Intel Hex file format.
  2. You can use a terminator character, like a C string, so you can count the characters before you send them to the SD card.
  3. You can make every frame contain the same number of digits and pad a frame with "null" characters if there too few.
Those are three methods I can think of off the top of my head, but I think we might need more details on how the messages are constructed in the first place. Final question: why do you need to know how much data you have in order to write it to the SD card?
I like number two
C:
for(int i = 0; my_data[i] != '\0'; i++);                                                 /* Then just use i as your data size. */
if you have to actually count digits then do the same but add...
C:
int digit_count = 0;
for(int i = 0; my_data[i] != '\0'; i++)                                                  /* Now i is your total length and digit_count is the number of digits */
     if(my_data[i] >= 48 && my_data[i] <= 57)
          digit_count++;
If you don't understand why I used 48 and 57 look up an Ascii table, I'd post a picture for you but I'm late for work. Hopefully this helps!
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,163
Does micro C have a string data type? Could it be used here instead of a char array? Then the .length method can be used to get the number of digits...
Code:
int myLen 0;
string myNumber "1234";
MyLen = myNumber.length;
Ok, it looks like Mikro C has a string library, with a strlen function.
 
Top