473,387 Members | 1,456 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,387 software developers and data experts.

convertion of negative integers to string without using atoi function

char intToStr(int a)
{
int n, i, j, sign, set;
char r[10], s[10];

if (a[0] == '-')
sign = -1;
if (sign == -1)
{
i = 1;
n = a % 10;
s[i++] = n | '0';
a = a / 10;
}
else
{
i = 0;
n = a % 10;
s[i++] = n | '0';
a = a / 10;
}
i--;

while (i >= 0)
{
r[j++] = s[i--];
}
return r;
}
i m the beginer of c programing language
i have doubt at the time of handling of negative numbers at the time of covertng to string
Sep 21 '14 #1
2 4217
weaknessforcats
9,208 Expert Mod 8TB
Negative numbers are written in 2's complement format. They are not formatted like positive numbers with a sign bit set.

You get the 2's complement by reversing the bits an adding 1. Here is the bit pattern for 5:

00000101

Now reverse the bits:
11111010

and add 1:

11111010
00000001
11111011 <- 2's complement for -5

To convert -5 to plus 5 you reverse the bits and add 1:

00000100
00000001
---------
00000101 <- +5
Sep 21 '14 #2
donbock
2,426 Expert 2GB
You say you want to write a function that converts an integer into a string, but you refer to atoi (which converts a string to an integer). Which way do you want the conversion to go?

2's complement is by far the most common encoding technique, but others are permitted by the C Standard. If we assume that your function works properly for positive integers, then the following snippet handles almost all negative numbers properly. (It looks like you are sort of trying to do something like this.)
Expand|Select|Wrap|Line Numbers
  1. if (value < 0)
  2.    return intToStr(-value);
  3. else
  4.    return intToStr(value);
However, it fails for negative numbers where -value cannot be represented as a positive number (eg, |INT_MIN| > INT_MAX, as is typically the case for 2's complement). This is a corner case that can be hard to handle.

However, there are problems with intToStr:
  1. The function is defined to return a char, but it actually returns a pointer to char.
  2. The function returns a pointer to automatic variable r for use outside the scope of r.
  3. It tests a[0] as if a were a char pointer, but a is actually an int.
  4. sign is undefined if a[0] is not '-'.
  5. The function only converts a single decimal digit of the input.
  6. In the final loop that copies s to r, j is undefined.
  7. Converting integer digit to string digit via n | '0' will work for ASCII string encoding but could fail for other string encodings. The traditional way to do this is via n + '0'.
  8. There are more problems, but these are a good start.
Sep 22 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
30
by: ceeques | last post by:
Hi I am a novice in C. Could you guys help me solve this problem - I need to convert integer(and /short) to string without using sprintf (I dont have standard libray stdio.h). for...
2
by: Bernard Dhooghe | last post by:
The information center writes: "Encryption Algorithm: The internal encryption algorithm used is RC2 block cipher with padding, the 128-bit secret key is derived from the password using a MD2...
11
by: martin paul | last post by:
Sir please consider the following code...... int getint(char *); int main () { char name; printf("enter the string\n"); scanf("%s",name); getint(name); } ...
7
by: Suyash Upadhyay | last post by:
Hello all, I encountered a programming problem while going through interview questions asked by companies. But I stuck at this: Given an array of characters. How would you reverse it? How would...
2
by: durgapavani | last post by:
char str1 = "124z3yu87"; char str2 = "1000"; char *str3 = "100"; printf("str1: %d\n", atoi(str1)); printf("str2: %d\n", atoi(str2)); printf("str3: %d\n", atoi(str3)); str1: 124...
2
by: srini4vasan | last post by:
#include <stdio.h> int main() { char n, m; puts (" Enter the first string and . to terminate :"); do { n = getchar(); putchar(n);
4
by: thyagi | last post by:
a function which takes a string (pointer to char) as a parameter and returns the integer equivalent.str2int("1658") would return the value 1658 To improve your program change the function to accept...
10
by: puneetsardana88 | last post by:
I want to know how to access particular character of a String without using any pre defined functions(like charAt etc)? Since there are no pointers in java and the concept of character array being...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.