473,804 Members | 2,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with copying/editing strings

Hello,

I am attempting to write a terminal interface program using PowerPC
Assembly/C, where I am using an integer-to-ASCII conversion algorithm.
Unfortunately, I have run into some difficulties that seem to deal
with the syntax/character types that am I using. Below is a segment of
my code which is intended to convert an integer to an array of ASCII
digits:
char * inttoascii(int val)
{
int i, temp1, temp2;
i = 0;
char * str;
temp1 = 1;
if(val < 0)
{
str[i] = '-';
val = val*-1;
i++;
}
while(val > temp1)
{
temp1 = temp1*10;
}
while(temp1 >= 10)
{
temp1 = temp1/10;
temp2 = val/temp1;
str[i] = temp2+48; *****
i++;
}
str[i] = '\0';
return(str);
}
The ***** line is where I'm having my problems; presumably adding 48
to a string element (i.e., a character) should convert that value to
its ASCII equivalent so it can be displayed on my terminal screen, but
when debugging the problem line doesn't seem to be doing anything at
all; presumably this is an issue of syntax that you can help me with.
I have also tried '0' and '0x30' in place of 48.

Another problem I have is with copying strings into other strings, for
example I have a help message that says:

strcopy(output, "+ for add, - for subtract, ? for help");

where strcopy is the following function:
void strcopy(char b[], char a[])
{
int i;

for (i = 0; a[i] != '\0'; i++)
b[i] = a[i];
b[i] = '\0';
}


When I try to do this conversion it gives me a machine check exception
in the simulator I'm using (SingleStep) something about error, memory
access in 0x0000000D) which it doesn't do if I use a normal strcopy
syntax where a and b are both declared as character pointers e.g. char
*a, char *b. Note that I do not have the standard library, standard
i/o or any other such libraries at my disposal for this program.

If anyone wants to see more of the code or can help me out, please
post a message here or e-mail me. Thanks!

Matt Lafer
University of Michigan
Nov 14 '05 #1
4 1412
Lafer <la****@yahoo.c om> wrote:
I am attempting to write a terminal interface program using PowerPC
Assembly/C, where I am using an integer-to-ASCII conversion algorithm.
Unfortunately, I have run into some difficulties that seem to deal
with the syntax/character types that am I using. Below is a segment of
my code which is intended to convert an integer to an array of ASCII
digits:
> char * inttoascii(int val)
> {
> int i, temp1, temp2;
> i = 0;
> char * str;
> temp1 = 1;
> if(val < 0)
> {
> str[i] = '-';
You have some bad bug here: str has never been initialized, so it
probably points to some random place in memory. You have to allocate
enough memory and assign that to str before you can start to use it.
(There was another thread today about the question how long the
string can become in the worst case, i.e. how much memory you need
to allocate to be on the safe side.)
> val = val*-1;
> i++;
> }
> while(val > temp1)
> {
> temp1 = temp1*10;
> }
Lets assume that val is e.g. is 17. Then temp1 is now 100.
> while(temp1 >= 10)
> {
> temp1 = temp1/10;
First time round temp1 is 10, thesecond time it's 1.
> temp2 = val/temp1;
The first time round temp2 will be 1, the second time it's going
to be 17, which you probably don't want. You need another line
where you do

val %= temp1;

so that you get temp2 set to 7 on the second time through the loop.
> str[i] = temp2+48; *****
Making sure that this works also with non-ASCII representations is
extremely simple and doesn't cost you anything but just makes your
program easier to read. Use

str[ i ] = temp2 + '0'.

(of course only after having assigned enough memory to str).
> i++;
> }
> str[i] = '\0';
> return(str);
Now here's another possible pitfall. If you should make str an
automatic array (instead of allocating memory for the pointer or
making it a static array) you would return a value that goes
out of scope the moment you leave the function and thus can't be
used by the caller.
> } The ***** line is where I'm having my problems; presumably adding 48
to a string element (i.e., a character) should convert that value to
its ASCII equivalent so it can be displayed on my terminal screen, but
when debugging the problem line doesn't seem to be doing anything at
all; presumably this is an issue of syntax that you can help me with.
I have also tried '0' and '0x30' in place of 48. Another problem I have is with copying strings into other strings, for
example I have a help message that says: strcopy(output, "+ for add, - for subtract, ? for help"); where strcopy is the following function: > void strcopy(char b[], char a[])
> {
> int i;
>
> for (i = 0; a[i] != '\0'; i++)
> b[i] = a[i];
> b[i] = '\0';
> }

When I try to do this conversion it gives me a machine check exception
in the simulator I'm using (SingleStep) something about error, memory
access in 0x0000000D) which it doesn't do if I use a normal strcopy
syntax where a and b are both declared as character pointers e.g. char
*a, char *b. Note that I do not have the standard library, standard
i/o or any other such libraries at my disposal for this program.


Looks a lot as if you pass an unitialized pointer to that function.
If you do something like

char *output;
strcopy(output, "+ for add, - for subtract, ? for help");

