ASCII Character Jumble

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.

Arcturus

Posted 2015-11-04T16:27:37.627

Reputation: 6 537

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 string J would go through the steps J -> 50 -> 101 -> 10 -> (no output), as would the string K or L. – Arcturus – 2015-11-04T21:29:20.660

Like @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

Answers

5

Pyth - 22 bytes

Hope to golf a lot more, pretty straightforward.

sfq3l`TmCid16csjRQCMz2

Try it online here.

Maltysen

Posted 2015-11-04T16:27:37.627

Reputation: 25 023

q3l`T is wonderful. – isaacg – 2015-11-05T02:48:36.133

Shouldn't fgTd be enough? – Jakube – 2015-11-05T09:41:05.540

4

CJam, 33 bytes

q~:i\fb:~0+2/W<Gfb:c' ,-'ÿ),127>-

Takes input in the form 6 "Hello, World!". Test it here.

See Dennis's answer for a similar but better solution with a nice explanation.

Martin Ender

Posted 2015-11-04T16:27:37.627

Reputation: 184 808

3

Bash + common linux utils, 118

printf %s "$1"|xxd -p|sed -r "s/../\U& /g;y/ /n/;s/^/dc -e$2o16i/e;s/../& /g;s/ .$//;"|xxd -rp|sed 's/[^[:print:]]//g'

Digital Trauma

Posted 2015-11-04T16:27:37.627

Reputation: 64 644

I think you could shorten printf %s "$1" into echo -n "$1" to save 2 bytes – Aaron – 2015-11-05T09:44:49.390

@Aaron That works until the input string is -e. Try echo -n "-e"

– Digital Trauma – 2015-11-05T19:06:18.847

Damn, nicely spotted ! – Aaron – 2015-11-05T19:20:09.950

2

JavaScript (ES6), 137 147

Using the most verbose functions available in JavaScript

f=(s,b)=>alert(s.replace(/./g,x=>x.charCodeAt().toString(b)).match(/../g).map(x=>(x=String.fromCharCode('0x'+x))<='~'&x>' '?x:'').join``)

// Just for test purpose, redefine alert()
alert=x=>document.write('<pre>'+x+'</pre>')

f('Hello, World!',6)
f('PORK',3)

edc65

Posted 2015-11-04T16:27:37.627

Reputation: 31 086

+1 for x=>x>= – Ypnypn – 2015-11-04T23:57:23.653

I think you can save some bytes by using [for(z of ...)if(...)...] instead of map(...).filter(...) – Ypnypn – 2015-11-04T23:58:17.500

@Ypnypn I did not found a way to use your hint (apart from using array comprehension that is ES7) but you pressed me to rethink it all. Thx. I hope you'll keep your +1 even if x=>x>= has gone – edc65 – 2015-11-05T00:08:03.967

1What's wrong with using ES7? – Ypnypn – 2015-11-05T01:20:45.043

1@Ypnypn I prefer an answer that can work even with subpar javascript engines <troll on>like Chrome</troll off> – edc65 – 2015-11-05T06:09:17.180

