char array to string...

This is a discussion on char array to string... within the C Programming forums, part of the General Programming Boards category; Hi. Currently I have: Code: char my_array[4] = { '1', '2', '3', '4' }; how do I convert the above ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    char array to string...

    Hi.
    Currently I have:

    Code:
    char my_array[4] = { '1', '2', '3', '4' };
    how do I convert the above to a string of: "1234"

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Change it to "my_array[5]" because you need space for the null terminating character.

    But in general, "string" means "array of char".

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    char array to string...

    Quote Originally Posted by tabstop View Post
    Change it to "my_array[5]" because you need space for the null terminating character.

    But in general, "string" means "array of char".
    Here is my issue:
    I am logging voltage data and writing out to an SD card. My MCU ADC is a 10-bit device.
    My ADC value could range from a 8-bit ADC equivalent value to a max of 10-bit ADC equivalent value.

    I decided on my_array[4] be cause the max size of my final ADC result must be an integer type to hold a max 10-bit ADC value.

    The conversion from ADC to an array_char is correct. However here is the problem.

    example:
    low voltage reading = 0x1A ==> my_array[4] = "2600"
    high voltage reading = 0x03FE ==> my_array[4] = "1022"

    In the first case (low voltage) the SD did not write correctly since it saw the extra two zeros. the second case (high voltage) is ok since it did see what it was expecting.

    my question. Is there a way for me to "flexibly" declare an array to take any size? I have tried unsigned int my_array[] but the compiler complain about it.

    Sorry for the long clarification of my issue. thanks for your help.
    Last edited by dtvonly; 10-16-2013 at 10:09 AM.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    Ok? So use another char array to store the string version of it:
    Code:
    char my_array = { '1', '2', '3', '4' };
    char * s;
    
    s = malloc(sizeof my_array + 1);
    memcpy(s,my_array,sizeof my_array);
    s[sizeof my_array] = 0;
    printf("%s\n", s);

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your two examples are not consistent? 0xFE is decimal 254, so either "254" or "0254" would be what I would expect to see; but then 0x6338 is 25400 in decimal and that's not going to fit in four characters.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    I see a couple of issues with your calculations.

    First 0xFE is equal to 254, not 2540. Second 0x6338 is equal to 25400.

    How are you trying to convert your numbers into your array of characters, note this is not a string, in C a string requires the end of string character '\0' to terminate the string.

    How are you trying to write your character array to your SD card?

    Why are you trying to convert to the character array in the first place?

    I would increase the size of your buffer slightly, array[10], and use sprintf() to convert the number into a C-string for writing to your SD card if you want human readable data.

    And since this seems to be an embedded device, I recommend staying away from dynamic memory unless absolutely necessary.


    Jim

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    char array to string...

    Quote Originally Posted by jimblumberg View Post
    I see a couple of issues with your calculations.

    First 0xFE is equal to 254, not 2540. Second 0x6338 is equal to 25400.

    How are you trying to convert your numbers into your array of characters, note this is not a string, in C a string requires the end of string character '\0' to terminate the string.

    How are you trying to write your character array to your SD card?

    Why are you trying to convert to the character array in the first place?

    I would increase the size of your buffer slightly, array[10], and use sprintf() to convert the number into a C-string for writing to your SD card if you want human readable data.

    And since this seems to be an embedded device, I recommend staying away from dynamic memory unless absolutely necessary.
    Jim
    Hi Jim. Thanks for the advice. sprintf does work better and easier.
    problem solved! thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  2. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 04:52 AM
  3. Appending char to string (char array)
    By sniper83 in forum C Programming
    Replies: 14
    Last Post: 04-15-2008, 06:48 AM
  4. char array to string
    By Bad_Scooter in forum C++ Programming
    Replies: 5
    Last Post: 07-27-2003, 10:03 AM
  5. string to char array
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2002, 08:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21