?
Solved

long to ascii

Posted on 1999-07-12
8
Medium Priority
?
396 Views
Last Modified: 2011-09-20
How do I convert longs to ascii strings?  There probably is a function to do this, but what is it and what library is it in?
0
Comment
Question by:VEngineer
8 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263526
check out sprintf
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263527
or itoa and ultoa
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263528
Syntax

#include <stdlib.h>
char *itoa(int value, char *string, int radix);

Description

Converts an integer to a string.
itoa converts value to a null-terminated string and stores the result in string. With itoa, value is an integer.
radix specifies the base to be used in converting value; it must be between 2 and 36, inclusive. If value is negative and radix is 10, the first character of string is the minus sign (-).

Note:      The space allocated for string must be large enough to hold the returned string, including the terminating null character (\0). itoa can return up to 17 bytes.

Return Value

itoa returns a pointer to string.

0
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263529
Syntax

#include <stdio.h>
int sprintf(char *buffer, const char *format[, argument, ...]);

Description

Writes formatted output to a string.

Note:      For details on format specifiers, see printf.

sprintf accepts a series of arguments, applies to each a format specifier contained in the format string pointed to by format, and outputs the formatted data to a string.
sprintf applies the first format specifier to the first argument, the second to the second, and so on. There must be the same number of format specifiers as arguments.

Return Value

On success, sprintf returns the number of bytes output. The return value does not include the terminating null byte in the count.
On error, sprintf returns EOF.
0
 
LVL 2

Author Comment

by:VEngineer
ID: 1263530
in C++, I've used itoa() from stdlib, but the C compiler that I am using does not have itoa() or _itoa() defined.. this thing must be primitive.. so I'm looking around for some alternate functions..

0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263531
sprintf() should be supported. In use it is similar to printf().
0
 
LVL 6

Accepted Solution

by:
Triskelion earned 80 total points
ID: 1263532
Here is an example based on the answers given above


void main(void)
{
   long lData=123;
   char sData[100];
   sprintf(sData, "%ld", lData);
   printf("The answer is %s", sData);
}
0
 

Expert Comment

by:Oliv
ID: 1263533
If you can't use itoa, itol, sprintf ore some basics functions like that, you could do thisl...

If you have to convert your 32 bits integer as a string (not in exponential format) :

unsigned long bibi;

char str[10];

str[0]=bibi/100000000+'0';
str[1]=(bibi%100000000)/10000000+'0';
str[2]=(bibi%10000000)/1000000+'0';
str[3]=(bibi%1000000)/100000+'0';
str[4]=(bibi%100000)/10000+'0';
str[5]=(bibi%10000)/1000+'0';
str[6]=(bibi%1000)/100+'0';
str[7]=(bibi%100)/10+'0';
str[8]=(bibi%10)+'0';
str[9]=0;

It works well...
bibi=429967295 will make str to be "429967295"
bibi=42 -> str=000000042
just your turn to change leading zero char by spaces if you don't want them
:)



0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question