473,372 Members | 1,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

How to convert integer to string array without specify array size?

Hi,

How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?

Thanks!

Dec 7 '05 #1
7 13850
he******@gmail.com wrote:
How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?


You can't. Where would you put it? In random, sizeless memory? There is
no such thing. Whenever you have an array, you will have had to specify
its size somehow.

Richard
Dec 7 '05 #2
Hi Richard,

Thank you for you reply!

But how can I read an integer for example, 12113, one by one for
comparison?

Thanks?

Dec 7 '05 #3
he******@gmail.com wrote:
How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?


You can use dynamically allocated memory. To get the size of the string
needed, you can find the log (base 10) of your number, truncate it, and
add 1.

For example for 12113...

size = floor(log10(12113)) + 1 = 4 + 1 = 5

You can now allocate a string with size characters using malloc and use
it to store your converted string.

Dec 7 '05 #4
why couldn't you just use asprintf ?

Saif wrote:
he******@gmail.com wrote:
How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?


You can use dynamically allocated memory. To get the size of the string
needed, you can find the log (base 10) of your number, truncate it, and
add 1.

For example for 12113...

size = floor(log10(12113)) + 1 = 4 + 1 = 5

You can now allocate a string with size characters using malloc and use
it to store your converted string.


Dec 7 '05 #5
Ja**********@gmail.com writes:
why couldn't you just use asprintf ?


There is no such thing as asprintf in C.

DES
--
Dag-Erling Smørgrav - de*@des.no
Dec 7 '05 #6
"Saif" <sa****@gmail.com> writes:
he******@gmail.com wrote:
How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?


You can use dynamically allocated memory. To get the size of the string
needed, you can find the log (base 10) of your number, truncate it, and
add 1.

For example for 12113...

size = floor(log10(12113)) + 1 = 4 + 1 = 5


If I'm going to convert it to an char array I'd use snprintf to
calculate the size:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char* buf = NULL;
int x = 12113;
size_t sz = snprintf(buf, 0, "%d", x) + 1;
buf = malloc(sz);
snprintf(buf, sz, "%d", x);
/* do something with buf */
free(buf);

return 0;
}
Dec 7 '05 #7
Niklas Norrthon wrote:
If I'm going to convert it to an char array I'd use snprintf to
calculate the size:


snprintf is a new function added in C99 and may not be supported by all
compilers. I know it's not supported in mine.

Dec 8 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
2
by: Mel WEaver | last post by:
Hello, I have the following delphi structure for a binary file. I'm looking for idea how to read this file. Mel type TMenuDataStruct = packed record exename : string;
4
by: Bill Borg | last post by:
Hello, I am using hashpasswordforstoringinconfigfile with md5 to generate a 32-character hashed password. But, since that string is only hex characters and is really only 128 bits (16 bytes),...
6
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public...
43
by: Steven T. Hatton | last post by:
http://public.research.att.com/~bs/bs_faq2.html#int-to-string Is there no C library function that will take an int and convert it to its ascii representation? The example Bjarne shows in his faq...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.