2 added 723 characters in body
source | link

I have a binary file that is filled with FF values. I filled its start with many \000. Then, I padded its start with 10 \000, in order to get some kind of offset, and then I wrote a shorter string, also terminated with \000

I used this printf:

printf \000\000\000\000\000\000\000\000\000\000MAC_ADDRESS=12:34:56:78:90,PCB_MAIN_ID=m/SF-1V/MAIN/0.0,PCB_PIGGY1_ID=n/SF-1V/PS/0.0,CSL_HW_VARIANT=D\000' > eeprom

This is how it looks like when I show the hexdump of the file

enter image description here

Now what I want to know is how can I read the string. I can use MY_STR=${eeprom:OFFSET} (eeprom is the file name) which will give me the string, but will also give me the rest of the file which I don't want. How can I stop it when it first encounters \000?

  • Can't use MY_STR=${eeprom:OFFSET:LENGTH} because string's length is unknown
  • Another thing - how can I fill it up again with FF?
  • Using sh (busybox)

EDIT

Trying to do some minor example of this... I have one file input with this values (after xxd -c 1 input):

0000000: 68  h
0000001: 65  e
0000002: 6c  l
0000003: 6c  l
0000004: 6f  o
0000005: 2c  ,
0000006: 20
0000007: 00  .
0000008: 69  i
0000009: 74  t
000000a: 27  '
000000b: 73  s
000000c: 20
000000d: 6d  m
000000e: 65  e
000000f: 2c  ,
0000010: 00  .

and I have this script s.sh:

BUF=""
for c in $(xxd -p input); do
    if [ "${c}" != 00 ]; then
        BUF="$BUFc";
    else
        break;
    fi
done

echo $BUF

and I expected it to echo "hello", however, nothing is printed

I have a binary file that is filled with FF values. I filled its start with many \000. Then, I padded its start with 10 \000, in order to get some kind of offset, and then I wrote a shorter string, also terminated with \000

I used this printf:

printf \000\000\000\000\000\000\000\000\000\000MAC_ADDRESS=12:34:56:78:90,PCB_MAIN_ID=m/SF-1V/MAIN/0.0,PCB_PIGGY1_ID=n/SF-1V/PS/0.0,CSL_HW_VARIANT=D\000' > eeprom

This is how it looks like when I show the hexdump of the file

enter image description here

Now what I want to know is how can I read the string. I can use MY_STR=${eeprom:OFFSET} (eeprom is the file name) which will give me the string, but will also give me the rest of the file which I don't want. How can I stop it when it first encounters \000?

  • Can't use MY_STR=${eeprom:OFFSET:LENGTH} because string's length is unknown
  • Another thing - how can I fill it up again with FF?
  • Using sh (busybox)

I have a binary file that is filled with FF values. I filled its start with many \000. Then, I padded its start with 10 \000, in order to get some kind of offset, and then I wrote a shorter string, also terminated with \000

I used this printf:

printf \000\000\000\000\000\000\000\000\000\000MAC_ADDRESS=12:34:56:78:90,PCB_MAIN_ID=m/SF-1V/MAIN/0.0,PCB_PIGGY1_ID=n/SF-1V/PS/0.0,CSL_HW_VARIANT=D\000' > eeprom

This is how it looks like when I show the hexdump of the file

enter image description here

Now what I want to know is how can I read the string. I can use MY_STR=${eeprom:OFFSET} (eeprom is the file name) which will give me the string, but will also give me the rest of the file which I don't want. How can I stop it when it first encounters \000?

  • Can't use MY_STR=${eeprom:OFFSET:LENGTH} because string's length is unknown
  • Another thing - how can I fill it up again with FF?
  • Using sh (busybox)

EDIT

Trying to do some minor example of this... I have one file input with this values (after xxd -c 1 input):

0000000: 68  h
0000001: 65  e
0000002: 6c  l
0000003: 6c  l
0000004: 6f  o
0000005: 2c  ,
0000006: 20
0000007: 00  .
0000008: 69  i
0000009: 74  t
000000a: 27  '
000000b: 73  s
000000c: 20
000000d: 6d  m
000000e: 65  e
000000f: 2c  ,
0000010: 00  .

and I have this script s.sh:

BUF=""
for c in $(xxd -p input); do
    if [ "${c}" != 00 ]; then
        BUF="$BUFc";
    else
        break;
    fi
done

echo $BUF

and I expected it to echo "hello", however, nothing is printed

1
source | link

how to read a nul-terminated-string from a binary file

I have a binary file that is filled with FF values. I filled its start with many \000. Then, I padded its start with 10 \000, in order to get some kind of offset, and then I wrote a shorter string, also terminated with \000

I used this printf:

printf \000\000\000\000\000\000\000\000\000\000MAC_ADDRESS=12:34:56:78:90,PCB_MAIN_ID=m/SF-1V/MAIN/0.0,PCB_PIGGY1_ID=n/SF-1V/PS/0.0,CSL_HW_VARIANT=D\000' > eeprom

This is how it looks like when I show the hexdump of the file

enter image description here

Now what I want to know is how can I read the string. I can use MY_STR=${eeprom:OFFSET} (eeprom is the file name) which will give me the string, but will also give me the rest of the file which I don't want. How can I stop it when it first encounters \000?

  • Can't use MY_STR=${eeprom:OFFSET:LENGTH} because string's length is unknown
  • Another thing - how can I fill it up again with FF?
  • Using sh (busybox)