Embedded C - string to char array [closed]












-3















I've got string as follows
"123 132 244" where that max value for an entry is 255



how can i convert that string to



unsigned char arr[3] = [123, ,132, ,244]










share|improve this question















closed as unclear what you're asking by user694733, Jonathon Reinhart, gsamaras, Broman, Groo Nov 19 '18 at 13:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 4





    What is [123, ,132, ,244] supposed to stand for?

    – StoryTeller
    Nov 19 '18 at 13:13











  • edited question

    – mustafabarakat
    Nov 19 '18 at 13:13






  • 1





    You can use strtok and delimit the string with white space and save it char array?

    – danglingpointer
    Nov 19 '18 at 13:14













  • yes but the resulted array will be only one char in each element

    – mustafabarakat
    Nov 19 '18 at 13:15






  • 2





    unsigned char arr[3] = [123, ,132, ,244] is not valid syntax and does not compile. Did you mean unsigned char arr[3] = {123, 132, 244};?

    – user694733
    Nov 19 '18 at 13:19
















-3















I've got string as follows
"123 132 244" where that max value for an entry is 255



how can i convert that string to



unsigned char arr[3] = [123, ,132, ,244]










share|improve this question















closed as unclear what you're asking by user694733, Jonathon Reinhart, gsamaras, Broman, Groo Nov 19 '18 at 13:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 4





    What is [123, ,132, ,244] supposed to stand for?

    – StoryTeller
    Nov 19 '18 at 13:13











  • edited question

    – mustafabarakat
    Nov 19 '18 at 13:13






  • 1





    You can use strtok and delimit the string with white space and save it char array?

    – danglingpointer
    Nov 19 '18 at 13:14













  • yes but the resulted array will be only one char in each element

    – mustafabarakat
    Nov 19 '18 at 13:15






  • 2





    unsigned char arr[3] = [123, ,132, ,244] is not valid syntax and does not compile. Did you mean unsigned char arr[3] = {123, 132, 244};?

    – user694733
    Nov 19 '18 at 13:19














-3












-3








-3








I've got string as follows
"123 132 244" where that max value for an entry is 255



how can i convert that string to



unsigned char arr[3] = [123, ,132, ,244]










share|improve this question
















I've got string as follows
"123 132 244" where that max value for an entry is 255



how can i convert that string to



unsigned char arr[3] = [123, ,132, ,244]







c embedded






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:13







mustafabarakat

















asked Nov 19 '18 at 13:11









mustafabarakatmustafabarakat

268




268




closed as unclear what you're asking by user694733, Jonathon Reinhart, gsamaras, Broman, Groo Nov 19 '18 at 13:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as unclear what you're asking by user694733, Jonathon Reinhart, gsamaras, Broman, Groo Nov 19 '18 at 13:27


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 4





    What is [123, ,132, ,244] supposed to stand for?

    – StoryTeller
    Nov 19 '18 at 13:13











  • edited question

    – mustafabarakat
    Nov 19 '18 at 13:13






  • 1





    You can use strtok and delimit the string with white space and save it char array?

    – danglingpointer
    Nov 19 '18 at 13:14













  • yes but the resulted array will be only one char in each element

    – mustafabarakat
    Nov 19 '18 at 13:15






  • 2





    unsigned char arr[3] = [123, ,132, ,244] is not valid syntax and does not compile. Did you mean unsigned char arr[3] = {123, 132, 244};?

    – user694733
    Nov 19 '18 at 13:19














  • 4





    What is [123, ,132, ,244] supposed to stand for?

    – StoryTeller
    Nov 19 '18 at 13:13











  • edited question

    – mustafabarakat
    Nov 19 '18 at 13:13






  • 1





    You can use strtok and delimit the string with white space and save it char array?

    – danglingpointer
    Nov 19 '18 at 13:14













  • yes but the resulted array will be only one char in each element

    – mustafabarakat
    Nov 19 '18 at 13:15






  • 2





    unsigned char arr[3] = [123, ,132, ,244] is not valid syntax and does not compile. Did you mean unsigned char arr[3] = {123, 132, 244};?

    – user694733
    Nov 19 '18 at 13:19








4




4





What is [123, ,132, ,244] supposed to stand for?

