[Question]
  [
# Challenge
In this challenge you have to take a number as input and output the corresponding letter of the alphabet, and vice versa. (1 <=> A, 2 <=> B) etc.
```
1 -> A
2 -> B
...
26 -> Z
A -> 1
B -> 2
...
Z -> 26
```
# Rules
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest code in bytes wins.
* The input will only consist of either an uppercase letter from `A` to `Z` or an integer from `1` to `26` inclusive.
* Trailing whitespaces (space and newline) are allowed.
  
[Answer]
# Pure Bash, 51
Most of the rest of the answers use some sort of conditional. This one dispenses with conditionals entirely, and instead treats the input as a base-36 number which indexes into an appropriately constructed bash-brace-expansion array:
```
a=(_ {A..I} {1..26} {J..Z} {A..Z})
echo ${a[36#$1]}
```
[Ideone.](https://ideone.com/zFW3J7)
[Answer]
# Erlang, 26 bytes
```
f([X])->X-64;f(X)->[X+64].
```
One of the few times where Erlang's string behavior is useful.
[Answer]
## Actually, 7 bytes
```
ú' +ûEí
```
[Try it online!](http://actually.tryitonline.net/#code=w7onICvDu0XDrQ&input=IkUi)
Explanation:
```
ú' +ûEí
ú' +     lowercase English alphabet, prepend space
    û    uppercase
     E   element (pushes the nth letter if input is an integer, leaves stack alone otherwise)
      í  index (pushes index of input if input is a string, leaves stack alone otherwise)
```
If lowercase is acceptable, this is 6 bytes:
```
ú' +Eí
```
[Try it online!](http://actually.tryitonline.net/#code=w7onICtFw60&input=ImUi)
[Answer]
# Python 2, 38 bytes
```
lambda x:x>''and 64^ord(x)or chr(64^x)
```
Test it on [Ideone](http://ideone.com/E9pcsJ).
[Answer]
# Python 3, 43 bytes
```
lambda x:x!=str(x)and chr(64|x)or ord(x)^64
```
The interesting thing about this solution is that it incorporates all the senses of OR, bitwise OR `|`, logical OR `or`, bitwise XOR `^` and logical XOR `!=` ...
[Answer]
# [2sable](http://github.com/Adriandmen/2sable), ~~9~~ 8 bytes
Code:
```
.bAu¹kr,
```
Explanation:
```
.b        # Convert 1 -> A, 2 -> B, etc.
  A       # Push the alphabet.
   u      # Convert it to uppercase.
    ¹k    # Find the index of the letter in the alphabet.
      r   # Reverse the stack.
       ,  # Pop and print with a newline.
```
Uses the **CP-1252** encoding. [Try it online!](http://2sable.tryitonline.net/#code=LmJBdcK5a3Is&input=Rw).
[Answer]
# Ruby, ~~47 39 + `n` flag = 40 bytes~~ ~~33~~ ~~34~~ 31 bytes
Anonymous function. Uses an exception handling trick like in @KarlNapf's [Python solution](https://codegolf.stackexchange.com/a/89968/52194).
-3 bytes from @manatwork
[Try it online](https://repl.it/Cn5s)
```
->i{(64+i).chr rescue i.ord-64}
```
Original full program version with the `n` flag for 40 bytes and reads from STDIN:
```
puts$_!~/\d/?$_.ord-64:(64+$_.to_i).chr
```
[Answer]
# Cheddar, ~~34~~ 32 bytes
*Saved 2 bytes thanks to @LeakyNun*
```
n->"%s"%n==n?n.ord()-64:@"(n+64)
```
I wish there was shorter way to check if string or number.
[Try it online!](http://cheddar.tryitonline.net/#code=bi0-IiVzIiVuPT1uP24ub3JkKCktNjQ6QCIobis2NCk&input=MQ) or 
[Test Suite](http://cheddar.tryitonline.net/#code=bGV0IGY9IG4tPiIlcyIlbj09bj9uLm9yZCgpLTY0OkAiKG4rNjQpOwpwcmludCBmKDEpOwpwcmludCBmKDI2KTsKcHJpbnQgZigiQSIpOwpwcmludCBmKCJaIik7&input=)
## Explanation
```
n ->                // func with arg `n`
    "%s"%n==n ?     // if n is string... (see below)
       n.ord() - 64  // return code point - 64
    :               // else...
    @"(n+64)         // chr(n+64)
```
`"%s"%n==n` checks if it is a string in a simple way. `"%s"` is a string format, I can format with `%` e.g. `"a %s c" % "b"` is equal to `"a b c"`. `%s` specifies it is a string, if a digit is passed it'll remain as `%s`.
[Answer]
# Mathematica ~~54~~ 41 Bytes
With an absolutely clever suggestion from LegionMammal978 that saves 13 bytes.
```
If[#>0,FromLetterNumber,,LetterNumber]@#&
```
`If[#>0,FromLetterNumber,,LetterNumber]` serves the sole purpose of deciding whether to apply `FromLetterNumber` or `LetterNumber` to the input.
`#>0` will be satisfied if the input, `#`, is a number, in which case `FromLetterNumber`will be selected.
However `#>0` will be neither true nor false if `#` is a letter, and `LetterNumber` will be selected instead.
---
```
If[#>0,FromLetterNumber,,LetterNumber]@#&["d"]
```
4
---
```
If[#>0,FromLetterNumber,,LetterNumber]@#&[4]
```
d
---
In Mathematica, `FromLetterNumber` and `LetterNumber` will also work with other alphabets. This requires only a few more bytes.
```
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[4, "Greek"]
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[4, "Russian"]
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[4, "Romanian"]
```
δ  
г  
b
```
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[δ, "Greek"]
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[г, "Russian"]
If[# > 0, FromLetterNumber, , LetterNumber][#, #2] &[b, "Romanian"]
```
4  
4  
4
[Answer]
## Haskell, 54 bytes
```
f s|s<"A"=[['@'..]!!read s]|1<2=show$fromEnum(s!!0)-64
```
Usage example: `map f ["1","26","A","Z"]` -> `["A","Z","1","26"]`.
Haskell's strict type system is a real pain here. Additionally all the short char <-> int functions like `chr` and `ord` need an import, so I have to do it by hand. For the letter -> int case, for example I need to convert `String` -> `Char` (via `!!0`) -> `Integer` (via `fromEnum`) -> `String` (via `show`).
[Answer]
# C, 55 bytes
```
i;f(char*s){i=atol(s);printf(i?"%c":"%d",64^(i?i:*s));}
```
[Answer]
# [Perl 6](http://perl6.org), 25 bytes
```
{+$_??chr $_+64!!.ord-64}
```
## Explanation:
```
# bare block lambda with implicit parameter of ｢$_｣
{
    +$_         # is the input numeric
  ??
    chr $_ + 64 # if it is add 64 and get the character
  !!
    $_.ord - 64 # otherwise get the ordinal and subtract 64
}
```
## Example:
```
say ('A'..'Z').map: {+$_??chr $_+64!!.ord-64}
# (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)
say (1..26).map: {+$_??chr $_+64!!.ord-64}
# (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
```
[Answer]
# C#, 32 bytes
```
n=>(n^=64)>26?(object)(char)n:n;
```
Casts to `Func<int, object>`.
Input: `char` implicitely converts to `int` so can be called with `int` (1-26) or `char` ('A'-Z').
Output: Either a `char` or `int`.
[Answer]
## PHP, 49 41 40 bytes
```
<?=+($i=$argv[1])?chr($i+64):ord($i)-64;
```
I do not think there is a good alternative to `is_numeric` right?
This is executed from command line (`$argv[1]` is the first variable given)
## Thanks to:
@insertusernamehere: Golfed 8 bytes. Replacing `is_numeric($i=$argv[1])` with `0<($i=$argv[1])`.This works because `(int)"randomLetter" == 0`.
@manatwork: Reduced with 1 byte. Replace `0<` with `+`. What happens in this case is that the + signal will cast the "Z" (or whatever letter) to an 0. This will result in false. Therefor any letter is always false and a number is always true.
[Answer]
# Excel, 33 bytes
```
=IFERROR(CHAR(A1+64),CODE(A1)-64)
```
[Answer]
# Python 2, 61 bytes
```
i=raw_input()
try:o=chr(int(i)+64)
except:o=ord(i)-64
print o
```
Yes I could switch to Python 3 for `input`
[Answer]
## PowerShell v2+, 42 bytes
```
param($n)([char](64+$n),(+$n-64))[$n-ge65]
```
Takes input `$n` (as an integer or an explicit char) and uses a pseudo-ternary to choose between two elements of an array. The conditional is `$n-ge65` (i.e., is the input ASCII `A` or greater). If so, we simply cast the input as an int and subtract `64`. Otherwise, we add `64` to the input integer, and cast it as a `[char]`. In either case, the result is left on the pipeline and printing is implicit.
## Examples
```
PS C:\Tools\Scripts\golfing> ([char[]](65..90)|%{.\alphabet-to-number.ps1 $_})-join','
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
PS C:\Tools\Scripts\golfing> (1..26|%{.\alphabet-to-number.ps1 $_})-join','
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
```
[Answer]
# [Befunge-98](https://github.com/catseye/Funge-98/blob/master/doc/funge98.markdown)\*, 19 bytes
```
&:39*\`'@\j;+,@;-.@
```
Because the question said you'll receive a `1-26` or an `A-Z` I assumed this meant the number 26 or the character A-Z. Most interprets struggle with entering alt-codes, so it is easier to use `&` and enter values like 26 for 26 or 90 for 'Z', as opposed to `~`.
## Pseudo-code
```
int c = get stdin
push the value of 27
bool is_number =  27 > c
push the value of `@` (64)
if is_number == 1
   jump to adding 64 to c //putting it the ASCII range
   print as ASCII
   end
else
   jump to subtracting 64 from c //putting it in the numerical range
   print as number
   end
```
Test it out (on Windows) [here!](https://github.com/tngreene/BefungeSharp)
\*This is technically Unefunge-98 because it only uses 1 dimension, but that name might be unfamiliar.
[Answer]
## [Befunge 93](https://esolangs.org/wiki/Befunge), 144 90 66 54 36 19 bytes
Not 100% sure if this is allowed, but if you are allowed to type A as 65, B as 66, etc., then (for [my] convenience's sake):
```
&:"@"`"@"\#. #-_+,@
```
Otherwise, at 36 bytes:
```
~:0\"A"-`#v_88*-.@
**~28*++,@>68*-52
```
(Thanks to tngreene for the suggestions!)
```
~:0\567+*-`#v_88*-.>$28*+,@
52**\28*++,@>~:0`!#^_\68*-
```
(Thanks to Sp3000 for saving 12 bytes by rearranging!)
```
~:0\567+*-`#v_88*-.>$82*+,@
            >~:0`!#^_\68*-52**\28*++,@
v                   >$28*+,@
             >~:0`!#^_\68*-52**\28*++,@
>~:0\567+*-`#^_88*-.@
v                    >$28*+,@
~           >11g~:0`!|
1                    >\68*-52**\28*++,@
1
p           
>011g567+*-`|
            >11g88*-.@
```
Ungolfed:
```
v                       >$ 28* + , @
                 >~:0 `!|
                        >\ 68* - 52* * \ 28* + + , @
>~:0\ 5 67+ * - `|
                 >88* - . @
```
This is my first working Befunge program ever, and I feel the need to golf this further. Any help would be greatly appreciated. 
You can test Befunge code [here](http://www.quirkster.com/iano/js/befunge.html). 
[Answer]
# Brainfuck, 445 Characters
More a proof of concept than a golfed code.
Requires Unsigned, Non-wrapping Brainfuck.
```
,[>+>+<<-]>[<+>-]>>++[->++++++<]>[-<<<+++++>>>]<<<<[->-<]>[,<++++[->------------<]++++[->>------------<<][-<<++++++++++>>]>[-<+>]>[-<<++++++++++>>]>++[->++++++<]>+[-<+++++>]<-[-<<<+>>>]<<<.>]>[[-<+<+>>]>++[->++++++<]>+[-<+++++>]<-[-<<->>]<<[->+>+<<]>>>++++++++++<+[>[->+>+<<]>[-<<-[>]>>>[<[-<->]<[>]>>[[-]>>+<]>-<]<<]>>>+<<[-<<+>>]<<<]>>>>>[-<<<<<+>>>>>]<<<<<-[->+>+<<]>[-<++++++++++>]<[-<->]++++[-<++++++++++++>]++++[->>++++++++++++<<]>>.<<<.>]
```
With Comments
```
,[>+>+<<-] Firstly Duplicate it across two buffers
>[<+>-] Move the second buffer back to the first buffer
>>++[->++++++<]>[-<<<+++++>>>] Establish 60 in the second buffer
<<<<
Compare Buffers 1 and 2
[->-<]
>
[ If there's still data in buffer 2
, Write the value in the units column to buffer two
<
++++
[->------------<] Subtract 12 from the units buffer
++++
[->>------------<<] Subtract 12 from the tens buffer
[-<<++++++++++>>] Multiply buffer three by ten into buffer 1
>
[-<+>] Add the units
>
[-<<++++++++++>>] Add the tens
>++ Add 65 to the buffer
[->++++++<]>+
[-<+++++>]
<- Actually we need 64 because A is 1
[-<<<+>>>] Add 64 to the first buffer
<<<
. Print the new letter
> Move to blank buffer
]
>
[ Otherwise we're a letter
[-<+<+>>] Copy it back over the first two buffers
>++ Write 64 to the buffer
[->++++++<]>+
[-<+++++>]
<-
[-<<->>] Subtract 64 from the letter
<<[->+>+<<]
>>>++++++++++< Copy pasted Division step x = current buffer y = 10 rest of the buffers are conveniently blank
+
[>[->+>+<<]>[-<<-[>]>>>[<[-<->]<[>]>>[[-]>>+<]>-<]<<]>>>+<<[-<<+>>]<<<]>>>>>[-<<<<<+>>>>>]<<<<<
-
[->+>+<<]
>[-<++++++++++>]
<[-<->]
++++
[-<++++++++++++>]
++++
[->>++++++++++++<<]
>>.<<<.>
] 
```
[Answer]
# Java, ~~104~~ ~~98~~ ~~97~~ ~~83~~ ~~54~~ ~~53~~ ~~51~~ ~~50~~ 30 bytes
`x->(x^=64)>64?(char)x+"":x+"";`
**Test Program**:
```
IntFunction<String> f = x -> (x ^= 64) > 64 ? (char) x + "" : x + "";
out.println(f.apply('A')); // 1
out.println(f.apply('Z')); // 26
out.println((f.apply(1))); // A
out.println((f.apply(26))); //Z
```
[Answer]
# [Forth (gforth)](http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/), 67 bytes
```
: f 2dup s>number? if d>s 64 + emit else 2drop d>s c@ 64 - . then ;
```
[Try it online!](https://tio.run/##hY/dTgIxEIXveYrjcoHGXSIbgomYVV7DhYvSnUKTbqdpuyK@PLaAwTt7M3/nzDdV7OO@2qkcTqcXKNTd4BAaO/Rb8m/QCl0TsJjjEdTrCDKBksizOw/ke55VmCLuyWKZdrjLjnh0hJzUaSy2AcEJSQHTAqgaFIklPZZYw3ltI7R1Q8S9IRUrYfTOUpd6FxdYIejvBH4or/KiaorfnIeYvUmkSig2hg/JvD3C0sFoS6NR/YwZ2o43aPUGT3gdYxwwbtKxrWF2G6S3Rk6hPPdJHRn1ooRk@0k@n9fRV26GmJi7hBYhXIvcVoOVUbMdTVpMVjeWEx3k3TnMbrS/qFW2f5RpF3uC3AsvZCSfPx@pd@yFP/5HPf0A "Forth (gforth) – Try It Online")
Input is passed as a string to the function
### Code Explanation
```
: f             \ start a new word definition
  2dup          \ make a copy of the input string
  s>number?     \ attempt to convert to a number in base 10
  if            \ check if successful
    d>s         \ drop a number (don't need double-precision)
    64 + emit   \ add to 64 and output the ascii letter at that value
  else          \ if not successful (input was a letter)
    2drop d>s   \ get rid of garbage number conversion result and string-length
    c@ 64 - .   \ get ascii value of char, subtract 64 and output 
  then          \ end the if block
;               \ end the word definition
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
i@ịe?ØA
```
[Try it online!](https://tio.run/##y0rNyan8/z/T4eHu7lT7wzMc/7sdbn/UtCby/39DHQVTHQUjEDbTUVByVAISHiAiEEREKQEA "Jelly – Try It Online")
## How it works
```
i@ịe?ØA - Main link. Takes an input A on the left
     ØA - Set the right argument to "ABC...XYZ"
    ?   - If statement:
   e    -   Condition: left in right; is A in the alphabet?
i@      -   If so: return the 1-index of A in the alphabet
  ị     -   Else: yield the A'th element of the alphabet
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~11~~ 10 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
¤?UdI:InUc
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=pD9VZEk6SW5VYw&input=WzgsIkgiXS1tUw)
```
¤?UdI:InUc     :Implicit input of integer or string U
¤              :Convert to binary string if integer (always truthy)
¤              :Slice off first 2 character if string (always falsey)
 ?             :If truthy
  Ud           :  Get character at codepoint U
    I          :  After first adding 64
     :         :Else
      In       :  Subtract 64 from
        Uc     :  Codepoint of U
```
[Answer]
# q, 29 bytes
```
{@[1+.Q.A?;x;{.Q.A y-1}[;x]]}
```
Find the index at which our letter occurs in the built-in list of uppercase letters
If it errors, index into the same list with the input instead
Adding and taking away ones as q indexing starts at 0, not 1
[Answer]
# [><> (Fish)](https://esolangs.org/wiki/Fish), ~~[27](https://codegolf.stackexchange.com/revisions/259112/1)~~ ~~[24](https://codegolf.stackexchange.com/revisions/259112/2)~~ ~~[21](https://codegolf.stackexchange.com/revisions/259112/3)~~ 19 bytes
```
<o+v?)0-}:"@":
n-$<
```
[Try it with a number test case](https://mousetail.github.io/Fish/#eyJ0ZXh0IjoiPG8rdj8pMC19OlwiQFwiOlxubi0kPCIsImlucHV0IjoiIiwic3RhY2siOiIyNiIsInN0YWNrX2Zvcm1hdCI6Im51bWJlcnMiLCJpbnB1dF9mb3JtYXQiOiJjaGFycyJ9)
[Try it with a letter test case](https://mousetail.github.io/Fish/#eyJ0ZXh0IjoiPG8rdj8pMC19OlwiQFwiOlxubi0kPCIsImlucHV0IjoiIiwic3RhY2siOiJBIiwic3RhY2tfZm9ybWF0IjoiY2hhcnMiLCJpbnB1dF9mb3JtYXQiOiJjaGFycyJ9)
Takes input on the stack, which is [a default input method for stack-based languages](https://codegolf.meta.stackexchange.com/a/8493/114970), and outputs with error.
[Answer]
## JavaScript (ES6), 49 bytes
```
s=>s>0?String.fromCharCode(s|64):parseInt(s,36)-9
```
Accepts integers in either number or string format, always return integers as a number. 42 bytes if I can return lower case and don't need to handle integers in string format:
```
s=>s>0?(s+9).toString(36):parseInt(s,36)-9
```
[Answer]
# Fourier, 30 bytes
```
I~F<64{1}{F+64a}F>64{1}{F-64o}
```
[**Try it online!**](http://fourier.tryitonline.net/#code=SX5GPDY0ezF9e0YrNjRhfUY-NjR7MX17Ri02NG99&input=RQ)
[Answer]
## R, 73 bytes
```
f=function(x){L=LETTERS;if(is.numeric(x)){i=L[(x)]}else{i=which(L==x)};i}
```
[Answer]
# MATL, 10 bytes
```
6WZ~t42>?c
```
Explanation:
```
6W              % 2**6 = 64, but golfier looking
  Z~            % bit-wise XOR with input
    t42>?       % if result is greater than 42
         c      % convert it to a character 
                % else, don't
```
[Try it online!](http://matl.tryitonline.net/#code=NldafnQ0Mj4_Yw&input=WzE6MjZd) with numeric inputs.  
[Try it online!](http://matl.tryitonline.net/#code=NldafnQ0Mj4_Yw&input=WydBJzonWidd) with alphabetic inputs.
]