Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

long to ascii

Posted on 1999-07-12
8
Medium Priority
?
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

722 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