取一串用空格分隔的二进制字符,然后将其转换为ASCII字符串。
例如...
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
将转换为...
Hello World
二进制字符串将存储在名为的变量中s。
这是一个代码高尔夫挑战,因此最短的解决方案将获胜。
ZX81在这里。
取一串用空格分隔的二进制字符,然后将其转换为ASCII字符串。
例如...
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
将转换为...
Hello World
二进制字符串将存储在名为的变量中s。
这是一个代码高尔夫挑战,因此最短的解决方案将获胜。
ZX81在这里。
Answers:
s.split.map{|x|x.to_i(2).chr}*""
s.gsub(/\w+ ?/){$&.to_i(2).chr}
借助Chron进行优化
.join""为*""节省几个大字。您也可以使用gsub代替split + map + join,例如:s.gsub(/\w+ ?/){$&.to_i(2).chr}(31个字符)。
s.gsub(/\d+./){$&.to_i(2).chr}工作,它是30个字符,我不知道为什么它能工作。本.不应该匹配的最后时间,但它的作用。
s.replace(/\d+./g,x=>String.fromCharCode('0b'+x))
编辑接受@bebe建议-感谢
Edit2不了解二进制数字文字-感谢@kapep
Edit3哇6 没保存4个字节@ETHproductions
注释中要求的解释
String.replace 可以接受2个参数:
/\d+./g:一位或多位数字,后跟一个不同的字符-g标志指定多次搜索模式值得注意的是,字符串末尾的regexp匹配数字序列,没有尾随空格,在这种情况下,点与最后一位数字匹配。尝试'123'.match(/(\ d +)./)进行验证。
(仍)是JavaScript较冗长的片段之一...
(未分配给字符串s)
var s='1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'
var x=
s.replace(/\d+./g,x=>String.fromCharCode('0b'+x))
console.log(x)
s.replace(/\d+./g,x=>String.fromCharCode(parseInt(x,2)))56
s.replace(/\d+./g,x=>String.fromCharCode(eval("0b"+x)))55
/\d+./g,x=>吗?
eval('0b'+x),以'0b'+x-0节省4个字节。
dc<<<2i$s[Pz0\<m]dsmx|rev
m以:
m如果堆栈为非空,则比较并调用宏m注册因为我们首先将整个二进制字符串压入堆栈,所以当我们弹出每个值时,最终将字符串反转。因此,我们使用该rev实用程序来纠正此问题。
$ s="1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
$ dc<<<2i$s[Pz0\<m]dsmx|rev
Hello World
$
38字节版本:
for(int*x=s;putchar(strtol(x,&x,2)););
如果s是一个指针,则只有31个字节:
while(putchar(strtol(s,&s,2)));
我认为这不是应该使用for和while循环的方式...但是它有效。
smCv+"0b"dPZ
请注意,s不是Pyth中的合法变量,因此我改用Z。
说明:
print(
s sum(
m map(lambda d:
C chr(
v eval(
+"0b"d "0b"+d)),
P split(
Z Z))))
例:
=Z"<the binary string from above>"smCv+"0b"dPZ
Hello World
id2代替v+"0b"d?无论如何,这既不可读又很酷。
00000000 30 d2 b4 08 cd 21 2c 30 72 06 d0 e2 08 c2 eb f2 |0....!,0r.......|
00000010 b4 02 cd 21 eb ea |...!..|
由于机器代码中没有真正的字符串变量(特别是没有名为“ s”的变量),因此我选择stdin作为输入。
NASM输入:
org 100h
section .text
start:
xor dl,dl
loop:
mov ah,8
int 21h
sub al,'0'
jb print
shl dl,1
or dl,al
jmp loop
print:
mov ah,2
int 21h
jmp start
-join(-split$s|%{[char][convert]::ToInt16($_,2)})
$ s中的二进制字符串的简单循环。但是必须包含[convert]会杀死我的分数。
编辑:真的只有一种方法可以在Powershell中实现这一目标,哇。Joey和我两个人在独立工作时都得到了几乎相同的答案!
输入:
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
输出:
Hello World
u:;(#.@:("."0))&.>cut s
测试:
s=:'1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'
u:;(#.@:("."0))&.>cut s
Hello World
说明:
cut s NB. split S on spaces
( )&.> NB. for each element
("."0) NB. evaluate each character
@: NB. and
#. NB. convert bitstring to number
; NB. unbox each number
u: NB. convert to ASCII
⎕UCS{2⊥⍎¨⍕⍵}¨⍎s
测试:
s←'1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'
⎕UCS{2⊥⍎¨⍕⍵}¨⍎s
Hello World
说明:
⍎s:评价s,把它变成一个整数数组。数组被写为以空格分隔的数字,因此这会分裂s。{... }¨:对于每个元素:
⍕⍵:将数字转回字符串⍎¨:评估每个数字,给出一个位串2⊥:以2为底的解码,给出数字⎕UCS:获取每个数字的字符<?=join(array_map('chr',array_map('bindec',explode(' ',$s))))
foreach(str_split($s,8)as$v)echo chr(bindec($v));
str_split($s,8)无法正常工作。foreach(explode(' ',$s)as$v)echo chr(bindec($v));会是有效的,但我不打算编辑我的第一个PPGC答案,而答案显然显然不是真的。还是很感谢你!
由于Brainfuck中没有变量,因此我只使用标准输入和输出。
32+解释器应将代码解释为32 +s。如果您的解释器不支持RLE,只需手动更换它们。
>,[32->+<[16-<[>++<-]>[<+>-]>-<]>[<<.[-]>>-]<,]<.
扩展(非RLE)版本:(91字节)
>,[-------------------------------->+<[----------------<[>++<-]>[<+>-]>-<]>[<<.[-]>>-]<,]<.
该代码假定EOF编码为0。
使用以下布局:
+---+---+------+
| x | a | flag |
+---+---+------+
其中x是要打印的ASCII字节,a是来自标准输入的字符,flag如果a为空格则为1 。
>, Read a character a into the second cell
[ While not EOF:
32- Decrease a by 32 (a -= ' ')
>+< Set the flag to 1
[ If a was not a space:
16- Decrease by 16 more ('0' == 32+16)
<[>++<-] a += 2*x
>[<+>-] Move it back (x = a)
>-< Reset the flag, it was not a space.
]>
[ If a was a space (flag == 1):
<<.[-] Print and reset x
>>- Reset the flag
]
<, Read the next caracter a
]
<. Print the last character x
在Java 8中使用lambda(75字节):
Arrays.stream(s.split(" ")).reduce("",(a,b)->a+(char)Byte.parseByte(b,2));
如果允许静态导入(此处使用了某些静态导入),则为(61字节):
stream(s.split(" ")).reduce("",(a,b)->a+(char)parseInt(b,2))
使用for循环的简短版本(60字节):
for(String n:s.split(" ")){out.print((char)parseInt(n,2));}
全Clojure隐含:
(apply str(map #(char(read-string(str"2r"%)))(re-seq #"\d+"s)))
与Java互操作:
(apply str(map #(char(Long/parseLong % 2))(.split s" ")))
REPL会议:
golf> (def s "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100")
#'golf/s
golf> (apply str(map #(char(read-string(str"2r"%)))(re-seq #"\d+"s)))
"Hello World"
golf> (apply str(map #(char(Long/parseLong % 2))(.split s" ")))
"Hello World"
s$=s$+" ":FOR i=1 TO LEN(s$):c$=MID$(s$,i,1):IF c$=" "THEN n=0:r$=r$+CHR$(n)ELSE n=n*2+VAL(c$)
NEXT:?r$
什么?我们这里没有花哨的二进制到十进制函数。自己做!
我在此meta post上将换行符(我认为这对于获取不带if-then-else而不是END IF)是一个字节。我不知道Windows上的QB64是否会以这种方式接受代码文件。大概没什么大不了的。
NodeJS – 62
Buffer(s.split(' ').map(function(a){return parseInt(a,2)}))+''
PHP – 75
array_reduce(explode(' ', $b),function($a,$b){return $a.chr(bindec($b));});
这将执行数字转换而无需parseInt或eval。向后读取字符串并计数位,如果它是1,则将其设置为位x。找到空格后,该数字将转换为char并开始使用新的0数来设置位。
x=n=0,w='',s=' '+s
for(i=s.length;i--;){m=s[i]
if(m==1)n|=1<<x
x++
if(m==' ')w=String.fromCharCode(n)+w,n=x=0
}
NS/{:~2bc}/
小号不CJam一个合法的变量名,所以我选择ñ代替。
$ cjam <(echo '
> "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
> :N;
> NS/{:~2bc}/
> '); echo
Hello World
NS/ " Split N at spaces. ";
{ }/ " For each chunk: ";
:~ " Evaluate each character ('0' ↦ 0, '1' ↦ 1). ";
2b " Convert from base 2 array to integer. ";
c " Cast to character. ";
这是我在Haskell的第一次高尔夫尝试。
map(chr.foldl1((+).(*2)).map digitToInt)$words s
您需要导入Data.Char
用法(以ghci为单位):
Prelude> :m +Data.Char
Prelude Data.Char> let s = "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
Prelude Data.Char> map(chr.foldl1((+).(*2)).map digitToInt)$words s
"Hello World"
说明:
map(chr.foldl1((+).(*2)).map digitToInt)$words s
$words s -- split s on spaces into a list
map digitToInt -- convert each digit in input string to int
((+).(*2)) -- a function that multiplies its first
-- argument by 2, then adds the second argument
foldl1((+).(*2)).map digitToInt -- fold the above over the list of ints:
-- in other words this is a function that reads strings as binary and gives the value as int
(chr.foldl1((+).(*2)).map digitToInt) -- cast to character
map(chr.foldl1((+).(*2)).map digitToInt)$words s -- map our function over the list of words