9
2
Task
Given an input string of one or more ASCII characters which codepoints are between 0 and 128 (exclusive), do the following:
- Convert each character into its 7-bit ASCII code (if the ASCII code is less than 7 bits, put leading zero bits)
- Concatenate all bits (this results in
7*n
bits wheren
is the number of characters) - For each bit in this bitstream, print 1 if it is different from the previous bit, and print 0 otherwise. The first output bit is always 1.
Example
Input:
Hi
Output:
11011001011101
Explanation:
The string "Hi" has the ASCII codes
72 105
which in bits are:
1001000 1101001
And the transition bits indicators:
11011001011101
This is code golf. Lowest byte count wins.
Test Cases
Test case 1:
Hello World!
110110010101110011010101101010110001110000111110000110000001011101101010101100110001
Test case 2:
%% COMMENT %%
1110111111011111100001100010010100001010110101011010011101010011111110011000001101111110111
Test case 3 (credit to Luis Mendo):
##
11100101110010
Congrats to Luis Mendo for the shortest solution with 9 bytes in MATL!
I'm confused - don't you mean
1
if the bit is different than the previous one? – Chas Brown – 2019-07-10T03:10:13.680Oops, yeah, sorry, fixed that. Error after editing. Thanks @ChasBrown – justhalf – 2019-07-10T03:26:55.503
There are 7! Way at last concatenate 2 7 bits string, and you not show either in example which one – RosLuP – 2019-07-10T08:42:00.157
2Suggested test case
##
(leading0
bit; some answers currently fail because of that) – Luis Mendo – 2019-07-10T09:25:28.2104How is this a duplicate of the Manchester encoding challenge? Am I missing something? – gastropner – 2019-07-10T10:25:56.737
2The other challenge says converting an input stream of bits into a double-rate output stream, with each input '1' translated to a '01' and each input '0' translated to a '10'. So not dupe in my opinion. If a large number of people upvote @gastropner's comment above I can un-dupe (or any other user with that ability) – Luis Mendo – 2019-07-10T11:24:33.733
1@Shaggy: Both test cases include a space, which has only a single bit set, and not the 7th. So I don't think the problem statement is guaranteeing that each ascii code will be exactly 7 bits in length. – recursive – 2019-07-10T15:05:55.723
@Shaggy, yes, I perhaps should clarify that if the ascii code is less than 7-bit it should be prepended with leading zeros. And, like recursive said, the space character already has less than 7 bits. – justhalf – 2019-07-10T16:26:54.507
@LuisMendo, the
%
character (second test case) also starts with 0. – justhalf – 2019-07-10T16:58:27.217@justhalf In some languages, if at least on character has all 7 bits (i.e. starts with 1) then all characters in the string are converted to 7 bits, even if fewer bytes would suffice for some characters. Consider this example in Octave, and compare with this, which only produces 6 bits. My test case was aimed to catch that sort of mistake
– Luis Mendo – 2019-07-10T17:05:35.453Ah, ok. I am not familiar with that. Thanks for catching that! I'll add the third test case. – justhalf – 2019-07-10T17:06:42.707
Do I need to support a string of length 0? Also, I can save a few bytes if it is guaranteed that for characters wider than 7-bits, all bits above the 7th are 0 - I'll assume that's the case unless you say otherwise – SmileAndNod – 2019-07-17T01:31:12.707
Bits above the 7th are all 0 -> yes, since the maximum code point that I would like to test is 127 (less than 128), as given in the description. String of length 0 I haven't really considered, but I guess it should be supported. – justhalf – 2019-07-17T12:29:21.157
Does the code have to support embedded zero bytes? (for example, C uses zero bytes as string terminator). – celtschk – 2019-07-17T21:20:30.417
It's ok to not handle null bytes. – justhalf – 2019-07-17T21:46:15.070
1@SmileAndNod On second thought, I think you don't need to handle empty string. – justhalf – 2019-07-17T21:47:44.403