How to loop through character in string and still detect null char in Bash












2















I have this function:



function convert_ascii_string_to_decimal {
ascii=$1
unset converted_result

while IFS="" read -r -n 1 char; do
decimal=$(printf '%d' "'$char")
echo $decimal
converted_result="$converted_result $decimal"
done < <(printf %s "$ascii")
converted_result=$(echo $converted_result | xargs) #strip leading and trailing
}


It is meant to take an ascii string variable, loop through every character, and concatenate the ascii decimal representation to a string. However, this while loop seems to ignore null chars, ie characters with ascii 0. I want to be able to read every single ascii there is, including null.










share|improve this question



























    2















    I have this function:



    function convert_ascii_string_to_decimal {
    ascii=$1
    unset converted_result

    while IFS="" read -r -n 1 char; do
    decimal=$(printf '%d' "'$char")
    echo $decimal
    converted_result="$converted_result $decimal"
    done < <(printf %s "$ascii")
    converted_result=$(echo $converted_result | xargs) #strip leading and trailing
    }


    It is meant to take an ascii string variable, loop through every character, and concatenate the ascii decimal representation to a string. However, this while loop seems to ignore null chars, ie characters with ascii 0. I want to be able to read every single ascii there is, including null.










    share|improve this question

























      2












      2








      2


      1






      I have this function:



      function convert_ascii_string_to_decimal {
      ascii=$1
      unset converted_result

      while IFS="" read -r -n 1 char; do
      decimal=$(printf '%d' "'$char")
      echo $decimal
      converted_result="$converted_result $decimal"
      done < <(printf %s "$ascii")
      converted_result=$(echo $converted_result | xargs) #strip leading and trailing
      }


      It is meant to take an ascii string variable, loop through every character, and concatenate the ascii decimal representation to a string. However, this while loop seems to ignore null chars, ie characters with ascii 0. I want to be able to read every single ascii there is, including null.










      share|improve this question














      I have this function:



      function convert_ascii_string_to_decimal {
      ascii=$1
      unset converted_result

      while IFS="" read -r -n 1 char; do
      decimal=$(printf '%d' "'$char")
      echo $decimal
      converted_result="$converted_result $decimal"
      done < <(printf %s "$ascii")
      converted_result=$(echo $converted_result | xargs) #strip leading and trailing
      }


      It is meant to take an ascii string variable, loop through every character, and concatenate the ascii decimal representation to a string. However, this while loop seems to ignore null chars, ie characters with ascii 0. I want to be able to read every single ascii there is, including null.







      bash ascii






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 16:18









      Daily MemesDaily Memes

      112




      112
























          2 Answers
          2






          active

          oldest

          votes


















          2














          To get all characters of a string as decimal number, you can use hexdump to parse a string:



           echo -e "hello x00world" | hexdump -v -e '1/1 "%d "'
          104 101 108 108 111 32 0 119 111 114 108 100 10


          This also works for parsing a file:



          echo '05 04 03 02 01 00 ff' | xxd -r -ps  > file
          hexdump --no-squeezing --format '1/1 "%d "' file
          5 4 3 2 1 0 255


          hexdump explanation:




          • options -v and --no-squeezing prints all bytes (without skipping duplicated bytes)

          • options -e and --format allows giving a specific format

          • format is 1/1 "%d " which means


            • Iteration count = 1 (process the byte only once)

            • Byte count = 1 (apply this format for each byte)

            • Format = "%d" (convert to decimal)








          share|improve this answer


























          • your first solution gives this error: hexdump: illegal option -- - usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...] hd [-bcdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]

            – Daily Memes
            Nov 13 '18 at 17:10











          • @DailyMemes I updated the answer with using short (instead of long) option.

            – oliv
            Nov 13 '18 at 17:35











          • @olive.. could you pls add explanation

            – stack0114106
            Nov 14 '18 at 6:50











          • @stack0114106 What isn't clear exactly?

            – oliv
            Nov 14 '18 at 7:20











          • the options -v-e and the format 1/1.. why specifically 1/1

            – stack0114106
            Nov 14 '18 at 8:44



















          1














          You can't store the null character in a bash variable, which is happening in your script with the $char variable.



          I suggest using xxd instead of writing your own script:



          echo -ne "some ascii text" | xxd -p


          If we echo a null charcter:



          $ echo -ne "" | xxd -p
          00





          share|improve this answer
























          • can this be done in decimal intsead of hex?

            – Daily Memes
            Nov 13 '18 at 17:22











          • also the problem with this is if there is no stdout, it still reads it as null char. Is it possible to detect whether there was an stdout? or is the lack of stdout represented by null?

            – Daily Memes
            Nov 13 '18 at 17:29











          • @DailyMemes I don't understand your question about what happens "if there is no stdout". In that case, I get nothing, not a null. Try cat /dev/null | xxd -p -- that gives me no output at all. Same with @oliv's hexdump answer, BTW.

            – Gordon Davisson
            Nov 13 '18 at 18:36













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285210%2fhow-to-loop-through-character-in-string-and-still-detect-null-char-in-bash%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          To get all characters of a string as decimal number, you can use hexdump to parse a string:



           echo -e "hello x00world" | hexdump -v -e '1/1 "%d "'
          104 101 108 108 111 32 0 119 111 114 108 100 10


          This also works for parsing a file:



          echo '05 04 03 02 01 00 ff' | xxd -r -ps  > file
          hexdump --no-squeezing --format '1/1 "%d "' file
          5 4 3 2 1 0 255


          hexdump explanation:




          • options -v and --no-squeezing prints all bytes (without skipping duplicated bytes)

          • options -e and --format allows giving a specific format

          • format is 1/1 "%d " which means


            • Iteration count = 1 (process the byte only once)

            • Byte count = 1 (apply this format for each byte)

            • Format = "%d" (convert to decimal)








          share|improve this answer


























          • your first solution gives this error: hexdump: illegal option -- - usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...] hd [-bcdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]

            – Daily Memes
            Nov 13 '18 at 17:10











          • @DailyMemes I updated the answer with using short (instead of long) option.

            – oliv
            Nov 13 '18 at 17:35











          • @olive.. could you pls add explanation

            – stack0114106
            Nov 14 '18 at 6:50











          • @stack0114106 What isn't clear exactly?

            – oliv
            Nov 14 '18 at 7:20











          • the options -v-e and the format 1/1.. why specifically 1/1

            – stack0114106
            Nov 14 '18 at 8:44
















          2














          To get all characters of a string as decimal number, you can use hexdump to parse a string:



           echo -e "hello x00world" | hexdump -v -e '1/1 "%d "'
          104 101 108 108 111 32 0 119 111 114 108 100 10


          This also works for parsing a file:



          echo '05 04 03 02 01 00 ff' | xxd -r -ps  > file
          hexdump --no-squeezing --format '1/1 "%d "' file
          5 4 3 2 1 0 255


          hexdump explanation:




          • options -v and --no-squeezing prints all bytes (without skipping duplicated bytes)

          • options -e and --format allows giving a specific format

          • format is 1/1 "%d " which means


            • Iteration count = 1 (process the byte only once)

            • Byte count = 1 (apply this format for each byte)

            • Format = "%d" (convert to decimal)








          share|improve this answer


























          • your first solution gives this error: hexdump: illegal option -- - usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...] hd [-bcdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]

            – Daily Memes
            Nov 13 '18 at 17:10











          • @DailyMemes I updated the answer with using short (instead of long) option.

            – oliv
            Nov 13 '18 at 17:35











          • @olive.. could you pls add explanation

            – stack0114106
            Nov 14 '18 at 6:50











          • @stack0114106 What isn't clear exactly?

            – oliv
            Nov 14 '18 at 7:20











          • the options -v-e and the format 1/1.. why specifically 1/1

            – stack0114106
            Nov 14 '18 at 8:44














          2












          2








          2







          To get all characters of a string as decimal number, you can use hexdump to parse a string:



           echo -e "hello x00world" | hexdump -v -e '1/1 "%d "'
          104 101 108 108 111 32 0 119 111 114 108 100 10


          This also works for parsing a file:



          echo '05 04 03 02 01 00 ff' | xxd -r -ps  > file
          hexdump --no-squeezing --format '1/1 "%d "' file
          5 4 3 2 1 0 255


          hexdump explanation:




          • options -v and --no-squeezing prints all bytes (without skipping duplicated bytes)

          • options -e and --format allows giving a specific format

          • format is 1/1 "%d " which means


            • Iteration count = 1 (process the byte only once)

            • Byte count = 1 (apply this format for each byte)

            • Format = "%d" (convert to decimal)








          share|improve this answer















          To get all characters of a string as decimal number, you can use hexdump to parse a string:



           echo -e "hello x00world" | hexdump -v -e '1/1 "%d "'
          104 101 108 108 111 32 0 119 111 114 108 100 10


          This also works for parsing a file:



          echo '05 04 03 02 01 00 ff' | xxd -r -ps  > file
          hexdump --no-squeezing --format '1/1 "%d "' file
          5 4 3 2 1 0 255


          hexdump explanation:




          • options -v and --no-squeezing prints all bytes (without skipping duplicated bytes)

          • options -e and --format allows giving a specific format

          • format is 1/1 "%d " which means


            • Iteration count = 1 (process the byte only once)

            • Byte count = 1 (apply this format for each byte)

            • Format = "%d" (convert to decimal)









          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 8:56

























          answered Nov 13 '18 at 17:00









          olivoliv

          8,2911130




          8,2911130













          • your first solution gives this error: hexdump: illegal option -- - usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...] hd [-bcdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]

            – Daily Memes
            Nov 13 '18 at 17:10











          • @DailyMemes I updated the answer with using short (instead of long) option.

            – oliv
            Nov 13 '18 at 17:35











          • @olive.. could you pls add explanation

            – stack0114106
            Nov 14 '18 at 6:50











          • @stack0114106 What isn't clear exactly?

            – oliv
            Nov 14 '18 at 7:20











          • the options -v-e and the format 1/1.. why specifically 1/1

            – stack0114106
            Nov 14 '18 at 8:44



















          • your first solution gives this error: hexdump: illegal option -- - usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...] hd [-bcdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]

            – Daily Memes
            Nov 13 '18 at 17:10











          • @DailyMemes I updated the answer with using short (instead of long) option.

            – oliv
            Nov 13 '18 at 17:35











          • @olive.. could you pls add explanation

            – stack0114106
            Nov 14 '18 at 6:50











          • @stack0114106 What isn't clear exactly?

            – oliv
            Nov 14 '18 at 7:20











          • the options -v-e and the format 1/1.. why specifically 1/1

            – stack0114106
            Nov 14 '18 at 8:44

















          your first solution gives this error: hexdump: illegal option -- - usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...] hd [-bcdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]

          – Daily Memes
          Nov 13 '18 at 17:10





          your first solution gives this error: hexdump: illegal option -- - usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...] hd [-bcdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]

          – Daily Memes
          Nov 13 '18 at 17:10













          @DailyMemes I updated the answer with using short (instead of long) option.

          – oliv
          Nov 13 '18 at 17:35





          @DailyMemes I updated the answer with using short (instead of long) option.

          – oliv
          Nov 13 '18 at 17:35













          @olive.. could you pls add explanation

          – stack0114106
          Nov 14 '18 at 6:50





          @olive.. could you pls add explanation

          – stack0114106
          Nov 14 '18 at 6:50













          @stack0114106 What isn't clear exactly?

          – oliv
          Nov 14 '18 at 7:20





          @stack0114106 What isn't clear exactly?

          – oliv
          Nov 14 '18 at 7:20













          the options -v-e and the format 1/1.. why specifically 1/1

          – stack0114106
          Nov 14 '18 at 8:44





          the options -v-e and the format 1/1.. why specifically 1/1

          – stack0114106
          Nov 14 '18 at 8:44













          1














          You can't store the null character in a bash variable, which is happening in your script with the $char variable.



          I suggest using xxd instead of writing your own script:



          echo -ne "some ascii text" | xxd -p


          If we echo a null charcter:



          $ echo -ne "" | xxd -p
          00





          share|improve this answer
























          • can this be done in decimal intsead of hex?

            – Daily Memes
            Nov 13 '18 at 17:22











          • also the problem with this is if there is no stdout, it still reads it as null char. Is it possible to detect whether there was an stdout? or is the lack of stdout represented by null?

            – Daily Memes
            Nov 13 '18 at 17:29











          • @DailyMemes I don't understand your question about what happens "if there is no stdout". In that case, I get nothing, not a null. Try cat /dev/null | xxd -p -- that gives me no output at all. Same with @oliv's hexdump answer, BTW.

            – Gordon Davisson
            Nov 13 '18 at 18:36


















          1














          You can't store the null character in a bash variable, which is happening in your script with the $char variable.



          I suggest using xxd instead of writing your own script:



          echo -ne "some ascii text" | xxd -p


          If we echo a null charcter:



          $ echo -ne "" | xxd -p
          00





          share|improve this answer
























          • can this be done in decimal intsead of hex?

            – Daily Memes
            Nov 13 '18 at 17:22











          • also the problem with this is if there is no stdout, it still reads it as null char. Is it possible to detect whether there was an stdout? or is the lack of stdout represented by null?

            – Daily Memes
            Nov 13 '18 at 17:29











          • @DailyMemes I don't understand your question about what happens "if there is no stdout". In that case, I get nothing, not a null. Try cat /dev/null | xxd -p -- that gives me no output at all. Same with @oliv's hexdump answer, BTW.

            – Gordon Davisson
            Nov 13 '18 at 18:36
















          1












          1








          1







          You can't store the null character in a bash variable, which is happening in your script with the $char variable.



          I suggest using xxd instead of writing your own script:



          echo -ne "some ascii text" | xxd -p


          If we echo a null charcter:



          $ echo -ne "" | xxd -p
          00





          share|improve this answer













          You can't store the null character in a bash variable, which is happening in your script with the $char variable.



          I suggest using xxd instead of writing your own script:



          echo -ne "some ascii text" | xxd -p


          If we echo a null charcter:



          $ echo -ne "" | xxd -p
          00






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 16:49









          maxmax

          360219




          360219













          • can this be done in decimal intsead of hex?

            – Daily Memes
            Nov 13 '18 at 17:22











          • also the problem with this is if there is no stdout, it still reads it as null char. Is it possible to detect whether there was an stdout? or is the lack of stdout represented by null?

            – Daily Memes
            Nov 13 '18 at 17:29











          • @DailyMemes I don't understand your question about what happens "if there is no stdout". In that case, I get nothing, not a null. Try cat /dev/null | xxd -p -- that gives me no output at all. Same with @oliv's hexdump answer, BTW.

            – Gordon Davisson
            Nov 13 '18 at 18:36





















          • can this be done in decimal intsead of hex?

            – Daily Memes
            Nov 13 '18 at 17:22











          • also the problem with this is if there is no stdout, it still reads it as null char. Is it possible to detect whether there was an stdout? or is the lack of stdout represented by null?

            – Daily Memes
            Nov 13 '18 at 17:29











          • @DailyMemes I don't understand your question about what happens "if there is no stdout". In that case, I get nothing, not a null. Try cat /dev/null | xxd -p -- that gives me no output at all. Same with @oliv's hexdump answer, BTW.

            – Gordon Davisson
            Nov 13 '18 at 18:36



















          can this be done in decimal intsead of hex?

          – Daily Memes
          Nov 13 '18 at 17:22





          can this be done in decimal intsead of hex?

          – Daily Memes
          Nov 13 '18 at 17:22













          also the problem with this is if there is no stdout, it still reads it as null char. Is it possible to detect whether there was an stdout? or is the lack of stdout represented by null?

          – Daily Memes
          Nov 13 '18 at 17:29





          also the problem with this is if there is no stdout, it still reads it as null char. Is it possible to detect whether there was an stdout? or is the lack of stdout represented by null?

          – Daily Memes
          Nov 13 '18 at 17:29













          @DailyMemes I don't understand your question about what happens "if there is no stdout". In that case, I get nothing, not a null. Try cat /dev/null | xxd -p -- that gives me no output at all. Same with @oliv's hexdump answer, BTW.

          – Gordon Davisson
          Nov 13 '18 at 18:36







          @DailyMemes I don't understand your question about what happens "if there is no stdout". In that case, I get nothing, not a null. Try cat /dev/null | xxd -p -- that gives me no output at all. Same with @oliv's hexdump answer, BTW.

          – Gordon Davisson
          Nov 13 '18 at 18:36




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285210%2fhow-to-loop-through-character-in-string-and-still-detect-null-char-in-bash%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini