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?
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 (unsignedchar)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).
#include <stdio.h>
#include <iostream>
#include <string.h>
usingnamespace 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
#include <stdio.h>
#include <iostream>
#include <string.h>
usingnamespace 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",(unsignedchar)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.