Save Char value from ASCII in a bash variable












2















I have this code to convert Dec ASCII value to char:



printf "\$(printf %o $ascii_value)"


Instead of print it, I would like to save it in a var. However, I can't get the Char value in that way.



root@private:/path# v=`printf "\$(printf %o 42)"`; echo $v
$(printf 0 42)
root@private:/path# printf "\$(printf %o 42)"
*


How can I save the Char value in a variable?
Thanks!!










share|improve this question



























    2















    I have this code to convert Dec ASCII value to char:



    printf "\$(printf %o $ascii_value)"


    Instead of print it, I would like to save it in a var. However, I can't get the Char value in that way.



    root@private:/path# v=`printf "\$(printf %o 42)"`; echo $v
    $(printf 0 42)
    root@private:/path# printf "\$(printf %o 42)"
    *


    How can I save the Char value in a variable?
    Thanks!!










    share|improve this question

























      2












      2








      2


      1






      I have this code to convert Dec ASCII value to char:



      printf "\$(printf %o $ascii_value)"


      Instead of print it, I would like to save it in a var. However, I can't get the Char value in that way.



      root@private:/path# v=`printf "\$(printf %o 42)"`; echo $v
      $(printf 0 42)
      root@private:/path# printf "\$(printf %o 42)"
      *


      How can I save the Char value in a variable?
      Thanks!!










      share|improve this question














      I have this code to convert Dec ASCII value to char:



      printf "\$(printf %o $ascii_value)"


      Instead of print it, I would like to save it in a var. However, I can't get the Char value in that way.



      root@private:/path# v=`printf "\$(printf %o 42)"`; echo $v
      $(printf 0 42)
      root@private:/path# printf "\$(printf %o 42)"
      *


      How can I save the Char value in a variable?
      Thanks!!







      bash shell char ascii






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 29 '18 at 14:42









      Miguel.GMiguel.G

      9810




      9810
























          2 Answers
          2






          active

          oldest

          votes


















          4














          If the printf available in your shell supports the -v option (similar to sprintf() in C) you can directly use that to store the content of the formatted string directly to the variable and not printing to standard output.



          printf -v char_value "\$(printf %o $ascii_value)"


          You can verify that with a simple example



          for ascii in {65..90} {97..122}; do
          printf -v char_value "\$(printf %o $ascii)"
          printf '%cn' "$char_value"
          done





          share|improve this answer





















          • 1





            Thanks for your solution, it works!:)

            – Miguel.G
            Dec 3 '18 at 14:48



















          1














          Simply do:



          ascii_value=65; char="$(printf "\$(printf "%o" "${ascii_value}")")"; echo $char
          A


          Lets make this a function for easy reuse:



          #!/usr/bin/env bash

          # Gets a character from its ASCII value
          # Params:
          # $1: the ASCII value of the character
          # Return:
          # >: the character with given ASCII value
          # ?: false if the ASCII value is out of the 0..127 range
          ASCIIToChar() {
          [ "${1}" -lt 0 -o "${1}" -gt 127 ] && return 1
          printf "\$(printf "%o" "${1}")"
          }

          # Lets demo the function above

          declare -i ascii # variable ascii of type integer
          declare character # variable character

          # Print a header
          echo -e "ASCIIttCharacter"
          for ascii in {65..90} {97..122}; do
          # Convert the ascii value and store the character
          character="$(ASCIIToChar "${ascii}")"

          # Print line with 2 columns ASCII and character
          echo -e "${ascii}tt${character}"
          done


          Will output:



          ASCII       Character
          65 A
          66 B
          67 C
          68 D
          69 E
          70 F
          [...]
          119 w
          120 x
          121 y
          122 z


          Or to convert UTF-8 to character



          # Gets a character from its UTF-8 value
          # Params:
          # $1: the UTF-8 value of the character
          # Return:
          # >: the character with given UTF-8 value
          # ?: false if UTF-8 value is out of the 0..65535 range
          UTF8toChar() {
          [ "$((${1}))" -lt 0 -o "$((${1}))" -gt 65535 ] && return 1;
          printf "\u$(printf "%04x" "${1}")"
          }

          # Lets demo the function above

          declare -i utf8 # variable utf8 of type integer
          declare character # variable character

          # Print a header
          echo -e "UTF-8ttCharacter"
          for utf8 in {9472..9616} # U+2500..U+259F semi-graphic
          do
          # Convert the UTF-8 value and store the character
          character="$(UTF8toChar "${utf8}")"

          # Print line with 2 columns UTF-8 and character
          printf "U+%04Xtt%sn" "${utf8}" "${character}"
          done


          Will output:



          UTF-8       Character
          U+2500 ─
          U+2501 ━
          U+2502 │
          U+2503 ┃
          U+2504 ┄
          U+2505 ┅
          [...]
          U+2567 ╧
          U+2568 ╨
          U+2569 ╩
          U+256A ╪
          U+256B ╫
          U+256C ╬
          U+256D ╭
          U+256E ╮
          U+256F ╯
          U+2570 ╰
          [...]





          share|improve this answer


























          • Hi! Thanks for your solution:)

            – Miguel.G
            Dec 3 '18 at 14:49











          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%2f53541516%2fsave-char-value-from-ascii-in-a-bash-variable%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









          4














          If the printf available in your shell supports the -v option (similar to sprintf() in C) you can directly use that to store the content of the formatted string directly to the variable and not printing to standard output.



          printf -v char_value "\$(printf %o $ascii_value)"


          You can verify that with a simple example



          for ascii in {65..90} {97..122}; do
          printf -v char_value "\$(printf %o $ascii)"
          printf '%cn' "$char_value"
          done





          share|improve this answer





















          • 1





            Thanks for your solution, it works!:)

            – Miguel.G
            Dec 3 '18 at 14:48
















          4














          If the printf available in your shell supports the -v option (similar to sprintf() in C) you can directly use that to store the content of the formatted string directly to the variable and not printing to standard output.



          printf -v char_value "\$(printf %o $ascii_value)"


          You can verify that with a simple example



          for ascii in {65..90} {97..122}; do
          printf -v char_value "\$(printf %o $ascii)"
          printf '%cn' "$char_value"
          done





          share|improve this answer





















          • 1





            Thanks for your solution, it works!:)

            – Miguel.G
            Dec 3 '18 at 14:48














          4












          4








          4







          If the printf available in your shell supports the -v option (similar to sprintf() in C) you can directly use that to store the content of the formatted string directly to the variable and not printing to standard output.



          printf -v char_value "\$(printf %o $ascii_value)"


          You can verify that with a simple example



          for ascii in {65..90} {97..122}; do
          printf -v char_value "\$(printf %o $ascii)"
          printf '%cn' "$char_value"
          done





          share|improve this answer















          If the printf available in your shell supports the -v option (similar to sprintf() in C) you can directly use that to store the content of the formatted string directly to the variable and not printing to standard output.



          printf -v char_value "\$(printf %o $ascii_value)"


          You can verify that with a simple example



          for ascii in {65..90} {97..122}; do
          printf -v char_value "\$(printf %o $ascii)"
          printf '%cn' "$char_value"
          done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 29 '18 at 17:31

























          answered Nov 29 '18 at 14:53









          InianInian

          41.1k64274




          41.1k64274








          • 1





            Thanks for your solution, it works!:)

            – Miguel.G
            Dec 3 '18 at 14:48














          • 1





            Thanks for your solution, it works!:)

            – Miguel.G
            Dec 3 '18 at 14:48








          1




          1





          Thanks for your solution, it works!:)

          – Miguel.G
          Dec 3 '18 at 14:48





          Thanks for your solution, it works!:)

          – Miguel.G
          Dec 3 '18 at 14:48













          1














          Simply do:



          ascii_value=65; char="$(printf "\$(printf "%o" "${ascii_value}")")"; echo $char
          A


          Lets make this a function for easy reuse:



          #!/usr/bin/env bash

          # Gets a character from its ASCII value
          # Params:
          # $1: the ASCII value of the character
          # Return:
          # >: the character with given ASCII value
          # ?: false if the ASCII value is out of the 0..127 range
          ASCIIToChar() {
          [ "${1}" -lt 0 -o "${1}" -gt 127 ] && return 1
          printf "\$(printf "%o" "${1}")"
          }

          # Lets demo the function above

          declare -i ascii # variable ascii of type integer
          declare character # variable character

          # Print a header
          echo -e "ASCIIttCharacter"
          for ascii in {65..90} {97..122}; do
          # Convert the ascii value and store the character
          character="$(ASCIIToChar "${ascii}")"

          # Print line with 2 columns ASCII and character
          echo -e "${ascii}tt${character}"
          done


          Will output:



          ASCII       Character
          65 A
          66 B
          67 C
          68 D
          69 E
          70 F
          [...]
          119 w
          120 x
          121 y
          122 z


          Or to convert UTF-8 to character



          # Gets a character from its UTF-8 value
          # Params:
          # $1: the UTF-8 value of the character
          # Return:
          # >: the character with given UTF-8 value
          # ?: false if UTF-8 value is out of the 0..65535 range
          UTF8toChar() {
          [ "$((${1}))" -lt 0 -o "$((${1}))" -gt 65535 ] && return 1;
          printf "\u$(printf "%04x" "${1}")"
          }

          # Lets demo the function above

          declare -i utf8 # variable utf8 of type integer
          declare character # variable character

          # Print a header
          echo -e "UTF-8ttCharacter"
          for utf8 in {9472..9616} # U+2500..U+259F semi-graphic
          do
          # Convert the UTF-8 value and store the character
          character="$(UTF8toChar "${utf8}")"

          # Print line with 2 columns UTF-8 and character
          printf "U+%04Xtt%sn" "${utf8}" "${character}"
          done


          Will output:



          UTF-8       Character
          U+2500 ─
          U+2501 ━
          U+2502 │
          U+2503 ┃
          U+2504 ┄
          U+2505 ┅
          [...]
          U+2567 ╧
          U+2568 ╨
          U+2569 ╩
          U+256A ╪
          U+256B ╫
          U+256C ╬
          U+256D ╭
          U+256E ╮
          U+256F ╯
          U+2570 ╰
          [...]





          share|improve this answer


























          • Hi! Thanks for your solution:)

            – Miguel.G
            Dec 3 '18 at 14:49
















          1














          Simply do:



          ascii_value=65; char="$(printf "\$(printf "%o" "${ascii_value}")")"; echo $char
          A


          Lets make this a function for easy reuse:



          #!/usr/bin/env bash

          # Gets a character from its ASCII value
          # Params:
          # $1: the ASCII value of the character
          # Return:
          # >: the character with given ASCII value
          # ?: false if the ASCII value is out of the 0..127 range
          ASCIIToChar() {
          [ "${1}" -lt 0 -o "${1}" -gt 127 ] && return 1
          printf "\$(printf "%o" "${1}")"
          }

          # Lets demo the function above

          declare -i ascii # variable ascii of type integer
          declare character # variable character

          # Print a header
          echo -e "ASCIIttCharacter"
          for ascii in {65..90} {97..122}; do
          # Convert the ascii value and store the character
          character="$(ASCIIToChar "${ascii}")"

          # Print line with 2 columns ASCII and character
          echo -e "${ascii}tt${character}"
          done


          Will output:



          ASCII       Character
          65 A
          66 B
          67 C
          68 D
          69 E
          70 F
          [...]
          119 w
          120 x
          121 y
          122 z


          Or to convert UTF-8 to character



          # Gets a character from its UTF-8 value
          # Params:
          # $1: the UTF-8 value of the character
          # Return:
          # >: the character with given UTF-8 value
          # ?: false if UTF-8 value is out of the 0..65535 range
          UTF8toChar() {
          [ "$((${1}))" -lt 0 -o "$((${1}))" -gt 65535 ] && return 1;
          printf "\u$(printf "%04x" "${1}")"
          }

          # Lets demo the function above

          declare -i utf8 # variable utf8 of type integer
          declare character # variable character

          # Print a header
          echo -e "UTF-8ttCharacter"
          for utf8 in {9472..9616} # U+2500..U+259F semi-graphic
          do
          # Convert the UTF-8 value and store the character
          character="$(UTF8toChar "${utf8}")"

          # Print line with 2 columns UTF-8 and character
          printf "U+%04Xtt%sn" "${utf8}" "${character}"
          done


          Will output:



          UTF-8       Character
          U+2500 ─
          U+2501 ━
          U+2502 │
          U+2503 ┃
          U+2504 ┄
          U+2505 ┅
          [...]
          U+2567 ╧
          U+2568 ╨
          U+2569 ╩
          U+256A ╪
          U+256B ╫
          U+256C ╬
          U+256D ╭
          U+256E ╮
          U+256F ╯
          U+2570 ╰
          [...]





          share|improve this answer


























          • Hi! Thanks for your solution:)

            – Miguel.G
            Dec 3 '18 at 14:49














          1












          1








          1







          Simply do:



          ascii_value=65; char="$(printf "\$(printf "%o" "${ascii_value}")")"; echo $char
          A


          Lets make this a function for easy reuse:



          #!/usr/bin/env bash

          # Gets a character from its ASCII value
          # Params:
          # $1: the ASCII value of the character
          # Return:
          # >: the character with given ASCII value
          # ?: false if the ASCII value is out of the 0..127 range
          ASCIIToChar() {
          [ "${1}" -lt 0 -o "${1}" -gt 127 ] && return 1
          printf "\$(printf "%o" "${1}")"
          }

          # Lets demo the function above

          declare -i ascii # variable ascii of type integer
          declare character # variable character

          # Print a header
          echo -e "ASCIIttCharacter"
          for ascii in {65..90} {97..122}; do
          # Convert the ascii value and store the character
          character="$(ASCIIToChar "${ascii}")"

          # Print line with 2 columns ASCII and character
          echo -e "${ascii}tt${character}"
          done


          Will output:



          ASCII       Character
          65 A
          66 B
          67 C
          68 D
          69 E
          70 F
          [...]
          119 w
          120 x
          121 y
          122 z


          Or to convert UTF-8 to character



          # Gets a character from its UTF-8 value
          # Params:
          # $1: the UTF-8 value of the character
          # Return:
          # >: the character with given UTF-8 value
          # ?: false if UTF-8 value is out of the 0..65535 range
          UTF8toChar() {
          [ "$((${1}))" -lt 0 -o "$((${1}))" -gt 65535 ] && return 1;
          printf "\u$(printf "%04x" "${1}")"
          }

          # Lets demo the function above

          declare -i utf8 # variable utf8 of type integer
          declare character # variable character

          # Print a header
          echo -e "UTF-8ttCharacter"
          for utf8 in {9472..9616} # U+2500..U+259F semi-graphic
          do
          # Convert the UTF-8 value and store the character
          character="$(UTF8toChar "${utf8}")"

          # Print line with 2 columns UTF-8 and character
          printf "U+%04Xtt%sn" "${utf8}" "${character}"
          done


          Will output:



          UTF-8       Character
          U+2500 ─
          U+2501 ━
          U+2502 │
          U+2503 ┃
          U+2504 ┄
          U+2505 ┅
          [...]
          U+2567 ╧
          U+2568 ╨
          U+2569 ╩
          U+256A ╪
          U+256B ╫
          U+256C ╬
          U+256D ╭
          U+256E ╮
          U+256F ╯
          U+2570 ╰
          [...]





          share|improve this answer















          Simply do:



          ascii_value=65; char="$(printf "\$(printf "%o" "${ascii_value}")")"; echo $char
          A


          Lets make this a function for easy reuse:



          #!/usr/bin/env bash

          # Gets a character from its ASCII value
          # Params:
          # $1: the ASCII value of the character
          # Return:
          # >: the character with given ASCII value
          # ?: false if the ASCII value is out of the 0..127 range
          ASCIIToChar() {
          [ "${1}" -lt 0 -o "${1}" -gt 127 ] && return 1
          printf "\$(printf "%o" "${1}")"
          }

          # Lets demo the function above

          declare -i ascii # variable ascii of type integer
          declare character # variable character

          # Print a header
          echo -e "ASCIIttCharacter"
          for ascii in {65..90} {97..122}; do
          # Convert the ascii value and store the character
          character="$(ASCIIToChar "${ascii}")"

          # Print line with 2 columns ASCII and character
          echo -e "${ascii}tt${character}"
          done


          Will output:



          ASCII       Character
          65 A
          66 B
          67 C
          68 D
          69 E
          70 F
          [...]
          119 w
          120 x
          121 y
          122 z


          Or to convert UTF-8 to character



          # Gets a character from its UTF-8 value
          # Params:
          # $1: the UTF-8 value of the character
          # Return:
          # >: the character with given UTF-8 value
          # ?: false if UTF-8 value is out of the 0..65535 range
          UTF8toChar() {
          [ "$((${1}))" -lt 0 -o "$((${1}))" -gt 65535 ] && return 1;
          printf "\u$(printf "%04x" "${1}")"
          }

          # Lets demo the function above

          declare -i utf8 # variable utf8 of type integer
          declare character # variable character

          # Print a header
          echo -e "UTF-8ttCharacter"
          for utf8 in {9472..9616} # U+2500..U+259F semi-graphic
          do
          # Convert the UTF-8 value and store the character
          character="$(UTF8toChar "${utf8}")"

          # Print line with 2 columns UTF-8 and character
          printf "U+%04Xtt%sn" "${utf8}" "${character}"
          done


          Will output:



          UTF-8       Character
          U+2500 ─
          U+2501 ━
          U+2502 │
          U+2503 ┃
          U+2504 ┄
          U+2505 ┅
          [...]
          U+2567 ╧
          U+2568 ╨
          U+2569 ╩
          U+256A ╪
          U+256B ╫
          U+256C ╬
          U+256D ╭
          U+256E ╮
          U+256F ╯
          U+2570 ╰
          [...]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 12:59

























          answered Nov 30 '18 at 0:45









          Léa GrisLéa Gris

          413




          413













          • Hi! Thanks for your solution:)

            – Miguel.G
            Dec 3 '18 at 14:49



















          • Hi! Thanks for your solution:)

            – Miguel.G
            Dec 3 '18 at 14:49

















          Hi! Thanks for your solution:)

          – Miguel.G
          Dec 3 '18 at 14:49





          Hi! Thanks for your solution:)

          – Miguel.G
          Dec 3 '18 at 14:49


















          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%2f53541516%2fsave-char-value-from-ascii-in-a-bash-variable%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







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules