6 Replies - 223 Views - Last Post: 26 September 2019 - 05:51 AM Rate Topic: -----

#1 preshma25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 19

Extended ASCII charters to decimal conversion

Posted 24 September 2019 - 11:48 PM

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..

This post has been edited by Skydiver: 25 September 2019 - 03:59 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: Extended ASCII charters to decimal conversion

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15313
  • View blog
  • Posts: 61,406
  • Joined: 12-June 08

Re: Extended ASCII charters to decimal conversion

Posted 25 September 2019 - 01:18 AM

What have you tried?
Was This Post Helpful? 0
  • +
  • -

#3 preshma25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 19

Re: Extended ASCII charters to decimal conversion

Posted 25 September 2019 - 02:52 AM

Hi,

My aim was to print the ASCII characters corresponding decimal value. If i am using extended ASCII values then i am unable to get the expected output.
Was This Post Helpful? 0
  • +
  • -

#4 preshma25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 19

Re: Extended ASCII charters to decimal conversion

Posted 25 September 2019 - 03:29 AM

Please find the attachment of my code.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7100
  • View blog
  • Posts: 24,122
  • Joined: 05-May 12

Re: Extended ASCII charters to decimal conversion

Posted 25 September 2019 - 03:58 AM

What he is asking is what else have you tried beyond what you have posted in your code. (Please learn to use code tags, by the way.)

Have you considered what other types are supported by C?
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2754
  • View blog
  • Posts: 4,414
  • Joined: 21-June 11

Re: Extended ASCII charters to decimal conversion

Posted 25 September 2019 - 04:37 AM

You're not using "extended ASCII", you're using UTF-8. Using UTF-8, the string "" will contain the bytes 195 and 161 (plus 0 as the terminator). Since `char` is a signed type on your system, the 195 comes out as -61. If you cast it to unsigned char, you'd get 195 instead.

Note that that's only one half of the character as that character is represented by two bytes. So you'd need to print both myString[0] and myString[1] to get both bytes of the character. Also note that UTF-8 is not a fixed-width encoding, so other characters might take up less or more than 2 bytes. So if you want to print all bytes of an arbitrary character, you'd either need to implement the UTF-8 logic to find out where the character ends or use a Unicode library.
Was This Post Helpful? 1
  • +
  • -

#7 preshma25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 19

Re: Extended ASCII charters to decimal conversion

Posted 26 September 2019 - 05:51 AM

Hi sepp2k,

Thanks for the information this will short out my issue.

This post has been edited by Skydiver: 26 September 2019 - 12:44 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1