Jump to content

Int to Char help C, STM32F429ZI

Shahin

I'm creating an int to char function, but struggling with parameter passing

 

char num_to_char(short Number, int POS) // Less than 100,000
{
char Num_char[5] = {'0','0','0','0','0'};
    
    while (Number > 10000)
    {
    Num_char[0]++;
    Number = Number - 10000;    
    }

    while (Number > 1000)
    {
    Num_char[1]++;
    Number = Number - 1000;    
    }
    
    while (Number > 100)
    {
    Num_char[2]++;
    Number = Number - 100;    
    }

    while (Number > 10)
    {
    Num_char[3]++;
    Number = Number - 10;    
    }
    
    while (Number > 1)
    {
    Num_char[4]++;
    Number = Number - 1;    
    }
return Num_char[POS];

 

 

 

 

calling back


voltage[0] = num_to_char( Number,  0);
voltage[1] = '.';
voltage[2] = num_to_char( Number, 1 );
voltage[3] = num_to_char( Number,  2);
voltage[4] = num_to_char( Number,  3);
voltage[5] =num_to_char( Number,  4);
voltage[6] = 'V';        

 

Any help?

Link to comment
Share on other sites

Link to post
Share on other sites

43 minutes ago, Shahin said:

Solved, Not sure how to delete post

even better than deleting it, you could post your solution so that the next person who finds this while googling can benefit from it :)

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/19/2018 at 12:26 PM, reniat said:

even better than deleting it, you could post your solution so that the next person who finds this while googling can benefit from it :)

 

My assumption would be that @Shahin realized the missing = signs in while loop (just a guess though) or realizing a short isn't good to use in this case.

 

Boundary cases are important

while(num >= 10000) captures the case where the number is 10000.

 

Anyways, @Shahin it is a good attempt, and glad you figured out a solution.  I do compliment you no the '0' and adding though.  A note, shorts range from -33k - 33k (roughly), so an int might be better (and then do an if statement to check if it is above 100k).  Also, since you are only returning 1 number, you can simplify it more with just some integer math

 

e.g.

return '0' + (Number / (int)pow(10,POS))%10; //It's late, I might be wrong in syntax, logic and such
//The concept being you divide by 10000 or whatever, and then %10 gets you the remainder (the value).

 

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

Well, because I'm bored, here's a solution in C if anyone cares:

Spoiler

#define MAX_SIZE 11
static char int_string[MAX_SIZE] = {0}; /* Assume 32-bit int, max digits is 10 + 1 for NUL */

void int_to_char(int input_num){
  	int exp_val = 1000000000;
  	int place = 0;
  
    /* Special case: if input is 0, fill in 0 and exit immediately*/
    if (input_num == 0){
      	int_string[0] = '0';
    }
  
  	/* Find out where the number starts from the most significant end */
    while ((input_num / exp_val) == 0){
      	exp_val /= 10;
    }
  
  	/* Fill in the numbers. */
    while (exp_val > 0){
      int_string[place++] = (char)((input_num / exp_val) + 0x30); /* 0x30 is 0 in most character encodings */
      input_num %= exp_val;
      exp_val /= 10;
    }
}

Notes:

  • This function either has two constant amount of comparisons, either 1 or 12. However it should be assumed 12 is going to always be the case.
  • This approach uses most significant to least significant digit to avoid adding leading zeroes. Least to most significant digit approach can't tell where the last actual most significant digit is until it traverses the whole number.

 

Link to comment
Share on other sites

Link to post
Share on other sites

#include <stdio.h>
  
char int_to_char(int n)
{
  char number[11];
  sprintf(number, "%d", n);
  return *number;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, mshaugh said:

#include <stdio.h>
  
char int_to_char(int n)
{
  char number[11];
  sprintf(number, "%d", n);
  return *number;
}

 

That only returns a single character, not a string.

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Unimportant said:

That only returns a single character, not a string.

You're definitely not wrong.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
  
char* int_to_char(int n)
{
  int length = (int)(n ? log10(n) + 1 : 1);
  char* number = (char*)(malloc(length+1));
  sprintf(number, "%d", n);
  
  return number;
}

int main(void)
{
  int n = 10000;
  
  char *number;
  number = int_to_char(n);
  
  printf("%s\n", number);
  free(number);
  
  return 0;
}

Alternatively just use `sprintf` in place of your own function.

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/22/2018 at 3:36 AM, mshaugh said:

You're definitely not wrong.

Alternatively just use `sprintf` in place of your own function.

While I like the sprintf method for being concise, I do have one particular issue with it. OP's target is an embedded systems processor.

 

Why is this an issue? Getting dynamic memory allocation to work on embedded systems processors seems to be a pain. A quick glance online about getting it setup involved things like messing with the linker script, memory mapping, among other things. And then there's the design aspect of it, such as figuring out a good enough heap size, if you have any way of gracefully handling memory leaks, etc. It also makes the system less deterministic. For all those reasons, especially when you're coding to bare metal, I don't recommend any sort of dynamic memory system for this sort of target.

Edited by M.Yurizaki
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×