Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
const size_t n = s.length() / 8;
vector<uint8_t> a(n);
for(size_t block = 0; block < n; block++)
{
uint8_t acc = 0;
const size_t start = block * 8;
for(size_t offset = start; offset < start + 8; offset++)
{
acc = (acc << 1) + (s[offset] - '0');
}
a[block] = acc;
}
subroutine to_s (s, a)
use iso_fortran_env, only: int8
character (len=*), intent(in) :: s
integer (kind=int8), allocatable, intent(out), dimension(:) :: a
allocate (a(len(s)/8))
read (unit=s,fmt='(*(B8.8))') a
end subroutine to_s
Size := Length(S) div 8;
SetLength(a, Size);
for i := 0 to Size - 1 do
begin
SBin := '%' + Copy(S, 1+(i*8), 8);
Val(SBin, a[i], Err);
if (Err <> 0) then
RunError(106);
end;
my $s = '1000' . '0010' . '0101' . '1010'; # AZ
my @a;
for ( my $i = 0; $i < length $s; $i += 8) {
my @b = pack 'b8', substr($s, $i, 8);
push @a, @b;
}
n = (len(s) - 1) // 8 + 1
a = bytearray(n)
for i in range(n):
b = int(s[i * 8:(i + 1) * 8], 2)
a[i] = b
f = lambda x: int(s[x:x + 8], 2)
a = [*map(f, range(0, len(s), 8))]
a = s.scan(/[01]{8}/).map{|slice| slice.to_i(2).to_s(16).rjust(2, "0")}
let a: Vec<u8> = s.as_bytes()
.chunks(8)
.map(|chunk| unsafe {
let chunk_str = std::str::from_utf8_unchecked(chunk);
u8::from_str_radix(chunk_str, 2).unwrap_unchecked()
})
.collect();