Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Convert Variable to Array in C

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hi,

Anyone know how to convert the variable to array elements?

Ex.
int Var = 12345;

after converted:

char VarAry[] = {"12345"); or
char VarAry[] = {'1','2','3','4','5'};

2Q: How can we directly know how many digits that we have in Var? From there we can know we got 5digits.

Please guide me how to write the C program...

Thank You.
 

The easy solution is to use sprintf. It will do the conversion and tell you how many characters it wrote. However, sprintf is big. If you need a small function, check your compiler's library to see if it provides an int-to-string function. If still no luck, someone can probably help you write one.

I can think of a couple of ways to compute the number of digits before doing the conversion:
1. A series of comparisons: Is it greater than 9? Greater than 99? Greater than 999?
2. If the number is greater than zero, calculate 1 + floor(log10(Var))
 

Hi,

echo47 said:
I can think of a couple of ways to compute the number of digits before doing the conversion:
1. A series of comparisons: Is it greater than 9? Greater than 99? Greater than 999?
2. If the number is greater than zero, calculate 1 + floor(log10(Var))

Can i have the program pattern which can help me to do the converting? My purpose not for display the word. I just wan it carry the Var digits to Array.

Please show me a sample program?

Thank You.
 

Here is an example of the sprintf method:

Code:
#include <stdio.h>

int main(void)
{
  int digits, Var=12345;
  char VarAry[20];

  digits = sprintf(VarAry, "%d", Var);
  printf("Var=%d VarAry=%s digits=%d\n", Var, VarAry, digits);
  return 0;
}
The output is:
Var=12345 VarAry=12345 digits=5

To see other methods, try searching Google Groups:
https://groups.google.com/groups/search?q=c+convert+int+to+string+-c++
 

Hi,

Thank for your sample program. It very usefull. Thank alot.

The sprintf is big. Do you got any alternative way just to find out how many digits that Var have?

Thank You.
 

Maybe do something like this:
Code:
unsigned decimal_digits(unsigned var)
{
  if (var <= 9) return 1;
  if (var <= 99) return 2;
  if (var <= 999) return 3;
  if (var <= 9999) return 4;
  if (var <= 99999) return 5;
  return 0;  /* error, I have only five fingers on my hand */
}
If you need faster execution, you could rearrange the 'if' statements so the most likely ones are executed first, or use a binary search method.

If your null-terminated string already exists, the strlen function will tell you its length.
 

echo47 said:
Maybe do something like this:
Code:
unsigned decimal_digits(unsigned var)
{
  if (var <= 9) return 1;
  if (var <= 99) return 2;
  if (var <= 999) return 3;
  if (var <= 9999) return 4;
  if (var <= 99999) return 5;
  return 0;  /* error, too many digits */
}

If we using this method our Var is constant value already. let say our Var = 42539, so we get trouble already.

Is it posible we convert the Var = 42539 to String Var = "42539" then we use sizeof function, from the size we can know how many digits that we have already right?

Is it the efficiency(fast execution or least program load) way to do that?

Thank You.
 

I don't understand your concern about "constant value". If you call decimal_digits with 42539, it returns "5". Of course, if you want to measure values higher than 99999, you need to add more "if" statements.

It's impossible to say if that's the most efficient way to code the function. I don't know your processor type, or your compiler type, or your typical values of "var". Even if I did know those things, I probably don't have your compiler to test.

You can use strlen to measure the length of a string. You don't want sizeof.
 

Hi,

Sorry, Now then i understand already. Thank You.

So, It is the better the way use "if" statements to verified the digits. More faster i think.

I am writing the 8051 uC code, Which is using C language. I am using Keil compiler. It is very good compiler, it can simulate the code each step of elap time. That's why sometime i will care the speed while running the code routing and the program size because the uC internal memory got limitation.

Do you got any idea.. to convert the Variable to String?

Thank You Very Much.
 

What do you need is standard C Library funtion itoa, if your compiler don't support itoa, you can build your own itoa like this:
Code:
#include <string.h>
void itostr(unsigned int val, unsigned char *result)
{
	static char buf[32] = {0};
	int i = 30;
	
	for(; val && i ; --i, val /= 10)
			buf[i] = "0123456789"[val % 10];
	strcpy(result,&buf[i+1]);
}
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Some compilers provide an itoa (or similar) function, but it is not a standard ANSI C function. If you use it, remember that your code may not be portable to other compilers.

For other conversion examples, see the "Google Groups" search link above.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Hi,

Code:
#include <string.h>
void itostr(unsigned int val, unsigned char *result)
{
	static char buf[32] = {0};
	int i = 30;
	
	for(; val && i ; --i, val /= 10)
			buf[i] = "0123456789"[val % 10];
	strcpy(result,&buf[i+1]);
}

For the code, how we write the call function? Please show me. What's the result value bring back to call function, is it a String?

Thank You.
 

That 'itostr' function returns nothing (it is declared 'void'). However, it writes a string into the char array pointed to by 'result'. Here is an example calling function:

Code:
#include <stdio.h>
int main(void)
{
  unsigned Var = 12345;
  char VarAry[32];

  itostr(Var, VarAry);
  printf("Var=%u VarAry=%s\n", Var, VarAry);
  return 0;
}
It outputs this text:
Var=12345 VarAry=12345

'itostr' is inefficient. I recommend cleaning it up before using it for serious work.
 

echo47 said:
'itostr' is inefficient. I recommend cleaning it up before using it for serious work.

What you mean that? I still not understand. Please can you explain again? But the code that budhy given is work, i can get the result.

Thank You.
 

It works, but it is inefficient:
1. It writes the string into a temporary buffer and then copies the temporary buffer to the output buffer, instead of simply writing the string directly into the output buffer.
2. I consumes 32 bytes of data memory even when the function isn't running.
3. It requires two divisions per output character, unless the compiler's optimizer is very smart. (Division is usually an expensive operation.) To determine how smart the optimizer is, you can examine its assembler output.
 

Hi,

Thank for your good explanation. From your explanation it look like simply use the data to store in memory storage and took alot of time to pass the value from the buffer.

Do you got any Good ideas? How can we do it more efficient? I search from the Google Groups, there got few example but it took longest time for execution.
ex1:
Code:
char *itos(char *s,int i) 
{  char *r = s; 
   int t; 
   if (i < 0) 
   {  i = -i; 
      *s++ = '-'; 
   } 
   t = i; 
   do 
   ++s; 
   while (t /= 10); 
   
   *s = '\0'; 
   
   do 
   *--s = '0' + i % 10; 
   while (i /= 10); 
   
   return s; 
}

ex2:
Code:
char *itos(char *s,unsigned i) 
{  unsigned t = i; 
   do ++s; while (t /= 10); 
   *s = '\0'; 
   do *--s = '0' + i % 10; while (i /= 10); 
   return s; 
}

Thank You.
 

Ouch! Those two require up to three divisions per character, depending on optimizer quality.

Performance depends on your CPU type and optimizer quality. Which CPU and compiler are are you using? Maybe someone here is familiar with your specific combination.

Check your compiler documentation to see if it provides a suitable well-optimized library function.
 

Hi,

I am using AT89C52 Microcontroller and Compiler Keil that i using. My compiler don't have that function only have Data-Convertion (eg:Converts a string to a float,Converts a string to an int and so on).

Thank again....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top