you're in for such an error because 'output' doesn't point to any
memory you own. It might just by chance point to memory location
0x0D (but don't count on that). BTW, why don't you use the classic
construct

void strcopy( char *b, const char *a )
{
while ( *b++ = *a++ )
/* empty */ ;
}

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2
Je***********@p hysik.fu-berlin.de writes:
Lafer <la****@yahoo.c om> wrote:
I am attempting to write a terminal interface program using PowerPC
Assembly/C, where I am using an integer-to-ASCII conversion algorithm.

str[i] = temp2+48;


Making sure that this works also with non-ASCII representations is
extremely simple and doesn't cost you anything but just makes your
program easier to read. Use

str[ i ] = temp2 + '0'.


Note that the OP says he wants to write an integer-to-ASCII converter,
as opposed to an integer-to-text-in-the-host-encoding converter. If he
really means that, 48 would in fact be correct and '0' wrong on
non-ASCII systems.

I don't find it obvious what the OP means. "Terminal interface program"
could refer to a program which is supposed to run on a non-ASCII system,
but interfaces with an ASCII terminal.
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #3
Martin Dickopp <ex************ ****@zero-based.org> wrote:
Je***********@p hysik.fu-berlin.de writes:
Lafer <la****@yahoo.c om> wrote:
I am attempting to write a terminal interface program using PowerPC
Assembly/C, where I am using an integer-to-ASCII conversion algorithm.

str[i] = temp2+48;


Making sure that this works also with non-ASCII representations is
extremely simple and doesn't cost you anything but just makes your
program easier to read. Use

str[ i ] = temp2 + '0'.

Note that the OP says he wants to write an integer-to-ASCII converter,
as opposed to an integer-to-text-in-the-host-encoding converter. If he
really means that, 48 would in fact be correct and '0' wrong on
non-ASCII systems. I don't find it obvious what the OP means. "Terminal interface program"
could refer to a program which is supposed to run on a non-ASCII system,
but interfaces with an ASCII terminal.


Yupp, of course you're right, I just carelessly assumed that the OP
meant int-to-string conversion when s/he wrote integer-to-ASCII.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4
Martin Dickopp <ex************ ****@zero-based.org> scribbled the following:
Je***********@p hysik.fu-berlin.de writes:
Lafer <la****@yahoo.c om> wrote:
I am attempting to write a terminal interface program using PowerPC
Assembly/C, where I am using an integer-to-ASCII conversion algorithm.

str[i] = temp2+48;
Making sure that this works also with non-ASCII representations is
extremely simple and doesn't cost you anything but just makes your
program easier to read. Use

str[ i ] = temp2 + '0'.

Note that the OP says he wants to write an integer-to-ASCII converter,
as opposed to an integer-to-text-in-the-host-encoding converter. If he
really means that, 48 would in fact be correct and '0' wrong on
non-ASCII systems. I don't find it obvious what the OP means. "Terminal interface program"
could refer to a program which is supposed to run on a non-ASCII system,
but interfaces with an ASCII terminal.


He said "integer-to-ASCII" but who's to say he didn't mean "integer-
to-whatever-happens-to-be-this-platform's-encoding"? The latter would
be more useful in most common situations. Getting these muddled up can
be the result of an "all the world's ASCII" viewpoint.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Immanuel Kant but Genghis Khan."
- The Official Graffitist's Handbook
Nov 14 '05 #5

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

Similar topics

4
2406
by: Dan Weeb | last post by:
Hi All, I have struggled through this far with help from many of you so thanks. I am stuck again. I am really new to this so don't be harsh :-) There are a few problems. You can run the script here http://www.pbrown.com/research/test1.php to see the formatting issues Formatting Issues
5
9636
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing all the contents of f2 will be created and thrown away. In the case of two StringIO objects, this means there's a point when the contents is held in memory three times.
0
1768
by: google account | last post by:
*sigh* I have done all that I know how.... I am using fedora core1, I believe. I have updated the MySQL version to 4.0.17-0. I am trying this: rpm -i MySQL-python2.1-0.9.2-1.i386.rpm
0
2026
by: Richard Taylor | last post by:
User-Agent: OSXnews 2.07 Xref: number1.nntp.dca.giganews.com comp.lang.python:437315 Hi I am trying to use py2app (http://undefined.org/python/) to package a gnome-python application called gramps (http://www.gramps-project.org) for MAC OS X.
3
4276
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found several examples on the web. They all seem to have problems, though that I've been unable to resolve. The most promising example I have found is at:
1
2774
by: Dan Bass | last post by:
I'm looking to develop a listbox with in-place editing where as each item is selected, it grows to fit in all the text boxes. When the item is deselected, it shrinks back to its original size. The editing bit is not a problem, but I can't get the selected item to resize, as the listbox does for the "Add/Remove programs". The initial thought I had was to have a handler for the MeasureItem event, then have the condition of whether the...
0
1105
by: Jeff Connelly | last post by:
Using Visual C# .NET 1.1. I'm editing a .resx file of strings, using the "Data" view (not the "XML" view). I can't find any way of moving or inserting rows in the middle (I assume you could do this in XML view.) You can reorder the rows by clicking the column headers, but otherwise you don't seem to have any control over where the strings go. Anything I'm missing?
18
3806
by: TORQUE | last post by:
Hi, Im wondering if anyone can help me with a problem. I have a form with more than 50 unbound fields. Some of the fields will be blank from time to time. This seems to be where im having trouble. I have tried keeping some of the fields bound and when I use the save button it has been saving as 2 different records. This is unacceptable. This is what I have, can anyone help me out with this?
409
11197
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are objects that are more than 4GB long. The problem is, when you have in thousands of places
0
9571
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
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 we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.