Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.awk Subject: Re: Can I output a binary file from an AWK program? Date: Sun, 29 Mar 2026 17:07:30 -0700 Organization: None to speak of Lines: 38 Message-ID: <87a4vquqm5.fsf@example.invalid> References: <10q99ai$ph7$1@news.muc.de> <20260329163603.959@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 30 Mar 2026 00:07:31 +0000 (UTC) Injection-Info: dont-email.me; posting-host="af1e4d03e5d4687ab81b254102748b94"; logging-data="2102851"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1844HGx9HmZ54hPi38QW4O7" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:2KdhBOy/ROCInVvC9q3H/pQViVQ= sha1:k1og98UYrlQwNOFZXEGMl9WzQzo= Xref: csiph.com comp.lang.awk:10066 Kaz Kylheku <046-301-5902@kylheku.com> writes: > On 2026-03-28, Alan Mackenzie wrote: >> I think the Subject: line says it all. I have a text source file to >> convert into a binary output file. For example, I want to be able to >> output a 32-bit integer as a four byte little-endian binary integer. > > Firstly, Awk's printf has a %c specifier which will output any byte: > > $ awk 'BEGIN { printf("%c%c", 0x41, 0x0A) }' > A > $ [...] The behavior seems to depend on the current locale. I haven't investigated it thoroughly. I don't know whether there's a way to force binary output. For example, the cent sign '¢' is U+00a2, represented in UTF-8 as the two-byte sequence 0xc2, 0xa2. I have LANG=en_US.UTF-8 in my environment. $ gawk --version | head -n 1 GNU Awk 5.2.1, API 3.2, PMA Avon 8-g1, (GNU MPFR 4.2.1, GNU MP 6.3.0) $ gawk 'BEGIN { printf("%c\n", 0xa2) }' ¢ $ LANG=C gawk 'BEGIN { printf("%c\n", 0xa2) }' | hd 00000000 a2 0a |..| 00000002 $ (I filtered the last through hd because the output is not valid UTF-8.) nawk behaves similarly. mawk and busybox awk do not. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */