[Question]
  [
# Challenge
Here's a simple one.
Write a function or program when given a number in base 10 as input, it will return or print that number's value in [Hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal "Hexadecimal - Wikipedia").
# Examples
```
15 -> F
1000 -> 3E8
256 -> 100
```
# Rules
* No built-in Hexadecimal functions whatsoever
* Letters may be lowercase or uppercase
* You will only need to worry about non-negative integers, no negatives or pesky decimals
* It should work with any arbitrarily large number up to language's default type's limit.
* Newline not mandatory
* As usual, this is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest code measured in bytes wins!
  
[Answer]
# Turing Machine Code, 412 bytes
As usual, I'm using the rule table syntax defined [here.](http://morphett.info/turing/turing.html) You can test it on that site or, alternatively, using [this java implementation.](https://github.com/SuperJedi224/Turing-Machine)
```
0 * * l B
B * * l C
C * 0 r D
D * * r E
E * * r A
A _ * l 1
A * * r *
1 0 9 l 1
1 1 0 l 2
1 2 1 l 2
1 3 2 l 2
1 4 3 l 2
1 5 4 l 2
1 6 5 l 2
1 7 6 l 2
1 8 7 l 2
1 9 8 l 2
1 _ * r Y
Y * * * X
X * _ r X
X _ _ * halt
2 * * l 2
2 _ _ l 3
3 * 1 r 4
3 1 2 r 4
3 2 3 r 4
3 3 4 r 4
3 4 5 r 4
3 5 6 r 4
3 6 7 r 4
3 7 8 r 4
3 8 9 r 4
3 9 A r 4
3 A B r 4
3 B C r 4
3 C D r 4
3 D E r 4
3 E F r 4
3 F 0 l 3
4 * * r 4
4 _ _ r A
```
Counts down from the input in base 10 while counting up from 0 in base 16. On decrementing zero, it erases the input block and terminates.
[Answer]
# Java, ~~92~~ 89 bytes
```
String x(int v){String z="";for(;v>0;v/=16)z="0123456789ABCDEF".charAt(v%16)+z;return z;}
```
[Answer]
# Javascript, ~~49~~ 43 bytes.
```
h=i=>(i?h(i>>4):0)+"0123456789abcdef"[i%16]
```
6 bytes saved by [user81655](https://codegolf.stackexchange.com/users/46855/user81655).
Test it [here](https://jsfiddle.net/g1smkay1/).
This has two leading zeroes, which is allowed by the rules.
Here's a version without leading zeroes: (47 bytes). 
```
h=i=>(i>15?h(i>>4):"")+"0123456789abcdef"[i%16]
```
Test it [here](https://jsfiddle.net/y2drn9qv/).
Both of these uses exactly the same approach as my [Python answer](https://codegolf.stackexchange.com/a/68249/34543). 
[Answer]
# CJam, ~~22~~ 21 bytes
```
ri{Gmd_A<70s=+\}h;]W%
```
*Thanks to @MartinB√ºttner for golfing off 1 byte!*
[Try it online!](http://cjam.tryitonline.net/#code=cml7R21kXzk-eyc3K30mXH1oO11XJQ&input=MTAwMA)
### How it works
```
ri                      e# Read an integer from STDIN.
  {             }h      e# Do:
   Gmd                  e#   Push qotient and residue of the division by 16.
      _A<               e#   Check if the residue is less than 10.
         70s            e#   Push "70".
            =           e#   Select the character that corresponds to the Boolean.
             +          e#   Add the character to the digit.
                        e#   This way, 10 -> 'A', etc.
               \        e#   Swap the quotient on top of the stack.
                        e# While the quotient is non-zero, repeat the loop.
                  ;     e# Pop the last quotient.
                   ]W%  e# Reverse the stack.
```
[Answer]
# Pyth, 33 26 21 20 bytes
This was a fun one.
```
sm@+jkUTGi_d2_c_.BQ4
```
[Try it online.](http://pyth.herokuapp.com/?code=sm%40%2BjkUTGi_d2_c_.BQ4&input=11259375&debug=0)
Explained:
```
                .BQ      Convert input to a binary string, e.g. 26 -> '11010'
             _c_   4     Reverse, chop into chunks of 4, and reverse again. We reverse 
                         because chop gives a shorter last element, and we want a shorter
                         first element: ['1', '0101']
                         Reversing three times is still shorter than using .[d4 to pad the
                         binary string to a multiple of 4 with spaces.
 m                       Map across this list:
         i_d2                Take the value of the reversed string in binary,
  @                          and use it as an index into the string:
   +jkUTG                    '0123456789abcdefghijklmnopqrstuvwxyz'
                             (The alphabet appended to the range 0 to 10)
s                        Concatenate to create the final string.
```
[Answer]
# C (function), 51
Recursive function takes input integer as a parameter:
```
f(n){n>>4?f(n>>4):0;n&=15;n+=n>9?55:48;putchar(n);}
```
### Test driver:
```
#include <stdio.h>
f(n){if(n>>4)f(n>>4);n&=15;n+=n<10?48:55;putchar(n);}
int main (int argc, char **argv) {
    f(15);puts("");
    f(1000);puts("");
    f(256);puts("");
    f(0);puts("");
    return 0;
}
```
[Answer]
# Python, ~~59~~ 58 bytes
```
h=lambda i:(i>15 and h(i/16)or'')+"0123456789abcdef"[i%16]
```
1 byte saved by [CarpetPython](https://codegolf.stackexchange.com/users/31785/carpetpython)
Run as: `print h(15)`
Test it [here](http://ideone.com/fork/TnwsF4) (Ideone.com).
Explanation:
```
h=lambda i:                                                 # Define h as a function that takes two arguments
           (i>15 and h(i/16)or'')                           # Evaluate h(i/16) if i > 15, else, give ''
                                 +"0123456789abcdef"[i%16]  # Append (i%16)'th hexadecimal number.
```
[Answer]
## Haskell, ~~59~~ ~~58~~ ~~43~~ ~~41~~ 39 bytes
```
s="0123456789ABCDEF"
(sequence(s<$s)!!)
```
Usage example: `sequence(s<$s)!!) $ 1000` -> `"00000000000003E8"`.
This creates a list of all hexadecimal numbers up to 16 hex-digits. Luckily this happens in order, so we can simply pick the `n`th one.
Edit: @Mauris squeezed out 2 bytes. Thanks!
[Answer]
# [APL (Dyalog APL)](https://www.dyalog.com/), 17 bytes
Must be run with `‚éïIO‚Üê0`, which is default on many APL systems.
```
(‚éïD,‚éïA)[16‚ä•‚ç£¬Ø1‚ä¢‚éï]
```
[Try it online!](https://tio.run/nexus/apl-dyalog#e9Q31dP/UdsEA65HHe1p/zUe9U110QESjprRhmaPupY@6l18aL3ho65FQLHY/0A1/9O4DE25gISBgQGQMjI1AwA "APL (Dyalog APL) ‚Äì TIO Nexus")
`(‚éïD,‚éïA)[`‚Ä¶`]` **D**igits concatenated to **A**lphabet, then indexed by‚Ä¶
 `16‚ä•‚ç£¬Ø1`  the inverse of 16-Base-to-Number, i.e. Number-to-Base-16
 `‚ä¢` applied to
 `‚éï` numeric input
[Answer]
# dc, 37
```
?[16~rd0<m]dsmxk[A~r17*+48+Pz0<p]dspx
```
Recursively divmods by 16, pushing the remainder to the stack until nothing left to divide. Then print each element of the stack, using divmod by 10 to achieve A-F digits. Probably more detail tomorrow... (and hopefully less bytes).
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~45~~ 44 bytes
```
f(n){n&&f(n/16);n%=16;putchar(n+48+n/10*7);}
```
[Try it online!](https://tio.run/##S9ZNT07@/z9NI0@zOk9NDUjrG5ppWuep2hqaWReUliRnJBZp5GmbWGgDJQy0zDWta//nJmbmaWhWK6RpmJmYG1toWivU/gcA "C (gcc) ‚Äì Try It Online")
[Answer]
# Bash (function), 62
Thanks to @manatwork for suggesting using recursion.
```
h()(x=({0..9} {A..F})
echo `(($1>15))&&h $[$1/16]`${x[$1%16]})
```
[Answer]
# [Perl 6](http://perl6.org), ~~¬†53¬†~~ 48 bytes
```
~~{[R~] (0..9,'A'..'F').flat[($\_,\*div 16...^0)X%16]||0}~~
{[R~] (0..9,'A'..'F').flat[.polymod(16 xx*)]||0}
```
This creates a sequence of values that are Integer divided (`div`), until the result is `0` excluding the `0` from the sequence
```
$_, * div 16 ...^ 0
```
It then crosses (`X`) that sequence using the modulus operator (`%`) with `16`
```
( ‚Ä¶ ) X[%] 16
```
It uses those values as indexes into a flattened list consisting of two Ranges `0..9` and `'A'..'Z'`
```
( 0 .. 9, 'A' .. 'Z' ).flat[ ‚Ä¶ ]
```
Finally it concatenates (`~`) them using the reverse (`R`) meta operator
```
[R[~]] ‚Ä¶
```
If that results in a False value (empty string), return `0`
```
‚Ä¶ || 0
```
---
Usage:
```
# (optional) give it a lexical name for ease of use
my &code = { ‚Ä¶ }
say <15 1000 256 0>.map: &code;
# (F 3E8 100 0)
say code 10¬π‚Å∞‚Å∞;
# 1249AD2594C37CEB0B2784C4CE0BF38ACE408E211A7CAAB24308A82E8F10000000000000000000000000
```
[Answer]
# [MATL](https://esolangs.org/wiki/MATL), 27 bytes
```
i`16H#\wt9>?7+]wt]xN$hP48+c
```
This uses [release 5.1.0](https://github.com/lmendo/MATL/releases/tag/5.1.0) of the language/compiler, which is earlier than this challenge.
### Example
```
>> matl
 > i`16H#\wt9>?7+]wt]xN$hP48+c
 >
> 1000
3E8
```
### Explanation
```
i              % input number
`              % do...
  16H#\        % remainder and quotient of division by 16
  w            % move remainder to top of stack
  t9>          % does it exceed 9?
  ?            % if so
    7+         % add 7 (for letter ASCII code)
  ]            % end if
  w            % move quotient back to the top
  t            % duplicate 
]              % ...while (duplicated) quotient is not zero
x              % delete last quotient (zero)
N$h            % create vector of all remainders 
P              % flip vector
48+c           % add 48 and convert to char (will be implicitly displayed)
```
[Answer]
# ùîºùïäùïÑùïöùïü, 31 chars / 62 bytes
```
‚Ü∫a=‚¨Ø;√Ø;√Ø‚â´4@a=‚©•·∏äƒÄ‚í∏¬™‚©•‚ÅΩ·πÅ·πá‚∏©‚®ù[√Ø%·∏ê]+a
```
`[Try it here (Firefox only).](http://molarmanful.github.io/ESMin/interpreter.html?eval=true&input=256&code=%E2%86%BAa%3D%E2%AC%AF%3B%C3%AF%3B%C3%AF%E2%89%AB4%40a%3D%E2%A9%A5%E1%B8%8A%C4%80%E2%92%B8%C2%AA%E2%A9%A5%E2%81%BD%E1%B9%81%E1%B9%87%E2%B8%A9%E2%A8%9D%5B%C3%AF%25%E1%B8%90%5D%2Ba)`
Okay, I figured out some more stuff that golfed it down.
# Explanation
It's essentially the same solution as @SuperJedi224's ES6 solution - but with something different.
See `‚©•·∏äƒÄ‚í∏¬™‚©•‚ÅΩ·πÅ·πá‚∏©‚®ù`? That's a really fancy way of writing `"0123456789ABCDEF"`. `‚©•·∏ä` creates a range from 0 to 10, `‚í∏¬™‚©•‚ÅΩ·πÅ·πá‚∏©` creates a range from 65 to 71 and converts it to a string of ASCII, and `ƒÄ...‚®ù` concatenates the two ranges and joins them into one string. This was probably the coolest part of my solution.
# Bonus non-competitive version, 24 chars / 45 bytes
```
‚Ü∫;√Ø;√Ø‚â´4@·µ¥=(‚©•·∏ä‚®ù+·∂ê)[√Ø%·∏ê]+·µ¥
```
I decided to add an alphabet string, like in Pyth.
[Answer]
# sed, 341 bytes
```
:
s/\b/_/2
s/[13579]/&;/g
y/123456789/011223344/
s/;0/5/g
s/;1/6/g
s/;2/7/g
s/;3/8/g
s/;4/9/g
s/;_;_;_;_/=/
s/;_;_;__/+/
s/;_;__;_/:/
s/;_;___/>/
s/;__;_;_/</
s/;__;__/?/
s/;___;_/(/
s/;____/*/
s/_;_;_;_/-/
s/_;_;__/^/
s/_;__;_/%/
s/_;___/$/
s/__;_;_/#/
s/__;__/@/
s/___;_/!/
s/____/)/
/[1-9_]/b
y/)!@#$%^-*(?<>:+=/0123456789ABCDEF/
s/^0*//
```
It's not the obvious language for this challenge, but it does have the advantage of supporting input numbers up to (depending on your implementation) between 4000 digits and the limit of your system's available (virtual) memory. I converted RSA-1024 to hex in about 0.6 seconds, so it scales reasonably well.
It works using successive division by two, accumulating every 4 bits of carry into a hex digit. We use non-letter characters to represent our output, so that we always accumulate carry between the decimal input and the hex output, and convert to conventional hexadecimal at the very end.
[Answer]
# PHP, ~~65 66 64+1 62~~ 59 bytes
```
function h($n){$n&&h($n>>4);echo"0123456789abcdef"[$n&15];}
```
recursive printing function, prints a leading zero (insert `>16` before `&&` to remove it)
---
programs, 64 bytes +1 for `-R` (run as pipe with `-nR`)
```
for(;$n=&$argn;$n>>=4)$s="0123456789abcdef"[$n&15].$s;echo$s?:0;
```
requires PHP 5.6 or later (5.5 cannot index string literals)
or
```
for(;$n=&$argn;$n>>=4)$s=(abcdef[$n%16-10]?:$n%16).$s;echo$s?:0;
```
requires PHP 5.6 or 7.0 (7.1 understands negative string indexes)
---
[Answer]
# Julia, 55 bytes
```
h(n)=(n>15?h(n√∑16):"")"0123456789ABCDEF"[(i=n%16+1):i]
```
This is the basic recursive function implementation. It accepts an integer and returns a string.
If the input is less than 15, floor divide it by 16 and recurse, otherwise take the empty string. Tack this onto the front of the appropriately selected hexadecimal character.
[Answer]
# [Pyre](https://github.com/tuomas56/pyre), 98 bytes
Doing this in a language without arithmetic operators was probably a mistake.
```
let h=def (n)(if n.gt(15)h(n.div(16).int!)else "").concat("0123456789abcdef".list!.get(n.mod(16)))
```
Use like this:
```
do
  let h = ...
  print(h(15))
end
```
Ungolfed:
```
let h = def (n) do
    if n.gt(15) 
        let x = h(n.div(16).int!)
    else 
        let x = ""
    x.concat("0123456789abcdef".list!.get(n.mod(16)))
end
```
[Answer]
# Ruby, 48 characters
(Copy of [Loovjo](https://codegolf.stackexchange.com/users/34543/loovjo)'s [Python answer](https://codegolf.stackexchange.com/a/68249).)
```
h=->n{(n>15?h[n/16]:'')+[*?0..?9,*?a..?f][n%16]}
```
Sample run:
```
2.1.5 :001 > h=->n{(n>15?h[n/16]:'')+[*?0..?9,*?a..?f][n%16]}
 => #<Proc:0x00000001404a38@(irb):1 (lambda)> 
2.1.5 :002 > h[15]
 => "f" 
2.1.5 :003 > h[1000]
 => "3e8" 
2.1.5 :004 > h[256]
 => "100" 
```
[Answer]
## Seriously, 35 bytes
```
,`;4¬™@%)4¬™@\`‚ï¨XŒµ D`@;7¬™D+@9<7*+c+`n
```
Hex Dump:
```
2c603b34a640252934a6405c60ce58ee204460403b37a6442b40393c372a2b632b606e
```
[Try It Online](http://seriouslylang.herokuapp.com/link/code=2c603b34a640252934a6405c60ce58ee204460403b37a6442b40393c372a2b632b606e&input=1000)
Explanation:
```
,                                    Get evaluated input
 `          `‚ï¨                       Repeat the quoted function until top of stack is 0
  ;4¬™@%                              Make a copy of the number mod 16
       )                             Send it to bottom of stack
        4¬™@\                         Integer divide the original copy by 16
              X                      Delete the leftover zero. At this point the stack is 
                                     the "digits" of the hex number from LSD to MSD
               Œµ                     Push empty string
                 D`              `n  Essentially fold the quoted function over the stack.
                   @;                Roll up the next lowest digit, make a copy
                     7¬™D+            Add 48
                         @           Bring up the other copy
                          9<         1 if it's at least 10, else 0
                            7*       Multiply with 7. 
                              +      Add. This will shift 58->65 and so on.
                               c     Convert to character.
                                +    Prepend to current string.
```
Note that the `;7¬™D+@9<7*+c` is equivalent to `4¬™‚ñÄE`, which would save 8 bytes, but I thought that perhaps a function that pushes the base b digits as a string might be considered too much of a "heaxadecimal built-in".
[Answer]
# Javascript ES6, ~~64~~ 58 bytes
```
v=>eval('for(z="";v;v>>=4)z="0123456789ABCDEF"[v%16]+z')
```
Saved 6 bytes thanks to ◊ün…üu…ê…Ø…π…ê◊üo…Ø and user81655.
[Answer]
# Befunge-93, 58
```
&:88+%"0"+:"9"`7*+\8/2/:!#|_#
,_@                       >:#
```
First time doing a real golfing challenge in Befunge, I bet there's a one-liner for this that's shorter since all those spaces in the middle of the second line seem wasteful.
You can step through it [here](http://www.quirkster.com/iano/js/befunge.html). Partial explanation:
`&`: Take input.
`:88+%`: Take the remainder modulo 16.
`"0"+`: Add it to the ASCII value of 0.
`:"9"``: If the result is greater than the ASCII value of 9...
`7*+`: Add 7 to convert it to a letter.
`\`: Save the resulting character on the stack.
`8/2/`: Divide by 16 rounding down.
`:!#|_`: Exit the loop if the result is 0.
`#`: Otherwise go back to the modulus step.
`>:#,_@` (wrapping around): Once finished, output the stack in LIFO order.
[Answer]
# [><>](https://esolangs.org/wiki/Fish), 46 + 3 = 49 bytes
This would have been shorter if ><> had integer division, which we now have to emulate by subtracting modulo 1. Still, I think this uses some pretty neat wrapping around tricks!
```
>:?!v:f1+%:a(?v  v
\-%1:,+1f}+"0"<+7<
!?:r/ro;
```
[Try it online!](https://tio.run/nexus/fish#@29nZa9YZpVmqK1qlahhX6agUMYVo6tqaKWjbZhWq61koGSjbW7DpWhvVaRflG/9//9/3TIFQwMDAwA "><> ‚Äì TIO Nexus")
## Explanation
**First loop**
```
>:?!v:f1+%:a(?v  v
\-%1:,+1f}+"0"<+7<
```
The first loop performs the classic converting to hex
algorithm. It does modulo 16 (`:f1+%`) and checks if 
the result is < 10 (`:a(?`). If it's not, we need to add
7 (`7+`) in order to go from the decimals to the capital alphabet
in the ASCII table. Else, we can proceed by adding the ASCII
value for 0 (`"0"+`) and shifting the character to be output
to the bottom of the stack because we'll have to output them
in reverse order. The top value is then replaced by its result
of integer division by 16. This is emulated by computing 
*a/b - (a/b)%1* (`f1+,:1%-`). When the loop is finished, the stack contains the hexadecimal characters in reversed output order and a 0.
**Second loop**
```
!?:r<ro;
```
The second loop reverses the list and checks if top element is 0. 
If it is, we know all nonzero were printed and we should terminate. 
Else, we output the character and reverse the list again to prepare 
for the next iteration. The `:` when entering the second loop will duplicate the 0 which has no effect.
[Answer]
# SpecBAS - 110 bytes
```
1 h$="0123456789ABCDEF",r$=""
2 INPUT d
4 q=INT(d/16),r=d-(q*16),r$=h$(r+1)+r$,d=q
5 IF q>15 THEN 4
6  ?h$(q+1)+r$
```
This uses an algorithm I found on [WikiHow](http://www.wikihow.com/Convert-from-Decimal-to-Hexadecimal) (2nd method).
Strings in SpecBAS are 1-based, hence the `+1` to pick out the correct element.
[Answer]
# [C (clang)](http://clang.llvm.org/), 83 bytes
```
f(n){char h[8]={0};int i=8;while(i>0){h[i-1]=n%16;n/=16;printf("%x",h[8-i]);i--;}}
```
[Try it online!](https://tio.run/nexus/c-clang#FYxBCoMwEADvfcViERJo2ngpwjZ@RHKQGMmCXSWNtBDy7J6tvcxpZs7Ebt5G/3ilkZZr6PZJsMwuDBFC31qTdUHiBGRafAeavaBOyxx6Uo01XDd35Js5uMZDm0RVf6rLUSqyEkkpLOW0/wfPgVjIDJNotNYSIfq0RQaNUPYvL8oNLvgf "C (clang) ‚Äì TIO Nexus")
Alternate solution in C
[Answer]
# Ruby, 40 bytes
~~Stolen from~~ Inspired by manatwork's answer, but using an interesting loophole to make it shorter.
```
h=->n{(n>15?h[n/16]:'')+(n%16).to_s(17)}
```
[Answer]
## REXX, ~~80~~ 78 bytes
```
arg n
h=
do while n>0
  h=substr('0123456789ABCDEF',n//16+1,1)h
  n=n%16
  end
say h
```
[Answer]
# C, 48 bytes
```
h(x){x/16&&h(x/16);x%=16;putchar(x+=48+x/10*7);}
```
This is not completely original, I shaved 5 bytes off of the version Digital Trauma put up.
[Answer]
# APL(NARS), chars 34, bytes 68
```
{‚çµ‚â§0:,'0'‚ãÑ(‚àá‚åä‚çµ√∑16),(1+16‚à£‚çµ)‚äÉ‚éïD,‚éïA}
```
test:
```
  g‚Üê{‚çµ‚â§0:,'0'‚ãÑ(‚àá‚åä‚çµ√∑16),(1+16‚à£‚çµ)‚äÉ‚éïD,‚éïA}
  g 0
0
  g 100
064
  g 1000
03E8
  g 1
01
```
]