20
\$\begingroup\$

Characters in strings are sometimes represented as their ASCII hexadecimal codes. Printable characters have two hex digits in their representation. Swapping those digits leads to another character, which will be our output.

The table of relevant character codes can be found on Wikipedia.

Details

  1. Take a string as input.
  2. For each character:
    1. Find corresponding hex value of ASCII code.
    2. Swap (reverse) the hex digits.
    3. Convert back to character.
  3. Output the new string.

Rules

  • To make thinks easier, let's consider only characters "reversible" within standard printable ASCII range - that is codepoints (in hex): 22-27,32-37,42-47,52-57,62-67,72-77 or characters: "#$%&'234567BCDEFGRSTUVWbcdefgrstuvw.
  • Input and output should be strings or closest equivalent in your language of choice.
  • You may assume the input is non-empty.
  • This is , the usual rules apply.

Example

input: debug
hex codes: 64 65 62 75 67
reversed: 46 56 26 57 76
output: FV&Wv

Test cases

bcd <-> &6F
234 <-> #3C
screw <-> 76'Vw
debug <-> FV&Wv
BEEF <-> $TTd
7W66V77 <-> success
"#$%&'234567BCDEFGRSTUVWbcdefgrstuvw <-> "2BRbr#3CScs$4DTdt%5EUeu&6FVfv'7GWgw
\$\endgroup\$
1
  • 3
    \$\begingroup\$ So... rotate by 4 on 8-bit ASCII character values. Converting to hex and back is merely one implementation, but seems to me an over-complicated way to describe it. (But I guess describes in terms of things more languages can do easily; some can't easily manipulate characters as integers.) \$\endgroup\$ May 26, 2022 at 0:48

36 Answers 36

1
2
1
\$\begingroup\$

Red, 39 bytes

func[s][foreach c s[prin c * 16 % 255]]

Try it online!

Based on other similar answers. Thanks to @dingledooper and @UnrelatedString (I think) for discovering it.

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

Knight, 47 bytes

;;;;=xP=y""=i~1W<=i+1iLx=y+yA%*16A Gx i 1 255Oy

Try it online!

Ungolfed & explained:

; = x PROMPT                                               # x = input()
; = y ""                                                   # y = ""
; = i ~1                                                   # i = -1
; WHILE (< (= i (+ 1 i)) (LENGTH x))                       # while (i = i + 1) < length(x):
  : = y (+ y (ASCII (% (* 16 (ASCII (GET x i 1))) 255)))   #   y = y + chr((ord(x[i]) * 16) % 255)
: OUTPUT y                                                 # print(y)
\$\endgroup\$
1
  • \$\begingroup\$ You never need to move around the semicolons when golfing. Let them separate the adjacent statements and you can always save 0 or 1 space. \$\endgroup\$
    – Bubbler
    Aug 3, 2022 at 4:33
1
\$\begingroup\$

Knight, 32 bytes

;=xP:Wx;O+A%*16Ax 255"\"=xGx 1Lx

Try it online!

Golfed Steffan's answer to minimize the use of auxiliary variables. y is not necessary if we print each char right away; i is not necessary since A implicitly grabs the 0th char from the current string and we can chop x directly.

Ungolfed:

; = x P                       x = a line of stdin
: W x                         While x is nonempty:
  ; O + A % * 16 A x 255 "\"    Output chr(ord(x[0]) * 16 % 255) + "\"
                                (to suppress the implicit newline ending)
  : = x G x 1 L x               x = x[1:]

If output of each char separated by newline is allowed:

Knight, 29 bytes

;=xP:Wx;O A%*16Ax 255=xGx 1Lx

Try it online!

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

Pyth, 9 bytes

mCi_.Hd16

Test suite

Outputs a list of characters.

Explanation:
mCi_.Hd16  | Full program
mCi_.Hd16Q | with implicit variables
-----------+-----------------------------------
m        Q | For each character d of the input,
    .Hd    |  Convert to hexstring
   _       |  Reverse
  i    16  |  Convert from base 16
 C         |  Convert to character
\$\endgroup\$
1
\$\begingroup\$

J, 15 bytes

[:u:255|16*3&u:

Try it online!

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

Thunno 2 B, 3 bytes

ḤṃH

Try it online!

Explanation

ḤṃH  # Implicit input
     # Implicit cast to ordinals
Ḥ    # Convert each to hexadecimal
 ṃ   # Reverse each string
  H  # Convert each from hexadecimal
     # Implicit cast to characters
     # 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.