Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Convert to Hexadecimal

Parent

Convert to Hexadecimal

+2
−1

Challenge

Write a program that takes in a number greater than or equal to 0 and outputs its representation in hexadecimal (base 16).


Examples

0     => 0
1     => 1
10    => A
15    => F
16    => 10
107   => 6B
153   => 99
207   => CF
1000  => 3E8
10381 => 288D
48821 => BEB5

Rules

  • No built-in hexadecimal functions.
    • More generally, any base conversion function is considered to be disallowed.
  • Letters can be uppercase or lowercase.
  • It should work at least for all inputs greater than 0 up to the language's default type limit.
  • Leading zeros in the output is fine. (i.e. 000003E8 for 32-bit numbers)
  • This is [code-golf], so shortest code wins.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

Input in integer type? (4 comments)
What defines a "Hexadecimal function"? Is it strictly a `toHex` function? Is `.toString(16)` banned? ... (6 comments)
Post
+1
−0

JavaScript (Node.js), 60 bytes

n=>{for(l='';n;l="0123456789ABCDEF"[n%16]+l,n>>=4);return l}

Try it online!

Explanation

n=>{for(l='';n;l="0123456789ABCDEF"[n%16]+l,n>>=4);return l}/*
        l='';                                                   | ⁡Starting with an empty string...
    for(     n;                                  );             | ⁢...loop while n still has hex digits (i.e. nonzero):
               l="0123456789ABCDEF"[n%16]+l,                    | ⁣    add the hex digit corresponding to n mod 16 on the left (so no +=)
                                            n>>=4               | ⁤    then floor division by 16
n=>{                                               return l}    | ⁢⁡(in an anonymous arrow function)
💎*/

Created with the help of Luminespire.

Caveats

"It should work at least for all inputs greater than 0 up to the language's default type limit." - the question. (i.e. returning an empty string for 0 is fine)
"The empty string is a syntactically valid representation of zero in positional notation (in any base), which does not contain leading zeros." - Wikipedia, which agrees with my opinion, but that doesn't matter anyways as stated above

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Currently works a second time - does it need to? (1 comment)
Currently works a second time - does it need to?
trichoplax‭ wrote 4 days ago

There's a way to save 4 bytes but it will make the code break after the first time it is called. I don't think we have a rule on this yet, so I've raised on Meta Answers that work only once to see what the community thinks. So the suggestion mentioned there as an example might not be valid yet, but there's a chance it may become valid once there's a consensus. I can't guess which way it will go.