24
Write a program that takes as its input a string consisting of printable characters (ASCII 20-7E) and an integer n
in [2,16] and performs the following modification to the string.
- Each character in the string is converted to its ASCII code (the examples given are in hexadecimal, though base 10 is also acceptable).
- The ASCII codes are converted to base
n
and are concatenated together. - The new string is split every other character. If there are an odd number of characters, then the last character is removed entirely.
- Printing ASCII codes (in base 16) are converted back into their characters, whereas non-printing ASCII codes are removed.
- The resulting string is printed.
Test case
Input
Hello, World!
6
Steps
Hello, World!
48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21
2002453003003031125222330331030024453
20 02 45 30 03 00 30 31 12 52 22 33 03 31 03 00 24 45
The output of this program is E001R"31$E
.
This is code golf, so standard rules apply. Shortest code in bytes wins.
This encoding algorithm can be useful for sending secret messages! – user41805 – 2015-11-04T20:02:04.110
Gotta do this sometime! – anOKsquirrel – 2015-11-04T20:49:23.203
@ΚριτικσιΛίθος while the program works for every possible input string, not every output string is unique. For example, in base
7
, the stringJ
would go through the stepsJ
->50
->101
->10
->(no output)
, as would the stringK
orL
. – Arcturus – 2015-11-04T21:29:20.660Like @Eridan said, this is a lossy encryption since odd sequences get the last character lopped off. Though I'm sure to the ignorant observer it could be a snarky way of communicating :) – DoctorHeckle – 2015-11-04T21:33:20.153
Can a function be written? – LegionMammal978 – 2015-11-04T22:11:07.117
1Step 1 is confusing - no need to convert chars to hexadecimal - in the example:
H
is ASCII 72 (decimal) or 48 (hex), but what I need is 200 (base 6). All the row 2 in the example is useless and confusing in my opinion – edc65 – 2015-11-04T23:23:02.023@edc65 You're right about not needing the hex codes, though that's what I tend to prefer when looking at an ASCII table. I have changed the instructions, but the examples are how I would look at the problem. – Arcturus – 2015-11-05T01:34:06.443