C programming- pointer problem

Discussion in 'Programmer's Corner' started by varden, Jul 30, 2016.

  1. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    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?
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,340
    1,850
    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?
     
    varden and JWHassler like this.
  3. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    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).
     
  4. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    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?
     
  5. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    But doesn't this fill all array but not only empt space?
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,340
    1,850
    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.
     
    varden likes this.
  7. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    @Papabravo Now I get the idea in my mind. Thanks again for your helps :)
     
  8. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    3,011
    2,357
    +1
     
    Papabravo likes this.
  9. odm4286

    Active Member

    Sep 20, 2009
    155
    5
    I like number two
    Code (C):
    1. 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...
    Code (C):
    1. int digit_count = 0;
    2. for(int i = 0; my_data[i] != '\0'; i++)                                                  /* Now i is your total length and digit_count is the number of digits */
    3.      if(my_data[i] >= 48 && my_data[i] <= 57)
    4.           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: Aug 8, 2016
    varden likes this.
  10. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    I was about to ask why 48 and 57 then saw your message :D Indeed it helped. Thank you :)
     
  11. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,902
    878
    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 (C):
    1. int myLen 0;
    2. string myNumber "1234";
    3. MyLen = myNumber.length;
    4.  
    Ok, it looks like Mikro C has a string library, with a strlen function.
     
Loading...