Results 1 to 6 of 6

Thread: [SOLVED] Is there a BASH equivalant of...

  1. #1
    Join Date
    Jan 2008
    Beans
    4,757

    [SOLVED] Is there a BASH equivalant of...

    Hi, thank-you in advanced.

    Is there a BASH equivalent of printing the character code of a letter or symbol?

    example in C
    Code:
    char ch = "A";
    printf ("%d", ch);
    the only thing I can find that is similiar is echo -e "/xnnn".
    But that is my question in reverse.

    Regards
    Iain

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Is there a BASH equivalant of...

    No simple way.

    Do you have to use bash?

    http://www.linuxquestions.org/questi...-ascii-488357/

  3. #3
    Join Date
    Mar 2005
    Location
    Haarlem, The Netherlands
    Beans
    363

    Re: Is there a BASH equivalant of...

    You can write a few functions yourself, eg:
    Code:
    #!/bin/bash
    # chr() - converts decimal value to its ASCII character representation
    # ord() - converts ASCII character to its decimal value
    
    chr() {
      printf \\$(printf '%03o' $1)
    }
    
    ord() {
      printf '%d' "'$1"
    }
    
    ord A
    echo
    chr 65
    echo
    (taken from http://wooledge.org:8000/BashFAQ)

  4. #4
    Join Date
    Jan 2008
    Beans
    4,757

    Re: Is there a BASH equivalant of...

    Thanks, I didn't know BASH used the C syntax.

    I suppose you learn something new everyday!

  5. #5
    Join Date
    Jan 2008
    Beans
    4,757

    Re: Is there a BASH equivalant of...

    Quote Originally Posted by LaRoza View Post
    No simple way.

    Do you have to use bash?
    Well, I suppose the answer is now yes. As I can't find a way to convert an input argument to its ascii number in C. (ie: a into 97)
    Because argv[1] is treated as a string, not a character.
    So printf ("%s", argv[1]); is the only way to properly print the input argument. (%c will print some random gibberish).

    but I can extract a number from an input argument.
    ch = (int)strtol ( argv[1], NULL, 10 );

    So I figured that it may be easier to use a language that treats it's input arguments as characters...

    Hence why I ask if BASH can do this. So I can execute the C program with the number argument of the char I want.

    Hope this makes sense.

    Regards
    Iain

    [EDIT]
    Here's a brief example of what I was trying to (and have now) acheive.
    Code:
    ___________________________________
    #!/bin/bash
    
    ch=$(printf '%d' "'$1")
    ./cprog $ch
    ___________________________________
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[])
    {
            int ch = (int)strtol (argv[1], NULL, 10);
            /*
            switch to react to the inputted argument
            */
            return 0;
    }
    ___________________________________
    Last edited by ibuclaw; March 25th, 2008 at 07:24 PM.

  6. #6
    Join Date
    Nov 2005
    Beans
    12

    Re: Is there a BASH equivalant of...

    Quote Originally Posted by tinivole View Post
    Well, I suppose the answer is now yes. As I can't find a way to convert an input argument to its ascii number in C. (ie: a into 97)
    Because argv[1] is treated as a string, not a character.
    So printf ("%s", argv[1]); is the only way to properly print the input argument. (%c will print some random gibberish).
    A string in C is an array of characters... so you can do argv[1][0] to get the first character in the first argument... then you can convert it to an int.

    Also, in C, you can just cast it to an int, or you can just use it as if it was one.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •