12
\$\begingroup\$

Take a list of 2-digit hexadecimal numbers as input, and output the binary value, replacing each 1 with an 'X', and each 0 with a space.

For example

Input = FF, 81, 47, 99.

  • FF = 11111111 in binary, so print XXXXXXXX
  • 81 = 10000001 in binary, so print X X
  • 47 = 01000111 in binary, so print X XXX
  • 99 = 10011001 in binary, so print X XX X

Full output:

XXXXXXXX
X      X
 X   XXX
X  XX  X

Clarifications

  • The hexadecimal numbers will always be 2-digits.
  • There can be any number of hexadecimal numbers.
  • You can choose to input in whatever format you want (separated by a space, comma, newline, etc.)
  • The character to output for a 1 is X, and for 0 it is a . This will not change.
  • The outputs must be padded to length 8

Also, apparently I wasn't clear enough: the input is in Hexadecimal (Base 16) not denary (Base 10)

This is , so the shortest answer in bytes wins!

\$\endgroup\$
18
  • 1
    \$\begingroup\$ Should the binary representations be left-padded with 0s to the length of the longest or always to length 8? \$\endgroup\$
    – Shaggy
    Sep 30, 2022 at 16:59
  • 1
    \$\begingroup\$ @Shaggy - they must be padded to length 8, even if the biggest number is not 8 bits long. \$\endgroup\$
    – The Thonnu
    Sep 30, 2022 at 17:02
  • 1
    \$\begingroup\$ Please edit the question to clearly indicate the padding requirement (people are not supposed to read all comments), and maybe include a test case to address that \$\endgroup\$
    – Luis Mendo
    Sep 30, 2022 at 18:08
  • 5
    \$\begingroup\$ For future challenges, consider relaxing the input and output requirements (such as allowing input in decimal or allowing a the answerer to choose the 1 character, not necessarily X). I don't think those restrictions make the challenge more interestin \$\endgroup\$
    – Luis Mendo
    Sep 30, 2022 at 18:10
  • 2
    \$\begingroup\$ Is accepting a list of lists of integers from \$[0,15]\$ valid (i.e. a list of numbers in base sixteen) or should this be tagged string? \$\endgroup\$ Sep 30, 2022 at 18:21

39 Answers 39

1
2
1
\$\begingroup\$

Red, 82 bytes

func[b][forall b[parse s: enbase/base b/1 2[8[change"1""X"| change"0"sp]]print s]]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Thon -el, 16 bytes

C{16D" X"b8" "JO

Explanation:

C{16D" X"b8" "JO
C{                // Get a list of strings as input, and loop through the list
  16D             // Convert from base 16 to base 10
     " X"b        // Convert from base 10 to base " X"
          8" "J   // Fill with " " up to 8 characters
               O  // Output

(-el flag is needed to prevent the last line being output twice)

\$\endgroup\$
2
  • \$\begingroup\$ Several years ago, we dropped the rule that languages have to precede the challenge. So this is competing. Relevant meta post: codegolf.meta.stackexchange.com/questions/12877/… \$\endgroup\$
    – naffetS
    Oct 3, 2022 at 18:03
  • \$\begingroup\$ @Steffan thanks. I didn't know about that. \$\endgroup\$
    – The Thonnu
    Oct 3, 2022 at 18:31
1
\$\begingroup\$

Ohm v2, 11 bytes

ôb€τ‥X r8Ü,

Try it online!

A neat little answer, in my opinion.

Explained

ôb€τ‥X r8Ü,
ôb          # Convert each hex number to decimal then to binary
  €         # To each now-binary number:
   τ‥X r    #    Replace all 0s with spaces and 1s with Xs
        8Ü  #    Right-pad with spaces until the string is length 8
          , #    Print with a newline
\$\endgroup\$
1
\$\begingroup\$

Nibbles, 10 9.5 bytes (19 nibbles)

>>.``@hex:1@=$"X "
>>.``@hex:1@=$"X "
                     # implicit map over lines of input:
         :1@         # prepend with 1
      hex            # get the hexadecimal value
   ``@               # convert to binary digits
  .                  # and use each of these
            =$       # to index into
              "X "   # letters of the string "X ";
>>                   # finally, remove the first element

enter image description here

\$\endgroup\$
1
\$\begingroup\$

><>, 83 bytes

2:?!^i:0(?;" "\ao
-:07@@af+)?$~-\
:?!v:2%@2,:1%->
00.>~$~1-
oa4.>~}}}}rl?!v" X"{?$~

Try it online!

Explanation

  1. Subtract 32 from input. If the result is larger than 25, subtract 7.
    This gives us the decimal value of the input+16.
  2. Repeat mod by 2, divide by 2 / mod by 1 to get the binary digits.
    Drop the first digit of the binary value.
    Since we added 16 to input this gives us the leading zeroes.
  3. Do steps 1 and 2 one more time for the 2nd hex digit.
  4. Reorder values, replace with "X" or " " and print each.
  5. When done, print a newline and go back to 1.
\$\endgroup\$
1
\$\begingroup\$

Raku, 67 bytes (62 characters)

' X'.comb[words».parse-base(16)».fmt('%08b')».comb]».join».say

Expects one picture per line, hex values space-separated on each line. Written to operate on standard input; change words to .words to operate on the topic instead at the cost of another byte.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Scala, 140 bytes

Golfed vesrion. Attempt this online!

def f(a:String):String=String.format("%08d",Integer.parseInt(Integer.toBinaryString(Integer.parseInt(a,16)))).map(c=>if(c=='0')' ' else 'X')

Ungolfed version. Attempt this online!

import scala.io.StdIn.readLine
import scala.jdk.CollectionConverters._

object Main {
  def f(input: String): String = {
    val decimal = Integer.parseInt(input, 16)
    val binary =
      String.format("%08d", Integer.parseInt(Integer.toBinaryString(decimal)))
    val translated = binary.map(c => if (c == '0') ' ' else 'X')
    return translated;
  }
  def main(args: Array[String]): Unit = {
    Array("FF", "00", "81", "47", "99").map(f).foreach(println);
  }
}

\$\endgroup\$
1
\$\begingroup\$

Stax, 11 8 bytes

╨÷◘Jè∞╩:

Run and debug it

Unpacks to:

Stax, 10 bytes

m|H. X:B8)

Run and debug it

m          # map the following over the input array:
 |H        # convert from hex to decimal
      :B   # and then to base
   . X     # " X"
        8) # and pad to length 8
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 N, 8 bytes

H` XBð8ṙ

Try it online!

Explanation

H` XBð8ṙ  # Implicit input
H         # From hexadecimal
 ` XB     # To base " X"
     ð8ṙ  # Pad to length 8
          # Implicit output
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.