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

Please Comment on this Integer to String Code.

Hi All,
I wrote the following to convert an integer to its string
representation from base -32 to 32(32 since strtoul takes 32).
Used recursion so as not to crowd the logic.
Please Comment on this code.
Posting from google groups.hope it dont end up in many expert's kill
file.

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

char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";

void print(int i,int base)
{
if (i == 0)return;
if (i%base >= 0)
{
print(i / base, base);
printf("%c",charTable[i % base]);
} else {
print(i / base + 1, base);
printf("%c",charTable[i % base - base]);
}
}

void printBase(int i,int base)
{
if ((abs(base) <= 1) || (abs(base) > 32) || (i == 0))
{
printf("0\n");
return;
}

if ((base > 0) && (i < 0))
{
printf("-");
i = -i;
}
print(i, base);
printf("\n");
}

int main(void)
{
int i;
for(i = -55; i <= 55; i++)
{
printf("%3d = ", i);
printBase(i,-10);
}
return 0;
}

Thanks and Regards,
manoj.

Mar 9 '06 #1
7 1842

manoj1...@gmail.com wrote:
Hi All,
I wrote the following to convert an integer to its string
representation from base -32 to 32(32 since strtoul takes 32). One friend points out that it takes upto 36. Used recursion so as not to crowd the logic.
Please Comment on this code.
Posting from google groups.hope it dont end up in many expert's kill
file.

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

char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void print(int i,int base)
{
if (i == 0)return;
if (i%base >= 0)
{
print(i / base, base);
printf("%c",charTable[i % base]);
} else {
print(i / base + 1, base);
printf("%c",charTable[i % base - base]);
}
}

void printBase(int i,int base)
{
if ((abs(base) <= 1) || (abs(base) > 32) || (i == 0)) if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0)) {
printf("0\n");
return;
}

if ((base > 0) && (i < 0))
{
printf("-");
i = -i;
}
print(i, base);
printf("\n");
}

int main(void)
{
int i;
for(i = -55; i <= 55; i++)
{
printf("%3d = ", i);
printBase(i,-10);
}
return 0;
}

Thanks and Regards,
manoj.


