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.
Can AWK do this? (It is supposed to by Turing complete, isn't it?)
[...]
On 2026-03-28 20:14, 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.
This needs clarification. - Note that Awk has no 32 bit integer data
type. Usually you have text input, say "1234567", that is internally
stored in a numerically interpreted field.
Can AWK do this? (It is supposed to by Turing complete, isn't it?)
Of course that can be done. You could read in the number and apply a
sequence of modulus and division operations on the read-in number; say
awk '{n=$0; printf "%c\n", n%256; n=int(n/256); ... }'
Or, in GNU Awk, you could extract the bytes by its bit-functions,
and(), rshift(), to extract the octet parts, then print them as a
character as above.
Janis--
[...]On 2026-03-28 20:14, 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.
I see that now. But it is ugly enough that maybe I really do want to
use some other language rather than AWK.
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.
On 2026-03-28, Alan Mackenzie <acm@muc.de> 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
$
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.
Can AWK do this? (It is supposed to by Turing complete, isn't it?)
Or should I dust off my three-quarters forgotten Python and write the
program in Python (Or P***) instead?
In article <10q99ai$ph7$1@news.muc.de>, Alan Mackenzie <acm@muc.de> 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....
Can AWK do this? (It is supposed to by Turing complete, isn't it?)
Now, if I were doing this myself, I'd write a short GAWK extension library >function that wraps the "fwrite" function, then use that. The gist of it
is that the extension lib would do:
fwrite(&num,4,1,stdout);
I would do it this way because I like AWK/GAWK a lot and would want to >continue using it and have no interest in learning any of those other >languages. You may or may not agree with me on this.
Kaz Kylheku <046-301-5902@kylheku.com> writes:
On 2026-03-28, Alan Mackenzie <acm@muc.de> 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
$
| Sysop: | DaiTengu |
|---|---|
| Location: | Appleton, WI |
| Users: | 1,113 |
| Nodes: | 10 (0 / 10) |
| Uptime: | 492335:45:12 |
| Calls: | 14,238 |
| Files: | 186,312 |
| D/L today: |
3,561 files (1,160M bytes) |
| Messages: | 2,514,865 |