@edc65 +1 because Chrome is subpar! (Please don't lynch me internet!) But why have you opened <troll on> then closed <troll off>? – wizzwizz4 – 2015-11-05T19:32:38.020

2

CJam, 24 bytes

l:irifbe_2/{Gbc',32>&}/

Note that there is a DEL character (0x7F) between ' and ,. Try it online in the CJam interpreter.

How it works

l:i                     Read a line from STDIN and cast each char to integer. 
   ri                   Read another integer (base) from STDIN.
     fb                 Convert each integer from line 1 to that base.
       e_2/             Flatten and split into chunks of length 2.
                        If the last chunk has only one element, it will get
                        converted into a control character, which will be
                        removed later.
          {         }/  For each digit pair:
           Gb             Convert the pair from base 16 to integer.
             c            Cast to character.
              ',          Push the string of ASCII characters up to '~'.
                32>       Remove the first 32 (control characters).
                   &      Intersect.

Dennis

Posted 2015-11-04T16:27:37.627

Reputation: 196 637

What about the DEL character...? It looks like you've worked around that, but I can't see it in your explanation! – wizzwizz4 – 2015-11-05T19:29:59.543

StackExchange filters unprintable characters. The DEL character is only present in thr permalink, and invisible even there. – Dennis – 2015-11-05T19:35:09.523

I mean... Delete is a control character, but is not one of the first 32 characters. It is character number 127, 0x7F, for people who are not familiar with ASCII. – wizzwizz4 – 2015-11-08T16:35:38.267

I'm not sure I understood your question. Are you wondering how I filter it from the output? – Dennis – 2015-11-08T16:55:37.620

Yes. Your code explanation doesn't seem to say how you filter the DEL character from the output. – wizzwizz4 – 2015-11-10T19:15:08.927

@wizzwizz4 , pushes an exclusive range; '<DEL>, pushes the string of all ASCII characters with code points strictly below 127. 32> removes the remaining control characters from that string, and & intersects the character on the stack with the generated string of printable ASCII characters. For each character, this gives either a singleton or an empty string. – Dennis – 2015-11-10T19:19:22.767

Your explanation says it pushes them up to ~... :facepalm: Up to and including ~. Ok, I understand now. Thanks for taking the time to explain it. – wizzwizz4 – 2015-11-11T20:47:08.463

1

Julia, 118 bytes

f(s,n)=join(map(i->(c=string(Char(parse(Int,i,16))))^isprint(c),matchall(r"..",join(map(i->base(n,Int(i)),[s...])))))

Ungolfed:

function f(s::AbstractString, n::Integer)
    # Construct an array of ASCII codes in base n
    v = map(i -> base(n, Int(i)), [s...])

    # Join into a string and get all pairs, truncating
    # to an even length
    m = matchall(r"..", join(v))

    # Parse each pair as an integer in base 16, get the
    # character associated with that code point, convert
    # to a string, and include if it's printable
    x = map(i -> (c = string(Char(parse(Int, i, 16)))^isprint(c), m)

    # Join as a string and return
    return join(x)
end

Alex A.

Posted 2015-11-04T16:27:37.627

Reputation: 23 761

1

TeaScript, 23 bytes

TeaScript is JavaScript for golfing

£lc¡T(y©K(2)ßC(P(l,16±µ

Relatively straight-forward but delightfully short. I can probably golf down a few more characters with some more operators. A few other new features might also be able to be used to cut down some bytes.

Ungolfed && Explanation

x.l(#
    l.c().T(y)
).K(2)
.m(#
    C(
      P(l,16)
    )
).j``

Downgoat

Posted 2015-11-04T16:27:37.627

Reputation: 27 116

1I believe this is 23 chars (29 bytes) long. – Cristian Lupascu – 2015-11-05T12:10:27.603

@w0lf That would be with UTF-8 encoding but because all characters are less then 256 we can safely count them as one byte – Downgoat – 2015-11-05T15:19:19.710

1

Mathematica, 134 bytes

Print@FromCharacterCode@Select[#~FromDigits~16&/@StringPartition[""<>ToCharacterCode@InputString[]~IntegerString~Input[],2],31<#<127&]

If a function is allowed:

Mathematica, 112 bytes

FromCharacterCode@Select[#~FromDigits~16&/@StringPartition[""<>ToCharacterCode@#~IntegerString~#2,2],31<#<127&]&

LegionMammal978

Posted 2015-11-04T16:27:37.627

Reputation: 15 731

1

Ruby 92

->s,n{o=''
s.chars.map{|x|x.ord.to_s n}.join.scan(/../).map{|x|x>?2&&x<?8&&o<<x.to_i(16)}
o}

Online test here.

Cristian Lupascu

Posted 2015-11-04T16:27:37.627

Reputation: 8 369

1

Python 2, 174 bytes

def J(a,b,i=0):
 h=r=''
 B=lambda n,b:n*'x'and B(n/b,b)+chr(48+n%b+7*(n%b>9))
 for c in a:h+=B(ord(c),b)
 while i<len(h):v=int(h[i:i+2],16);r+=chr(v)*(31<v<127);i+=2
 print r

Try it here

Not really the best tool for the job. Since Python has no convert-to-arbitrary-base function, I had to implement my own. That was fun, at least--particularly finding a [marginally] shorter expression for the digits than "0123456789ABCDEF"[n%b]. For iterating over two characters at a time, I found a while loop was slightly shorter than a functional approach.

181 bytes as a full program:

B=lambda n,b:n*'x'and B(n/b,b)+chr(48+n%b+7*(n%b>9))
a=raw_input()
b=input()
h=r=''
for c in a:h+=B(ord(c),b)
i=0
while i<len(h):v=int(h[i:i+2],16);r+=chr(v)*(31<v<127);i+=2
print r

DLosc

Posted 2015-11-04T16:27:37.627

Reputation: 21 213

0

MATLAB, 103 bytes

function k(s,n),b=dec2base(s,n)';b(~cumsum(b-'0',1))='';c=base2dec(textscan(b,'%2c'),16)';char(c(c>31))

I've written a function k that takes a string s and an integer n as input. e.g.:

k('Hello, World!',6)

gives

 E001R"31$E

Most annoying thing I had to work around is leading zeros showing up when converting to base n. Getting these out of the array that was to be split after every 2nd character cost quite a lot of bytes. Not sure if it is possible to save any more bytes using this approach.

slvrbld

Posted 2015-11-04T16:27:37.627

Reputation: 619

0

PHP - 286 bytes

Put the string in $s and the integer in $b.

<?php $s=$_GET["s"];$b;$m="array_map";echo implode($m(function($v){return ctype_print($v)?$v:"";},$m("chr",$m("hexdec",str_split(strlen(implode($a=$m(function($v){global$b;return base_convert($v,16,$b);},$m("dechex",$m("ord",str_split($s))))))%2==1?substr(implode($a),0,-1):$a,2)))));?>

Pass the value to GET["s"].

undefined

Posted 2015-11-04T16:27:37.627

Reputation: 211