0

I'm trying to convert a decimal value into a string. I use the STM32CubeIDE IDE but am getting an error like 'Invalid binary operator'. I'm able to set &n to the decimal value 3695. I need to convert that into a string. How would I do that?

void main() 
{

    uint8_t TxArr;
    uint16_t Data;
    int a[10];
    int i;
   while (1)
   {
     HAL_I2C_Master_Transmit(&hi2c1,0x16, &TxArr, 1, 1000);
     HAL_I2C_Master_Receive(&hi2c1, 0x17, &Data, 2, 1000);
     for(i=0;i<4;i++)
      {
      a[i]=Data%10+0x30;     //value in Data is 3695.
      Data=Data/10;
      HAL_UART_Transmit(&huart3, a[i], 11, 100);
      HAL_Delay(300);
      }

      }
Mohan Khedkar
  • 33
  • 1
  • 6
  • please format your code properly, this is just unreadable. (hint it's probably something with mixing tabs and spaces) – Tarick Welling Oct 31 '19 at 14:59
  • @TarickWelling,thanks for your response sir.First time i asked question in this environment,so sorry for the mistake done in writing code.Now i edited it please check it once. – Mohan Khedkar Nov 01 '19 at 03:55

2 Answers2

3

The error is produced by:

n = n / 10;

What this would do if n was a pointer (int* n) is that it would repoint n to a location one tenth the original number. You will need to begin using normal array operators if you want your code to work properly.

The second problem you have is the following: You declare n as a array of 10 integers of type and with int.

int n[10];

Then in your for loop you try to do this:

n > 0; 

This is an invalid operation as n wil decay into a pointer and as such the address of your array is compared to 0. This will always evaluate as TRUE!

A good way to convert a integer to a string (char array) is this answer. In your sitaution that would be

int yourToBeConvertedNumber;
char str[INT_MAX]; // or any other reasonable upper bound you have set for the input data.
snprintf(str, sizeof(str), "%d", yourToBeConvertedNumber);
LegendofPedro
  • 1,393
  • 2
  • 11
  • 23
Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
  • 2
    `str[INT_MAX];` - this is creating a VERY VERY BIG string. You don't want `INT_MAX` bytes, you want enough bytes to store `INT_MAX` in base-10 representation, that is `log10(INT_MAX)`, which is like 10 characters for 32-bit `int`. So `char str[11]` is enough. – KamilCuk Oct 23 '19 at 17:15
  • @KamilCuk feel free to add to the post. It was just a quick bit of code which at least didn't have a buffer overflow in it. But yeah `INT_MAX` is quite stupid. – Tarick Welling Oct 23 '19 at 18:57
  • @Tarick Welling,thanks for your response sir.I used this snprintf(str, sizeof(str), "%d", yourToBeConvertedNumber); but it just display ASCII value ,I want to transmit this ASCII value in matlab for that we have to store this value in a array,so how can i do it? – Mohan Khedkar Oct 31 '19 at 12:29
  • of course it displays garbage. What is `0x30 + 9` and what is different when you do `0x30 + 10`? You didn't read/do the last part of the answer – Tarick Welling Oct 31 '19 at 15:02
  • @Tarick Welling,thanks for your quick response sir,0x30+9 this is for taking ascii value of 9.Initially i get decimal value like 3695 and converting each digit into ascii by seperating each digit. – Mohan Khedkar Nov 01 '19 at 03:47
0

Generally speaking, to convert an integer to a string, you can use the sprintf function. It should be available in newliband even newlib-nano standard C libraries.

However my guess here is that you have an array of integers where each element is a number between 0 and 9 ?

If this is true you have several issues: you seem to handle the variable n like an integer and not an address. Also your string should be one element longer and composed of chars.

You may do something like this:

char a[11];
for(i=0;i<10;i++)
{
   a[i]=n[i]%10 +'0'; 
}
a[11]='\0';
Guillaume Petitjean
  • 2,408
  • 1
  • 21
  • 47
  • @Petitjean thanks for your response sir,i refer your suggestion and made some changes in my code as given above,please check it once and correct me.By this code i am getting ascii value of each digit and stored in a array but when i am trying to check it on hyperterminal or docklight ,it shows garbage value.Any suggestion over it sir? – Mohan Khedkar Oct 31 '19 at 13:11