Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: K&R, 2nd edition, Brian's concerns with ``char c = EOF''
Date: Wed, 15 Dec 2021 00:31:35 -0800
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <86zgp246c8.fsf@linuxsc.com>
References: <86pmqwkm7u.fsf@levado.to> <4fcda596-2e00-4dd6-bf8f-4616218f4398n@googlegroups.com> <91a56855-b917-45a2-89be-7e2ed806d357n@googlegroups.com> <87a6hac0oj.fsf@nosuchdomain.example.com> <11efb602-293a-4a90-b0bd-832617377f57n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="87e29f815fda23c6ea6c6dd22211e8ad"; logging-data="12712"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CR/wgm8/9xqsMWQcX60yzTFZNyDYGSxA="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:n1kBdS28xUFDGzBEkvk40BoRZes= sha1:HTZp30mQxWauR/vhkAHdidhQ204=
Xref: csiph.com comp.lang.c:163841
Dave Dunfield writes:
> [...]
For future reference here is a short C program that does
base64 encoding.
/*** b64e.c - base64 encode stdin to stdout. */
/*** NOTE: This program assumes characters are 8 bits. */
#include
static char A[] = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
};
int
main( void ){
int c;
unsigned long b = 0, n = 0, k = 0;
char out[ 73 ];
while( c = getchar(), c != EOF ){
b = (b<<8) + c, n += 8;
do {
if( k > 71 ) out[k] = 0, puts( out ), k = 0;
n -= 6;
out[ k++ ] = A[ b >>n &63 ];
} while( n > 5 );
}
if( n > 0 ) out[ k++ ] = A[ b <<(6-n) &63 ];
if( k > 0 ) out[k] = 0, puts( out ), k = 0;
else puts( "====" );
return 0;
}