Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

character to integer conversion

Status
Not open for further replies.

aibelectronics

Member level 2
Joined
Aug 8, 2005
Messages
48
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,841
sdcc strtol

Hello! Can anybody explain how this c program would do this conversion?

char d, e[2];
.....
e[0]='0';
e[1]='1';
e[2]=d;

scale=atoi(e);
....

how would c convert d to an integer. are there some numbers representing letters in c?
thanks in advance!
 

charicters are integers anyway
that conform to the asci and later the ascii charicter set's
were originaly designed for tty or rtty comunications systems

each letter represents an integer or a byte value


https://www.asciitable.com/

classic lookup table examples you can print this out on a laser printer
or capture it


basicaly a byte and an integer are the same thing
as long as we are talking about conventions

some c assemblers have there own set of laws and rules

it is therefore best to study some example code and read all texts about
the 'c' compiler you choose

it pays to remember not all c compiler work the same way

the asci for the letter d in no ' ' is the same as any pre defined or cast char variable
the top line in your code example sets out a single charicter of 0-255 range {00-ff } and the other cast is for the charicter 'holder' array e[x]

so there is no real conversion only the atoi and it will

convert the charicters in e to integers
and simply add on what value the char d is from 00-FF
you have set it to

here is an example that should run with a tweak in c
it is ment for c++ however its all much of a muchness


:D

int atoi ( const char * string );
Convert string to integer.
Parses string interpreting its content as a number and returns an int value.

Parameters.

string
String representing an integer number. The number is considered until a non-numeric character is found (digits, and signs are considered valid numeric characters for this parameter as specified in format). The format used is:
[whitespaces][+|-][nnnnn]
(where whitespaces are any tab or space character and nnnnn may be any number of digits)
Return Value.
The converted integer value of the input string.
On overflow the result is undefined.
If an error occurs 0 is returned.

Portability.
Defined in ANSI-C.

/* atoi example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int i;
char szInput [256];
printf ("Enter a number: ");
gets ( szInput );
i = atoi (szInput);
printf ("Value entered is %d, and its double %d",i,i*2);
return 0;
}

Output:
Enter a number: 73
Value entered is 73, and its double 146


a good tip is to use a cheep or freeware sim for one of the older chips like z80
then you can experiment with many more examples

a good freeware compiler that is being built
is SDCC i have tried it it works and realy it is a nice compiler


the main differance between compiler is how variables like integers are defines in lengh
forinstance some compiler only let you have 0-255 as an integer
most use word lengh 0000 -ffff - 0 - 65535
and in this type to get 0-255 you would need to define the int as
short int x;
 

e[0]='0';
e[1]='1';

In general, you cannot assume those are ASCII conversions. Characters are represented by small integers corresponding to their values in the machine's character set, whatever that may be. Most systems use ASCII, but not all. If you know the program will be compiled for an ASCII system, then you are fine.

e[2]=d;

Your program may crash, because that writes beyond the end of the array.

scale=atoi(e);

Your program may crash again, because atoi() expects a null-terminated string.
 

hmm...very interesting. the author of the code didn't talk of his system crashing though :D i'll try it out practically and keep you posted.
anyway, i believe this would be relevant, the compiler used to compile it is turbo C so...
 

First of all [2] = d generates an error. it should be 'd' or 0x0d.
atoi won't converts any characters other that '0' to '9'. When ever it sees other character it returns what ever value it converted till then. means
e="012d" returns 12, e = "01d2" returns 1.

the problem of crashing may be there or may not be there. if e is a global variable it is initialized with 0, so it will not crash, if not it can crash.


Cheers
idlebrain
 

Yes, [2] = d is an syntax error, but that's not what aibelectronics wrote. ;)

e[2] = d is not a syntax error because e[2] and d are both chars. However, e[2] is beyond the end of the two-character array. Doesn't matter if it's declared global or local, it could still crash.

The C standard says that atoi() looks for the null-terminator.
First, it says:
atoi() is equivalent to (int)strtol(nptr, (char **)NULL, 10).
Then it says:
The strtol, strtoll, strtoul, and strtoull functions convert the initial portion of the string pointed to by nptr to long int, long long int, unsigned long int, and unsigned long long int representation, respectively. First, they decompose the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by the isspace function), a subject sequence resembling an integer represented in some radix determined by the value of base, and a final string of one or more unrecognized characters, including the terminating null character of the input string. Then, they attempt to convert the subject sequence to an integer, and return the result.
 

sorry there actually was an error on my part, d was set to:

d=getch;
...
e[2]=d;
...
atoi[e];

but i think the picture is becoming clearer now, thanks a lot:D
 

I hope the following

if you enter a character 0 to 9 the atoi will return a 2 digit number. 12 if you enter 2, 14 if you enter 4, 19 if you enter 9.

if you any other character from key board, the answer will be 1. As atoi terminates when an invalid characters (other than 0 to 9) appears in the string.

what your experiments says?

Cheers
idlebarin
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top