Mar 9 '06 #2
ma*******@gmail.com writes:
Hi All,
I wrote the following to convert an integer to its string
representation from base -32 to 32(32 since strtoul takes 32).
(You've already corrected this to 36 in another article.)
Used recursion so as not to crowd the logic.
Please Comment on this code.
Posting from google groups.hope it dont end up in many expert's kill
file.
Have you checked the results of your program? They're completely
wrong. A in base -10 should be identical to -(A in base 10).
#include <stdio.h>
#include <stdlib.h>

char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";

void print(int i,int base)
{
if (i == 0)return;
if (i%base >= 0)
{
print(i / base, base);
printf("%c",charTable[i % base]);
} else {
print(i / base + 1, base);
printf("%c",charTable[i % base - base]);
}
print(i / base, base); in both cases, and charTable[abs(i % base)].
}

void printBase(int i,int base)
{
if ((abs(base) <= 1) || (abs(base) > 32) || (i == 0))
{
printf("0\n");
return;
}
I'm not certain you should print 0 for bad bases: I would print out
something that calls attention to the abuse of the function. In some
situations, I might use an assert() for this: for others, I'd probably
print something like "#ERR".

Also, you should really use something other than a magic number for
"32". You've already had to change this stuff once: what would've
happened if you'd forgotten to change the number above? If you use

(sizeof charTable) - 1

then you'll never have to mess with it again. (you could remove the "-
1" part if you use >= instead of >.)

if ((base > 0) && (i < 0))
{
printf("-");
i = -i;
}
Great. But what if base < 0 and i > 0?
I think you want (base > 0) == (i < 0). And the inside will need some
modification (remove the i = -i).
print(i, base);
printf("\n");
}

int main(void)
{
int i;
for(i = -55; i <= 55; i++)
{
printf("%3d = ", i);
printBase(i,-10);
}
return 0;
}

Thanks and Regards,
manoj.

Mar 9 '06 #3
Micah Cowan <mi***@cowan.name> writes:
[...]
Have you checked the results of your program? They're completely
wrong. A in base -10 should be identical to -(A in base 10).


I haven't looked at how the program actually behaves, but A in base
-10 is not the same as -(A in base 10).

For a 3-digit number in base +10, the place values are +100, +10, and +1.
For a 3-digit number in base -10, the place values are +100, -10, and +1
(because (-10)**2 is +100, not -100).

So, for example, 123 base 10 would be 283 in base -10.

(Negative bases are silly, of course.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 9 '06 #4
Keith Thompson <ks***@mib.org> writes:
Micah Cowan <mi***@cowan.name> writes:
[...]
Have you checked the results of your program? They're completely
wrong. A in base -10 should be identical to -(A in base 10).


I haven't looked at how the program actually behaves, but A in base
-10 is not the same as -(A in base 10).

For a 3-digit number in base +10, the place values are +100, +10, and +1.
For a 3-digit number in base -10, the place values are +100, -10, and +1
(because (-10)**2 is +100, not -100).

So, for example, 123 base 10 would be 283 in base -10.


Doh! Of course you are right. I /knew/ I should've listen to that
nagging feeling more closely.
Mar 9 '06 #5

Micah Cowan wrote:
I'm not certain you should print 0 for bad bases: I would print out
something that calls attention to the abuse of the function. In some
situations, I might use an assert() for this: for others, I'd probably
print something like "#ERR". I want to use this later as a function returning a string.But will keep
this in mind
Great. But what if base < 0 and i > 0?
I think you want (base > 0) == (i < 0). And the inside will need some
modification (remove the i = -i).

I have a doubt here.If i is INT_MIN, will i = -i be undefined?

Thanks and Regards,
manoj.

Mar 10 '06 #6
ma*******@gmail.com writes:
Micah Cowan wrote:
I'm not certain you should print 0 for bad bases: I would print out
something that calls attention to the abuse of the function. In some
situations, I might use an assert() for this: for others, I'd probably
print something like "#ERR".

I want to use this later as a function returning a string.But will keep
this in mind
Great. But what if base < 0 and i > 0?
I think you want (base > 0) == (i < 0). And the inside will need some
modification (remove the i = -i).

I have a doubt here.If i is INT_MIN, will i = -i be undefined?


On a two's complement system, yeah. But ignore most of what I wrote,
as I was trying to adapt it to my broken conception of how it was
supposed to work.

You can fix the i = -i thing by simply removing it, and adding the
condition to print():

if (base > 0 && i < 0)
{
print(-(i/base),base);
printf("%c",charTable[-(i % base)]);
}
else if (i%base >= 0)
{
...

However, this condition will be true at most once, but evaluated every
time, so you'd probably do better to move it to where you currently
have the i = -i, and put the other print() currently in printBase()
into an else clause.

HTH,
-Micah
Mar 10 '06 #7
Keith Thompson wrote:
Micah Cowan <mi***@cowan.name> writes:
[...]
Have you checked the results of your program? They're completely
wrong. A in base -10 should be identical to -(A in base 10).


I haven't looked at how the program actually behaves, but A in base
-10 is not the same as -(A in base 10).

For a 3-digit number in base +10, the place values are +100, +10, and +1.
For a 3-digit number in base -10, the place values are +100, -10, and +1
(because (-10)**2 is +100, not -100).

So, for example, 123 base 10 would be 283 in base -10.

(Negative bases are silly, of course.)


They may be silly, but they are completely sound mathematically. The
advantage to them is that you don't need a "-" sign to express numbers
over the entire integer range.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Mar 10 '06 #8

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

Similar topics

2
by: Sathyaish | last post by:
I am writing a program that will take a string that has a C source file and it would format all the multi-line comments like these: //Jingle bells, jingle bells, jingles all the way //O what...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
1
by: Mark Hollander | last post by:
Hi All, Could you please help me convert this code so that it will run in VB.NET Thank You Mark Hollander Private Type USER_INFO Name As String
3
by: Joe | last post by:
Hello All, I am developing a webform which creates ArrayLists of people's names and addresses (the values are retrieved from an xml file) and dynamically drops a user control onto the webform...
58
by: manoj1978 | last post by:
Hi All, I wrote the following to print an integer in its string representation for base -36 to 36. Please comment on this code. #include <iostream> #include <string> using std::abs; using...
5
by: Registered User | last post by:
I'm a newbie and have attempted to create a program to convert a string containing the representation of a number in a specified base to a decimal integer. Here's the code: #include <stdio.h> ...
8
by: Greg Lyles | last post by:
Hi all, I'm trying to develop an ASP.NET 2.0 website and am running into some real problems with what I thought would be a relatively simple thing to do. In a nutshell, I'm stuck on trying to...
19
by: yxq | last post by:
Hello, I want to get the string resource from DLL file, the code work well for Vista x86, but it will not work on Vista x64, why? can anyone help to view the code below? thank you very much. ...
2
by: BairnsRus | last post by:
Wonder if anyone can point me in the right direction? For part of my coursework I have been asked to create a BMI monitor which calculates height, weight and displays BMI for up to 5 records. So far...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.