Hi.
Currently I have:
how do I convert the above to a string of: "1234"Code:char my_array[4] = { '1', '2', '3', '4' };
Thanks.
Hi.
Currently I have:
how do I convert the above to a string of: "1234"Code:char my_array[4] = { '1', '2', '3', '4' };
Thanks.
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.
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);
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.
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