-2

Alright so pretty simple, I want to convert a letter to a number so that a = 0, b = 1, etc. Now I know I can do

number = letter + '0';

so when I input the letter 'a' it gives me the number 145. My question is, if I am to run this on a different computer or OS, would it still give me the same number 145 for when I input the letter 'a'?

Yitzak Hernandez
  • 355
  • 4
  • 23
  • It matters on whether you're using ASCII, or any other notation. – Box Box Box Box Feb 26 '16 at 01:56
  • 2
    In theory, no. In practice yes, simple because both ASCII and UTF-8 encode an `'a'` as 97 and `'0'` as 48, so `'a' + '0'` is 145. – user3386109 Feb 26 '16 at 01:57
  • If you convert ascii `a` to int it's 97 – Andreas DM Feb 26 '16 at 01:58
  • The C language specification does not stipulate any specific character encoding, it depends on your platform and compiler, which makes naive implementations of character-shifting (and `tolower`/`toupper`) potentially dangerous. – Dai Feb 26 '16 at 01:58
  • i don't get why you'd need 145, but `number = letter - 'a'` would give you `0` for `a`, regardless of encoding – blld Feb 26 '16 at 01:59
  • If you're interested in writing portable software, use standard solutions like `atoi` or `strtol`. – Jonathon Reinhart Feb 26 '16 at 02:01
  • @ RiGid You can either use the accepted answer from the duplicate question, assuming that your code will only run on systems that use the ASCII or UTF-8 character sets. Or to be pedantically correct, you can use the answer that I posted. Look for the answer with lots of yellow in it. – user3386109 Feb 26 '16 at 03:14
  • Something `number = strtol((char s[2]){letter,0}, 0, 36) - 10;` might work for you. (portable, since C99, no ASCII assumption) – chux - Reinstate Monica Feb 26 '16 at 03:16

3 Answers3

2

It depends on what character encoding you are using. If you're using the same encoding and compiler on both the computers, yes, it will be the same. But if you're using another encoding like EBCDIC on one computer and ASCII on another, you cannot guarantee them to be the same.


Also, you can use atoi.

If you do not want to use atoi, see: Converting Letters to Numbers in C

Community
  • 1
  • 1
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
0

It depends on what character encoding you are using. It is also important to note that if you use ASCII the value will fit in a byte. If you are using UTF-8 for example, the value wont fit a byte but you will require two bytes (int16) at least.

Now, lets assume you are making sure you use one specific character encoding then, the value will be the same no matter the system.

alexandergs
  • 182
  • 2
  • 12
  • 1
    The letters `a-z` and `A-Z`, and the digits `0-9`, are all encoded as a single byte in both ASCII and UTF-8. And they have the same value in ASCII and UTF-8. In fact, every 7-bit ASCII value is encoded as a single byte with the same value in UTF-8. See [this wikipedia article](https://en.wikipedia.org/wiki/UTF-8). – user3386109 Feb 26 '16 at 02:15
-1

Yes, the number used to represent a is defined in the American Standard Code for Information Interchange. This is the standard that C compilers use by default, so on all other OSs you will get the same result.

nom
  • 256
  • 3
  • 16
  • 1
    C does not use ASCII "by default" - though a particular compiler and platform might. The C language specification does not require or mandate any particular encoding, and programs should not depend on that assumption either (thinking of ASCII vs EBDDIC) – Dai Feb 26 '16 at 02:00
  • @Dai, But most C compilers do right? – nom Feb 26 '16 at 02:01
  • 2
    yes, but "most C compilers" also used to use a 4-byte pointer size (e.g. `int ptr = &foo`) - but any programs that used that assumption will break when run on modern 64-bit platforms. If something works because it depends on assumptions it's "wrong". – Dai Feb 26 '16 at 02:02