473,473 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sprintf confusion

For my program , I want to convert an integer into array of characters. I
see C has an atoi function o convert from an array of characters to
integers but no standard function to convert from integer to an array opf
chars. Why so ? Does that conversion not come into practice ?
we have sprintf but that riddles with the buffer-overflow problem :( and
snprintf is not in ANSI but in C99.
I have a network program in C ( which is not topical here, hence I did
not post the code). The client sends a number to the server and server
squares the number and sends it back to the client. Now any communication
between them happens using characters. So client sends an array of
characters to the server:

arr[] = { '3', '\0' };
and the server reads and square the number and sends this array back to
the client:

arr_new[] = { '9', '\0' };
Right now I solved the problem using sprintf and by using the number whose
squares are of one digit and I have also limited the size of array to 2
only.

but is this the C way of doing things ?


--
http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)

Jun 27 '08 #1
4 1275
On May 15, 6:21*pm, arnuld <sunr...@see.sigs.invalidwrote:
For my program , I want to convert an integer into array of characters. I
see C has an atoi *function o convert from an array of characters to
integers but no standard function to convert from integer to an array opf
chars. Why so ? *Does that conversion not come into practice ?

we have sprintf but that riddles with the buffer-overflow problem :( and
snprintf is not in ANSI but in C99.
I have itoa() and ltoa() on my C system. But I suspect these may not
be standard [why not?].

Then sprintf seems a good alternative. Just give it a big enough
buffer size (certainly more than 2 characters!). After all how big can
an integer be? Give it 100 characters and be done with it.

Or use it to create your own itoa().

--
Bartc
Jun 27 '08 #2
Bart wrote:
On May 15, 6:21 pm, arnuld <sunr...@see.sigs.invalidwrote:
>For my program , I want to convert an integer into array of characters. I
see C has an atoi function o convert from an array of characters to
integers but no standard function to convert from integer to an array opf
chars. Why so ? Does that conversion not come into practice ?

we have sprintf but that riddles with the buffer-overflow problem :( and
snprintf is not in ANSI but in C99.

I have itoa() and ltoa() on my C system. But I suspect these may not
be standard [why not?].

Then sprintf seems a good alternative. Just give it a big enough
buffer size (certainly more than 2 characters!). After all how big can
an integer be? Give it 100 characters and be done with it.
I'm familiar with itoa
as a code example and an exercise from K&R, sec 3.6.
You still need a buffer with that.

--
pete
Jun 27 '08 #3
Bart said:

<snip>
After all how big can an integer be?
You can find out with sizeof.

If you want a buffer big enough to support an integer parameter n, you can
get one like this (given appropriate furniture, e.g. <limits.h>) - the
#defines are merely for explanatory purposes and I don't propose that you
use them in Real Code:

#define BYTES_PER_INT (sizeof n)
#define BITS_PER_BYTE (CHAR_BIT)
#define BITS_PER_INT (BYTES_PER_INT * BITS_PER_BYTE)
#define BIT_CAPACITY 3 /* see below */
#define ALLOW_FOR_MINUS 1
#define ALLOW_FOR_TERM 1
#define DIGIT_COUNT ((BITS_PER_INT + BIT_CAPACITY - 1) / BIT_CAPACITY)

#define BUFFER_SIZE (ALLOW_FOR_MINUS + DIGIT_COUNT + ALLOW_FOR_TERM)

Bit capacity: since decimal representation gives us ten digits ('0' through
'9'), each digit can represent log(2)10 = 3.32+ bits, so it can certainly
represent 3 bits.

Note that for decimal representation the calculated buffer size is always
sufficient, but it can err a little - always on the generous side.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #4
arnuld wrote:
For my program , I want to convert an integer into array of characters. I
see C has an atoi function o convert from an array of characters to
integers but no standard function to convert from integer to an array opf
chars. Why so ? Does that conversion not come into practice ?
Its commonplace to need to read input and convert it to numbers, but the
reverse is less common.
we have sprintf but that riddles with the buffer-overflow problem :( and
snprintf is not in ANSI but in C99.
sprintf() can be used effectively if you're careful.
Right now I solved the problem using sprintf and by using the number whose
squares are of one digit and I have also limited the size of array to 2
only.

but is this the C way of doing things ?
yes.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Jun 27 '08 #5

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

Similar topics

13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
3
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf,...
6
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make...
1
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
2
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
12
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???...
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
4
by: MimiMi | last post by:
I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly! I wouldn't be surprised if my problems have something to do with...
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.