– StoryTeller
Nov 19 '18 at 13:13





What is [123, ,132, ,244] supposed to stand for?

– StoryTeller
Nov 19 '18 at 13:13













edited question

– mustafabarakat
Nov 19 '18 at 13:13





edited question

– mustafabarakat
Nov 19 '18 at 13:13




1




1





You can use strtok and delimit the string with white space and save it char array?

– danglingpointer
Nov 19 '18 at 13:14







You can use strtok and delimit the string with white space and save it char array?

– danglingpointer
Nov 19 '18 at 13:14















yes but the resulted array will be only one char in each element

– mustafabarakat
Nov 19 '18 at 13:15





yes but the resulted array will be only one char in each element

– mustafabarakat
Nov 19 '18 at 13:15




2




2





unsigned char arr[3] = [123, ,132, ,244] is not valid syntax and does not compile. Did you mean unsigned char arr[3] = {123, 132, 244};?

– user694733
Nov 19 '18 at 13:19





unsigned char arr[3] = [123, ,132, ,244] is not valid syntax and does not compile. Did you mean unsigned char arr[3] = {123, 132, 244};?

– user694733
Nov 19 '18 at 13:19












2 Answers
2






active

oldest

votes


















1















  1. Step 1 : valid the string's format (number - one space - number - one
    space ...)

  2. Step 2 : Count the number of space

  3. Step 3 : Allocate the final array

  4. Step 4 : use strtok to have token + sscanf to convert the string to number

  5. Step 5 : return array (+ array size ? It's always useful).


In which step do you have difficulty ?






share|improve this answer
























  • The tag embedded may not be insignificant. It's possible string.h (etc.) is not available.

    – Fiddling Bits
    Nov 19 '18 at 13:26











  • @FiddlingBits I'm not familiar with embedded C but a "manual" strotk and sscanf for int is still possible. You can even do the 2 step in one if you have to recode that part.

    – Tom's
    Nov 19 '18 at 13:32



















0














The expression "123 132 244" is a string literal. If you have access to string functions such as strtok() (defined in strings.h) then parse the string into three char values using strtok() and a string converter such as atoi() or strtol(). If you do not have access to strings.h, then the string to unsigned char conversion will need to be done another way. Here is one example:



const char str = {"123 132 244"};

void convertStr(const char *string, unsigned int ucArr[3], size_t size);

int main(void)
{
unsigned int ucArray[3];
convertStr(str, ucArray, sizeof(str)/sizeof(str[0]));

return 0;
}

void convertStr(const char *string, unsigned int ucArr[3], size_t size)
{
int i=0, j=0, k=0;
int accum[3] = {0};//store numeric version of 3 alpha characters
int ex = 0;//use in conversion of single digit value to place value.
int acm = 0;//accumulate integer value of 3 successive integer values
//stored in accum

while(1)
{
if(isdigit(*string))
{
accum[i] = *string - '0';
i++;
}
else
{
//convert
for(k=i-1;k>=0;k--)
{
ex = pow(10, k);
acm += accum[i-k-1]*ex;
}
ucArr[j] = acm;
j++;
i=0;
}
acm = 0;
if(*string == NULL) break;
*string++;
}
}





share|improve this answer





















  • 1





    It's more likely that an embedded system doesn't have access to pow than to string.h. pow would mean that you have to load in some software floating point library which you don't want unless you actually have FPU. Using strtoul in a little while loop is probably the correct solution.

    – Lundin
    Nov 19 '18 at 15:43













  • @Lundin - will edit...

    – ryyker
    Nov 19 '18 at 15:45






  • 1





    No need since the question is closed and this may all get deleted by the Roomba bot, eventually.

    – Lundin
    Nov 19 '18 at 15:46











  • @Lundin - Okay. Thanks for the input anyway. I do not do a lot of embedded, so don't know what most dev system provide for the smaller uPs.

    – ryyker
    Nov 19 '18 at 15:58


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















  1. Step 1 : valid the string's format (number - one space - number - one
    space ...)

  2. Step 2 : Count the number of space

  3. Step 3 : Allocate the final array

  4. Step 4 : use strtok to have token + sscanf to convert the string to number

  5. Step 5 : return array (+ array size ? It's always useful).


In which step do you have difficulty ?






share|improve this answer
























  • The tag embedded may not be insignificant. It's possible string.h (etc.) is not available.

    – Fiddling Bits
    Nov 19 '18 at 13:26











  • @FiddlingBits I'm not familiar with embedded C but a "manual" strotk and sscanf for int is still possible. You can even do the 2 step in one if you have to recode that part.

    – Tom's
    Nov 19 '18 at 13:32
















1















  1. Step 1 : valid the string's format (number - one space - number - one
    space ...)

  2. Step 2 : Count the number of space

  3. Step 3 : Allocate the final array

  4. Step 4 : use strtok to have token + sscanf to convert the string to number

  5. Step 5 : return array (+ array size ? It's always useful).


In which step do you have difficulty ?






share|improve this answer
























  • The tag embedded may not be insignificant. It's possible string.h (etc.) is not available.

    – Fiddling Bits
    Nov 19 '18 at 13:26











  • @FiddlingBits I'm not familiar with embedded C but a "manual" strotk and sscanf for int is still possible. You can even do the 2 step in one if you have to recode that part.

    – Tom's
    Nov 19 '18 at 13:32














1












1








1








  1. Step 1 : valid the string's format (number - one space - number - one
    space ...)

  2. Step 2 : Count the number of space

  3. Step 3 : Allocate the final array

  4. Step 4 : use strtok to have token + sscanf to convert the string to number

  5. Step 5 : return array (+ array size ? It's always useful).


In which step do you have difficulty ?






share|improve this answer














  1. Step 1 : valid the string's format (number - one space - number - one
    space ...)

  2. Step 2 : Count the number of space

  3. Step 3 : Allocate the final array

  4. Step 4 : use strtok to have token + sscanf to convert the string to number

  5. Step 5 : return array (+ array size ? It's always useful).


In which step do you have difficulty ?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 13:16









Tom'sTom's

2,042420




2,042420













  • The tag embedded may not be insignificant. It's possible string.h (etc.) is not available.

    – Fiddling Bits
    Nov 19 '18 at 13:26











  • @FiddlingBits I'm not familiar with embedded C but a "manual" strotk and sscanf for int is still possible. You can even do the 2 step in one if you have to recode that part.

    – Tom's
    Nov 19 '18 at 13:32



















  • The tag embedded may not be insignificant. It's possible string.h (etc.) is not available.

    – Fiddling Bits
    Nov 19 '18 at 13:26











  • @FiddlingBits I'm not familiar with embedded C but a "manual" strotk and sscanf for int is still possible. You can even do the 2 step in one if you have to recode that part.

    – Tom's
    Nov 19 '18 at 13:32

















The tag embedded may not be insignificant. It's possible string.h (etc.) is not available.

– Fiddling Bits
Nov 19 '18 at 13:26





The tag embedded may not be insignificant. It's possible string.h (etc.) is not available.

– Fiddling Bits
Nov 19 '18 at 13:26













@FiddlingBits I'm not familiar with embedded C but a "manual" strotk and sscanf for int is still possible. You can even do the 2 step in one if you have to recode that part.

– Tom's
Nov 19 '18 at 13:32





@FiddlingBits I'm not familiar with embedded C but a "manual" strotk and sscanf for int is still possible. You can even do the 2 step in one if you have to recode that part.

– Tom's
Nov 19 '18 at 13:32













0














The expression "123 132 244" is a string literal. If you have access to string functions such as strtok() (defined in strings.h) then parse the string into three char values using strtok() and a string converter such as atoi() or strtol(). If you do not have access to strings.h, then the string to unsigned char conversion will need to be done another way. Here is one example:



const char str = {"123 132 244"};

void convertStr(const char *string, unsigned int ucArr[3], size_t size);

int main(void)
{
unsigned int ucArray[3];
convertStr(str, ucArray, sizeof(str)/sizeof(str[0]));

return 0;
}

void convertStr(const char *string, unsigned int ucArr[3], size_t size)
{
int i=0, j=0, k=0;
int accum[3] = {0};//store numeric version of 3 alpha characters
int ex = 0;//use in conversion of single digit value to place value.
int acm = 0;//accumulate integer value of 3 successive integer values
//stored in accum

while(1)
{
if(isdigit(*string))
{
accum[i] = *string - '0';
i++;
}
else
{
//convert
for(k=i-1;k>=0;k--)
{
ex = pow(10, k);
acm += accum[i-k-1]*ex;
}
ucArr[j] = acm;
j++;
i=0;
}
acm = 0;
if(*string == NULL) break;
*string++;
}
}





share|improve this answer





















  • 1





    It's more likely that an embedded system doesn't have access to pow than to string.h. pow would mean that you have to load in some software floating point library which you don't want unless you actually have FPU. Using strtoul in a little while loop is probably the correct solution.

    – Lundin
    Nov 19 '18 at 15:43













  • @Lundin - will edit...

    – ryyker
    Nov 19 '18 at 15:45






  • 1





    No need since the question is closed and this may all get deleted by the Roomba bot, eventually.

    – Lundin
    Nov 19 '18 at 15:46











  • @Lundin - Okay. Thanks for the input anyway. I do not do a lot of embedded, so don't know what most dev system provide for the smaller uPs.

    – ryyker
    Nov 19 '18 at 15:58
















0














The expression "123 132 244" is a string literal. If you have access to string functions such as strtok() (defined in strings.h) then parse the string into three char values using strtok() and a string converter such as atoi() or strtol(). If you do not have access to strings.h, then the string to unsigned char conversion will need to be done another way. Here is one example:



const char str = {"123 132 244"};

void convertStr(const char *string, unsigned int ucArr[3], size_t size);

int main(void)
{
unsigned int ucArray[3];
convertStr(str, ucArray, sizeof(str)/sizeof(str[0]));

return 0;
}

void convertStr(const char *string, unsigned int ucArr[3], size_t size)
{
int i=0, j=0, k=0;
int accum[3] = {0};//store numeric version of 3 alpha characters
int ex = 0;//use in conversion of single digit value to place value.
int acm = 0;//accumulate integer value of 3 successive integer values
//stored in accum

while(1)
{
if(isdigit(*string))
{
accum[i] = *string - '0';
i++;
}
else
{
//convert
for(k=i-1;k>=0;k--)
{
ex = pow(10, k);
acm += accum[i-k-1]*ex;
}
ucArr[j] = acm;
j++;
i=0;
}
acm = 0;
if(*string == NULL) break;
*string++;
}
}





share|improve this answer





















  • 1





    It's more likely that an embedded system doesn't have access to pow than to string.h. pow would mean that you have to load in some software floating point library which you don't want unless you actually have FPU. Using strtoul in a little while loop is probably the correct solution.

    – Lundin
    Nov 19 '18 at 15:43













  • @Lundin - will edit...

    – ryyker
    Nov 19 '18 at 15:45






  • 1





    No need since the question is closed and this may all get deleted by the Roomba bot, eventually.

    – Lundin
    Nov 19 '18 at 15:46











  • @Lundin - Okay. Thanks for the input anyway. I do not do a lot of embedded, so don't know what most dev system provide for the smaller uPs.

    – ryyker
    Nov 19 '18 at 15:58














0












0








0







The expression "123 132 244" is a string literal. If you have access to string functions such as strtok() (defined in strings.h) then parse the string into three char values using strtok() and a string converter such as atoi() or strtol(). If you do not have access to strings.h, then the string to unsigned char conversion will need to be done another way. Here is one example:



const char str = {"123 132 244"};

void convertStr(const char *string, unsigned int ucArr[3], size_t size);

int main(void)
{
unsigned int ucArray[3];
convertStr(str, ucArray, sizeof(str)/sizeof(str[0]));

return 0;
}

void convertStr(const char *string, unsigned int ucArr[3], size_t size)
{
int i=0, j=0, k=0;
int accum[3] = {0};//store numeric version of 3 alpha characters
int ex = 0;//use in conversion of single digit value to place value.
int acm = 0;//accumulate integer value of 3 successive integer values
//stored in accum

while(1)
{
if(isdigit(*string))
{
accum[i] = *string - '0';
i++;
}
else
{
//convert
for(k=i-1;k>=0;k--)
{
ex = pow(10, k);
acm += accum[i-k-1]*ex;
}
ucArr[j] = acm;
j++;
i=0;
}
acm = 0;
if(*string == NULL) break;
*string++;
}
}





share|improve this answer















The expression "123 132 244" is a string literal. If you have access to string functions such as strtok() (defined in strings.h) then parse the string into three char values using strtok() and a string converter such as atoi() or strtol(). If you do not have access to strings.h, then the string to unsigned char conversion will need to be done another way. Here is one example:



const char str = {"123 132 244"};

void convertStr(const char *string, unsigned int ucArr[3], size_t size);

int main(void)
{
unsigned int ucArray[3];
convertStr(str, ucArray, sizeof(str)/sizeof(str[0]));

return 0;
}

void convertStr(const char *string, unsigned int ucArr[3], size_t size)
{
int i=0, j=0, k=0;
int accum[3] = {0};//store numeric version of 3 alpha characters
int ex = 0;//use in conversion of single digit value to place value.
int acm = 0;//accumulate integer value of 3 successive integer values
//stored in accum

while(1)
{
if(isdigit(*string))
{
accum[i] = *string - '0';
i++;
}
else
{
//convert
for(k=i-1;k>=0;k--)
{
ex = pow(10, k);
acm += accum[i-k-1]*ex;
}
ucArr[j] = acm;
j++;
i=0;
}
acm = 0;
if(*string == NULL) break;
*string++;
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 15:44

























answered Nov 19 '18 at 15:32









ryykerryyker

12.1k22858




12.1k22858








  • 1





    It's more likely that an embedded system doesn't have access to pow than to string.h. pow would mean that you have to load in some software floating point library which you don't want unless you actually have FPU. Using strtoul in a little while loop is probably the correct solution.

    – Lundin
    Nov 19 '18 at 15:43













  • @Lundin - will edit...

    – ryyker
    Nov 19 '18 at 15:45






  • 1





    No need since the question is closed and this may all get deleted by the Roomba bot, eventually.

    – Lundin
    Nov 19 '18 at 15:46











  • @Lundin - Okay. Thanks for the input anyway. I do not do a lot of embedded, so don't know what most dev system provide for the smaller uPs.

    – ryyker
    Nov 19 '18 at 15:58














  • 1





    It's more likely that an embedded system doesn't have access to pow than to string.h. pow would mean that you have to load in some software floating point library which you don't want unless you actually have FPU. Using strtoul in a little while loop is probably the correct solution.

    – Lundin
    Nov 19 '18 at 15:43













  • @Lundin - will edit...

    – ryyker
    Nov 19 '18 at 15:45






  • 1





    No need since the question is closed and this may all get deleted by the Roomba bot, eventually.

    – Lundin
    Nov 19 '18 at 15:46











  • @Lundin - Okay. Thanks for the input anyway. I do not do a lot of embedded, so don't know what most dev system provide for the smaller uPs.

    – ryyker
    Nov 19 '18 at 15:58








1




1





It's more likely that an embedded system doesn't have access to pow than to string.h. pow would mean that you have to load in some software floating point library which you don't want unless you actually have FPU. Using strtoul in a little while loop is probably the correct solution.

– Lundin
Nov 19 '18 at 15:43







It's more likely that an embedded system doesn't have access to pow than to string.h. pow would mean that you have to load in some software floating point library which you don't want unless you actually have FPU. Using strtoul in a little while loop is probably the correct solution.

– Lundin
Nov 19 '18 at 15:43















@Lundin - will edit...

– ryyker
Nov 19 '18 at 15:45





@Lundin - will edit...

– ryyker
Nov 19 '18 at 15:45




1




1





No need since the question is closed and this may all get deleted by the Roomba bot, eventually.

– Lundin
Nov 19 '18 at 15:46





No need since the question is closed and this may all get deleted by the Roomba bot, eventually.

– Lundin
Nov 19 '18 at 15:46













@Lundin - Okay. Thanks for the input anyway. I do not do a lot of embedded, so don't know what most dev system provide for the smaller uPs.

– ryyker
Nov 19 '18 at 15:58





@Lundin - Okay. Thanks for the input anyway. I do not do a lot of embedded, so don't know what most dev system provide for the smaller uPs.

– ryyker
Nov 19 '18 at 15:58



Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)