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.

C18. How to convert text "A" to its ASCII code, 65.

Status
Not open for further replies.

Francesco cembrola

Newbie level 6
Joined
Jul 22, 2006
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,362
Hi guys,
Can anyone help me with this.

Using C18.

I want to get the ASCII code of a character string like "A" (not a numerical string like "5").
I the above case the result would be an 8-bit interger with a value of 65.
I do I do that?

I have searched invane without success. I cannot find a function that does that.
Any idea?

Regards

Francesco
 

C compiler
function atoi from stdlib.h ?
 

I'm not really sure what you are trying to do.
Are you trying to assign the integer representation of a character into a variable?

Code:
unsigned char i;
i='A'; // i is assigned the decimal value 65
 

Hi.
Unfortunately that does not work!

If you type:


Code:
char str[]="8";
 i=atoi(str); 
printf("ASCII: %d\n\r",i);
output=7.



But if you type;

Code:
char str[]="A";

 i=atoi(str); 
printf("ASCII: %d\n\r",i);
Output=0.

atoi only converts numerical string to integer values.

Regards

Francesco
 
Last edited by a moderator:

So what you want is to convert the character 'A' to characters '6' '5' ?
 

Hi alexan_e,

Say I have:
char str[1]="A";

I want an interger that cointain the number 65 which is the ASCII code for "A".

Regards

Francesco
 

Re: C18. How to convert text "A" to its ASCII code, 65.

How about

Code:
unsigned char i;
char mystr[2];
i='A'; // i is assigned the decimal value 65

mystr[0]=(i/10)+48;  // equals '6'
mystr[1]=(i%10)+48;  // equals '5'

- - - Updated - - -

I want an interger that cointain the number 65 which is the ASCII code for "A".

You mean like the code in post #3?
i gets assigned the integer value 65 , is this what you want?
 
No alexan_e.

If uoy take a look at the ASCII code table you will find that the letter "A" has code 65, the letter "B" has code 66 and so on.

So if I my_char is "B" the result should be 66.

Regards
 

I'm still not sure what you want to do , it must be one of the two things I have shown.

Code:
unsigned char i;
i='A'; // i is assigned the decimal value 65
i='B'; // i is assigned the decimal value 66
i='C'; // i is assigned the decimal value 67
i='D'; // i is assigned the decimal value 68
i='E'; // i is assigned the decimal value 69
 

Re: C18. How to convert text "A" to its ASCII code, 65.

alexan_e
your code below almost works.

---------------------------
Code:
unsigned char i;
char mystr[1];
i='A'; // i is assigned the decimal value 65


mystr[0]=(i/10)+48;
mystr[1]=(i%10)+48; 
-------------------------
  
printf("ASCII: %d\n\r",mystr[1]); 
printf("ASCII: %d\n\r",mystr[0]);

mMy output is 53 and 54

Can you explain what are you doing?
regards

Francesco

- - - Updated - - -

Thank you for the hints. It made me think.

Solution found.:p

Code:
unsigned char i;
char mystr[1];
i='C'; // i is assigned the ASCII value 67

  
printf("ASCII: %d\n\r",i);

The output is 67 wich is the ASCII code of "C"

Regards

Francesco
 
Last edited by a moderator:

I guess the assignment part is easy to understand 'A' is the same as decimal 65 or 0x41 so if you assign any of these to a variable you get the same result.

so I assume the confusing part is

Code:
mystr[0]=(i/10)+48;
mystr[1]=(i%10)+48;

We have a two digit decimal number, first we want to get the left digit.
When you divide any integer number with another integer then the decimal are truncated so

65/10 =6.5 but the decimals are truncated so you get 6
75/10 =7.5 but the decimals are truncated so you get 7
...

To get the right side digit I use the modulo operator , it results to the remainder of the division

60%10 results to 0
61%10 results to 1
62%10 results to 2
...

so now that you have two integers assigned to the two characters all you need is to add decimal 48 to turn an integer 0-9 to its ascii representation '0' - '9'
 

Of course you are right concerning atoi.

You might just do an explicit cast:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* char tests */
 
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
/* char characters */
char c1, c2, c3;
 
/* pointers to strings */
char *ps1, *ps2, *ps3;
 
/* integer results */
int i1, i2, i3, i4;
 
c1 = 'A';
ps1 = "A";
ps2 = "abcABC";
ps3 = "123";
 
i1 = (int)'A'; // same as i1 = (int)c1; yields 65
i2 = atoi(ps1); // yields 0
c3 = ps2[4]; // points to 'B'
i3 = (int)c3; //yields 66
i4 = atoi(ps3); //yields 123
 
printf("%d %d %d %d", i1, i2, i3, i4); 
 
return 0;
}



which will yield an output like
65 0 66 123
 
Last edited by a moderator:

Re: C18. How to convert text &quot;A&quot; to its ASCII code, 65.

After coming across this thread, I felt I needed to clarify a few facts concerning C types.

I want to get the ASCII code of a character string like "A" (not a numerical string like "5").
I the above case the result would be an 8-bit interger with a value of 65.
I do I do that?

I have searched invane without success. I cannot find a function that does that.

The reason you are unable to find a function or routine which converts a character into an integer value, is due to the fact no conversion is necessary.

All four of the following C statements produce EXACTLY the same result:

Code:
unsigned char c1 = 'A';
unsigned char c2 = 0b01000001;
unsigned char c3 = 0x41;
unsigned char c4 = 65;

After the above statements are executed:

c1 = c2 = c3 = c4

There is NO difference between the values contained in all four variables, it is in fact the SAME value.

Alex touched on this point in Post #9.

How the contents of four variables is interpreted is solely depended on your C code which accesses the value contained in any of the four variables.



Code:
char str[1]="A";

I want an interger that cointain the number 65 which is the ASCII code for "A".

Simple.

Code:
unsigned int c1 = 'A';

The variable c1 now contains the value 65, or 0x41 or 0b01000001 or 'A', depending on how it is interpreted.

For example:
Code:
unsigned int c1 = 'A';
unsigned int c2 = 'B';
unsigned int c3;

c3 = c1 + c2;

The variable c3 now contains the value 131 or 0x83 or 0b010000011, once again depending on how you would like to interpret it.


You could achieve the same result by the following:

Code:
unsigned int c3;

c3 = 'A' + 'B';

The variable c3 now contains the value 131 or 0x83 or 0b010000011, once again depending on how you would like to interpret it.


Solution found.:p

Code:
unsigned char i;
char mystr[1];
i='C'; // i is assigned the ASCII value 67

  
printf("ASCII: %d\n\r",i);

The output is 67 wich is the ASCII code of "C"

Actually the printf() routine has absolutely no effect on the contents of variable i in your code above.

It simply interprets or formats the value by the Conversion Specification you provided, in this case %d.

The following is valid code and demonstrates my previous assertions:

Code:
unsigned char i;
i='C';            // i is assigned the ASCII value 67
 
printf("Signed Integer: %d, ASCII: %c, HEX: %X \n\r", i, i, i);

Typical Output:

Signed Integer: 67, ASCII: C, HEX: 43

Although exact results depend on compiler, the above output demonstrations how a single value can be formatted several ways depending only the Conversion Specifications provided leaving the actual value unchanged.



You may also find the routine sprintf() helpful if you need to format the contents of variables in specific ways and then store the results in a string buffer.


BigDog
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top