How to Write Integer to Binary File Using Bash

How to write integer to binary file using Bash?

This is what I could come up with:

int=65534
printf "0: %.8x" $int | xxd -r -g0 >>file

Now depending on endianness you might want to swap the byte order:

printf "0: %.8x" $int | sed -E 's/0: (..)(..)(..)(..)/0: \4\3\2\1/' | xxd -r -g0 >>file

Example (decoded, so it's visible):

printf "0: %.8x" 65534 | sed -E 's/0: (..)(..)(..)(..)/0: \4\3\2\1/' | xxd -r -g0 | xxd
0000000: feff 0000 ....

This is for unsigned int, if the int is signed and the value is negative you have to compute the two's complement. Simple math.

How to write a number on a binary file with bash

Perl to the rescue:

perl -we 'print pack "N", shift' 224128

To check the output, you can use e.g. xxd:

$ perl -we 'print pack "N", shift' 224128 | xxd
00000000: 0003 6b80 ..k.

And indeed,

$ echo $((16#36b80))
224128

If you need different endianness, check pack.

using bash: write bit representation of integer to file

You can use echo to emit specific bytes using hex or octal. For example:

echo -n -e \\x30 

will print ascii 0 (0x30)

(-n remove trailing newline)

Is it possible to write the binary representation of a number into a file using bash sript?

Try

num=176

printf -v oct0 '%03o' "$(( (num>>24) & 0xff ))"
printf -v oct1 '%03o' "$(( (num>>16) & 0xff ))"
printf -v oct2 '%03o' "$(( (num>> 8) & 0xff ))"
printf -v oct3 '%03o' "$(( num & 0xff ))"

printf "\\$oct0\\$oct1\\$oct2\\$oct3" >package.bin
  • You can set num to any 32-bit positive integer value.
  • See BashFAQ/071 (How do I convert an ASCII character to its decimal (or hexadecimal) value and back?) for details of how the conversion is done.

How to write binary data in Bash

You can write arbitrary bytes in hex or octal with:

printf '\x03' > file   # Hex
printf '\003' > file # Octal

If you have binary, it's a bit tricker, but you can turn it into octal with:

printf '%o\n' "$((2#00000011))"

which of course can be nested in the above:

binary=00000011
printf "\\$(printf '%o' "$((2#$binary))")" > file

Note that this only works with up to 8 bits. If you want to write longer values, you have to split it up into groups of 8.

How to write a binary file using Bash?

you can use the following command:

echo -n -e \\x48\\x00\\x49\\x00 > myfile

Get numerical value of integer in binary file header

You can use this to look at that integer after 80 bytes of data

cat test_above2GB_fails.stl | dd ibs=1 skip=80 2>/dev/null | od -tu4

ddis used to skip first 80 bytes. od -td4 means to format input as 4-bytes unsigned decimal. You can use od -tx1 to view in hex mode

or without using cat and read only 4 bytes from 80 to 84

dd if=test_above2GB_fails.stl ibs=1 skip=80 count=4 2>/dev/null | od -tu4

fwrite() not working to write integer in binary file

The first parameter of fwrite expects a pointer.

Lines such as the following:

fwrite(L->PAGES, sizeof(int), 1, arq);

Should be written as follows:

fwrite(&(L->PAGES), sizeof(int), 1, arq);

Sames goes for YEAR and PRICE members of that struct

fwrite(&(L->YEAR), sizeof(int), 1, arq);
...
fwrite(&(L->PRICE), sizeof(float), 1, arq);

Note, you don't need to make the same change for TITLE, PUBLISHER, and AUTHOR because the type of those member fields are already pointers (char*).

Writing integer values to a file in binary using C

You have to write a minimum of two bytes to store a 9-bits value. An easy solution is to use 16 bits per 9 bits value

Choose a 16 bits unsigned type, eg uint16_t and store the 2 bytes

 uint16_t w = 275;
fwrite(&w, 1, 2, myfilep);

Reading the word w, ensure it actually uses only its 9 first bits (bits 0~8)

 w &= 0x1FF;

Note that you might have endianness issues if you read the file on another system that doesn't have the same endianness as the system that wrote the word.

You could also optimize that solution using 9 bits of a 16 bits word, then using the remaining 7 bits to store the first 7 bits of the next 9 bits value etc...

See this answer that explains how to work with bit shifting in C.



Related Topics



Leave a reply



Submit