Author Topic: convert variable unsigned char string to float and int in keil 5  (Read 1832 times)

0 Members and 1 Guest are viewing this topic.

Offline tariqTopic starter

  • Contributor
  • Posts: 24
  • Country: ir
hi
i want to convert array of strings to int and float
for example my string data is :

unsigned char a[4]="24.5";
float f;

i want to put  characters as a float to f .
and Similarly for int.
what should i do?

and in opposite what should i do to convert int or float into unsigned char.what is their functions?

Thank you very much.

 

Offline greenpossum

  • Frequent Contributor
  • **
  • Posts: 408
  • Country: au
Re: convert variable unsigned char string to float and int in keil 5
« Reply #1 on: August 12, 2020, 11:59:14 am »
Is the 24.5 a constant string or could it be read in as a variable. Because I'm wondering why you don't just initialise f directly, i.e. float f = 24.5f.
 

Offline tariqTopic starter

  • Contributor
  • Posts: 24
  • Country: ir
Re: convert variable unsigned char string to float and int in keil 5
« Reply #2 on: August 12, 2020, 12:20:27 pm »
no i receive my data as characters from uart unit of stm32 microcontroller by this command

HAL_UART_Receive(&huart1,data,5,1000) ;

my data is variable and it is numbers in character is there any command to convert data into float?
 

Offline MosherIV

  • Super Contributor
  • ***
  • Posts: 1530
  • Country: gb
Re: convert variable unsigned char string to float and int in keil 5
« Reply #3 on: August 12, 2020, 01:01:48 pm »
Look in string.h library for your compiler.

Standard string library does have text number to int functions.
The float version depends on your compiler implementation.
 

Offline greenpossum

  • Frequent Contributor
  • **
  • Posts: 408
  • Country: au
Re: convert variable unsigned char string to float and int in keil 5
« Reply #4 on: August 12, 2020, 01:09:24 pm »
Look up the strtof and strtol functions and don't forget to check for errors.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5407
  • Country: gb
Re: convert variable unsigned char string to float and int in keil 5
« Reply #5 on: August 12, 2020, 01:17:55 pm »
I recommend you make the string a[5] or a[] so it can allocate an implicit ‘\0’ termination for use with C libraries.
 

Offline oPossum

  • Super Contributor
  • ***
  • Posts: 1447
  • Country: us
  • Very dangerous - may attack at any time
Re: convert variable unsigned char string to float and int in keil 5
« Reply #6 on: August 13, 2020, 08:17:52 am »
sscanf and sprintf

float/double may not be supported or require compiler flags to enable support

The functions used by sscanf/sprintf may be accessible (check the headers) for a more efficient implementation.
 

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 8763
  • Country: fi
Re: convert variable unsigned char string to float and int in keil 5
« Reply #7 on: August 13, 2020, 09:48:53 am »
atof() if you have that available. Doing exactly what you ask for, and nothing more, it's more lightweight and simpler than sscanf(). sscanf() is more capable/versatile. On some targets, atof() isn't available.

Good embedded compilers should have atoi, itoa, utoa, atof, ftoa etc. available because full printf/scanf family are quite big for many microcontroller targets, while just fine on general purpose desktop/mobile computing.
« Last Edit: August 13, 2020, 09:51:34 am by Siwastaja »
 

Online newbrain

  • Super Contributor
  • ***
  • Posts: 1768
  • Country: se
Re: convert variable unsigned char string to float and int in keil 5
« Reply #8 on: August 13, 2020, 02:58:11 pm »
The drawback I can see with the atox() family is the lack of error reporting.
Worse: if the string contains a value that cannot be represented in the target type, the behaviour is undefined.

As the string is coming from an UART, there's no guarantee it will be correct.

In this case, the strtox() functions provide a better way to check for errors.
Nandemo wa shiranai wa yo, shitteru koto dake.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf