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

+3
−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

3 comment threads

Does zero input need to be handled? (1 comment)
Input in integer type? (5 comments)
What defines a "Hexadecimal function"? Is it strictly a `toHex` function? Is `.toString(16)` banned? ... (6 comments)
Post
+0
−0

R, 73 bytes

`-`=\(n,s="",`<`=paste0)`if`(n,n%/%16-c(0:9,LETTERS[1:6])[1+n%%16]<s,0<s)

Attempt This Online!

Similar to the JS and Python answers, but uses recursion.

Adds a leading 0 to the output. This way, the case for 0 is solved, with no empty output.

History

1 comment thread

Working for input zero is required (5 comments)
Working for input zero is required
trichoplax‭ wrote 8 months ago

Welcome to Codidact!

From the challenge wording:

Write a program that takes in a number greater than or equal to 0

The score in the heading needs to be the number of bytes in code that works for 0, otherwise the answer will likely be deleted due to having an unfair advantage over answers that meet the requirements.

trichoplax‭ wrote 8 months ago · edited 8 months ago

It appears that the code currently gives empty output for input zero, rather than failing. A JavaScript answer already relies on claiming that empty output is a valid representation of zero, so you might just need to edit to reflect this rather than saying "Fails on 0".

trichoplax‭ wrote 8 months ago

In case this answer is edited to rely on being able to give empty output to represent zero, I've posted on Meta to see if voting supports this approach as a default.

Glory2Ukraine‭ wrote 8 months ago

trichoplax‭ thank you for the clarification; one of the rules in this challenge is "It should work at least for all inputs greater than 0 ", and I took it literally. Anyway I have modified my golf to include the 0-case too.

trichoplax‭ wrote 8 months ago

Thanks for pointing that out - I had overlooked the inconsistency in the challenge rules. I've commented under the challenge to ask for this to be made consistent.