Extended ASCII characters to decimal conversion

Hi All,

Is there any way to convert extended ASCII charters(from the range 128-255) to its corresponding decimal values programmatically?

Brief:: standard ASCII charters ranging from 0-127 we can store it in simple character variable and we do the conversion like below example.

e.x:1. for Standard ASCII charter to decimal conversion
char myString [] = "+"; // input is '+' corresponding decimal value is 43.
printf("myString decimal val = %d, size = %d, %d\n",myString[0], sizeof(tmpBuffer));

O/p: myString decimal val = 43, size = 2

2) e.x: for Extended ASCII charter to decimal conversion
char myString [] = "á"; // input is 'á' corresponding decimal value is 160.
printf("myString decimal val = %d, size = %d, %d\n",myString[0], sizeof(tmpBuffer));

O/p: myString decimal val = -61, size = 3


in the above 2) e.x i am getting the decimal output value is -61 but actual is 160.

Can anyone help me out how can i get the o/p as exact decimal value?

Thanks in Advance.. :)
Last edited on
The problem you are experiencing is that char is implementation defined to be either signed or unsigned. Your implementation chose signed.

You can simply cast as unsigned first in order to get what you want:

1
2
3
4
int value_of_char( char c )
{
  return (unsigned char)c;
}

Now in code you can get á to print as 160:

 
  printf( "value of %c is %d\n", 'á', value_of_char( 'á' ) );


BTW, there is really no such thing as “extended ASCII” (even though it indeed used to be a phrase people used). You are working with Windows Code Page 437 (https://en.wikipedia.org/wiki/Code_page_437).

Hope this helps.
Hi,

Thanks for your reply.

Please refer the below link to know about Extended ASCII charters.

Link: "http://www.asciitable.com/"

Please see the below program as well:

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
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

int main(void)
{
    // Printing ASCII charcter in the range of 0 - 127
   char myString[] = "+";
   printf("myString = %c, decimal val = %d, size = %d\n",myString[0], myString[0], strlen(myString));
   
   // Printing the ASCII charcter in the range of 128 - 255
    char temp[] = "á";
    printf("temp = %c, decimal val = %d, size = %d\n",temp[0], temp[0], strlen(temp));
   
   // Solution to print the ASCII charcter in the range 128 - 255 is
   cout<<"Printing the Extended ASCII code = ";
   cout<<temp[0];
   cout<<temp[1];
   
   // Need solution to print the Extended ASCII charcter corresponding decimal value
   // I tried in the below way [Can anyone provide the solution for this?]
   printf("\nPrinting the Extended ASCII code Decimal value = %d",temp);

return 0;
}


myString = +, decimal val = 43, size = 1                                                                                                                                             
temp = �, decimal val = -61, size = 2                                                                                                                                                
Printing the Extended ASCII code = á                                                                                                                                                 
Printing the Extended ASCII code Decimal value = 649936765

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
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

int main(void)
{
    // Printing ASCII charcter in the range of 0 - 127
   char myString[] = "+";
   printf("myString = %c, decimal val = %d, size = %d\n",myString[0], myString[0], strlen(myString));
   
   // Printing the ASCII charcter in the range of 128 - 255
    char temp[] = "á";
    printf("temp = %c, decimal val = %d, size = %d\n",temp[0], temp[0], strlen(temp));
   
   // Solution to print the ASCII charcter in the range 128 - 255 is
   cout<<"Printing the Extended ASCII code = ";
   cout<<temp[0];
   cout<<temp[1];
   
   // Need solution to print the Extended ASCII charcter corresponding decimal value
   // I tried in the below way [Can anyone provide the solution for this?]
   printf("\nPrinting the Extended ASCII code Decimal value = %d",temp);
   // Actual expected o/p is "160" please refer the link for ASCII values: "http://www.asciitable.com/"
   
return 0;
}


myString = +, decimal val = 43, size = 1                                                                                                                                             
temp = �, decimal val = -61, size = 2                                                                                                                                                
Printing the Extended ASCII code = á                                                                                                                                                 
Printing the Extended ASCII code Decimal value = 649936765  // Expected o/p is: 160

printf("\nPrinting the Extended ASCII code Decimal value = %d",temp);

I think you meant
printf("\nPrinting the Extended ASCII code Decimal value = %d",(unsigned char)temp[0]);

temp itself is an array (which may, or may not, in this instance have decayed to a pointer).

Reiterate @Duthomas's comment: there is no unambiguous "Extended ASCII". Don't believe everything you read on the web! It's whatever characters your collating sequence decides to add. The output is 160 in my Windows console, but something completely different in cpp.sh.
Last edited on
@lastchance

Thanks for the reply.

May i know on which windows console your getting this? I mean Visual studio or any other IDE?


I mean the Windows command window.

I don't use, need or like IDEs.


Here's your code compiling and running in the command window:
https://i.imgur.com/PaDb005.jpg

and here's your code in my editor:
https://i.imgur.com/wV8EkWF.jpg

Now, who needs a bloated IDE?
Last edited on
Topic archived. No new replies allowed.