Re: converting int values to other formats

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sunil

    Re: converting int values to other formats

    I am trying to see as to what kind of input such as below can produce
    a output. If you see for input 7, the output is " and for 8 is v. I am
    trying to determine what can generate an output in this format using C

    Thanks

    Please enter a key to continue : 7
    Input 7 Output "
    Please enter a key to continue : 8
    Input 8 Output v
    Please enter a key to continue : 9
    Input 9 Output $
    Please enter a key to continue : !
    Input ! Output @
    Please enter a key to continue : a
    Input a Output #
    Please enter a key to continue : b
    Input b Output @
    Please enter a key to continue : c
    Input c Output E
    Please enter a key to continue : d
    Input d Output @
    Please enter a key to continue : e
    Input e Output %
    Please enter a key to continue : f
    Input f Output #
    Please enter a key to continue : g
    Input g Output G
    Please enter a key to continue : h
    Input h Output #
  • Ben Bacarisse

    #2
    Re: converting int values to other formats

    Sunil <sravipati@gmai l.comwrites:
    I am trying to see as to what kind of input such as below can produce
    a output. If you see for input 7, the output is " and for 8 is v. I am
    trying to determine what can generate an output in this format using C
    >
    Please enter a key to continue : 7
    Input 7 Output "
    Please enter a key to continue : 8
    Input 8 Output v
    Please enter a key to continue : 9
    Input 9 Output $
    Please enter a key to continue : !
    Input ! Output @
    Please enter a key to continue : a
    Input a Output #
    Please enter a key to continue : b
    Input b Output @
    Please enter a key to continue : c
    Input c Output E
    Please enter a key to continue : d
    Input d Output @
    Please enter a key to continue : e
    Input e Output %
    Please enter a key to continue : f
    Input f Output #
    Please enter a key to continue : g
    Input g Output G
    Please enter a key to continue : h
    Input h Output #
    That is a little under-specified. One possible answer is:

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char c, *r, src[] = "789!abcdef gh", dst[] = "\"v$@#@E@%#G#" ;
    while (printf("Please enter a key to continue : ") 0 &&
    fflush(stdout) == 0 &&
    scanf(" %c", &c) == 1)
    if (r = strchr(src, c))
    printf("Input %c Output %c\n", c, dst[r - src]);
    return 0;
    }

    but you almost certainly don't mean that. Are you asking if anyone
    can spot an "interestin g" relationship between the input and the
    output?

    --
    Ben.

    Comment

    • Walter Roberson

      #3
      Re: converting int values to other formats

      In article <8180b240-40f9-42e0-9791-71f7b827be1e@q2 4g2000prf.googl egroups.com>,
      Sunil <sravipati@gmai l.comwrote:
      >I am trying to see as to what kind of input such as below can produce
      >a output. If you see for input 7, the output is " and for 8 is v. I am
      >trying to determine what can generate an output in this format using C
      Possibly:

      malloc() space to hold the character and then print the -pointer-
      using a %c format.

      If so, then entering the same character twice would not necessarily
      have the same result each time.


      Or, just index by the input character into an uninitialized
      offset in memory. Could be nearly anything there. Might happen
      to hit machine instructions, for example.


      Other than that, I don't see any particular pattern. I was thinking
      perhaps some nands and xors, but if it is that, it would have to be
      a number of them, not something simple.
      --
      "If there were no falsehood in the world, there would be no
      doubt; if there were no doubt, there would be no inquiry; if no
      inquiry, no wisdom, no knowledge, no genius."
      -- Walter Savage Landor

      Comment

      • Jack Klein

        #4
        Re: converting int values to other formats

        On Sat, 26 Apr 2008 20:14:37 -0700 (PDT), Sunil <sravipati@gmai l.com>
        wrote in comp.lang.c:
        I am trying to see as to what kind of input such as below can produce
        a output. If you see for input 7, the output is " and for 8 is v. I am
        trying to determine what can generate an output in this format using C
        >
        Thanks
        >
        Please enter a key to continue : 7
        Input 7 Output "
        Please enter a key to continue : 8
        Input 8 Output v
        Please enter a key to continue : 9
        Input 9 Output $
        Please enter a key to continue : !
        Input ! Output @
        Please enter a key to continue : a
        Input a Output #
        Please enter a key to continue : b
        Input b Output @
        Please enter a key to continue : c
        Input c Output E
        Please enter a key to continue : d
        Input d Output @
        Please enter a key to continue : e
        Input e Output %
        Please enter a key to continue : f
        Input f Output #
        Please enter a key to continue : g
        Input g Output G
        Please enter a key to continue : h
        Input h Output #
        Your question does not make it very clear what it is that you are
        trying to do.

        One easy way to have an arbitrary one-to-one map of one set of
        characters to another is to have an array of structures like this:

        struct char_map { int in, int out };

        struct char_map table [] =
        {
        { '7', '"' },
        { '8', 'v' },
        /* and so on, for however many you need */
        };

        Then when you receive an input character, you search the first member
        of every structure in the array to see if there is a match. If there
        is, you output the second member of the structure.

        Another possibility is to use a pair of strings:

        const char inputs [] = "789!abcdef gh";
        const char output [] = "\"v$@#@E@%#G#" ;

        You can search the input string for the input character with strchr(),
        and use its distance from the start of the string (if it is found) to
        compute the index into the replacement string.

        In either approach, you have to decide what to do if the input does
        not match one of your defines substitution patterns.

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://c-faq.com/
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++

        Comment

        • sunny

          #5
          Re: converting int values to other formats

          On Apr 26, 8:54 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
          Sunil <sravip...@gmai l.comwrites:
          I am trying to see as to what kind of input such as below can produce
          a output. If you see for input 7, the output is " and for 8 is v. I am
          trying to determine what can generate an output in this format using C
          >
          Please enter a key to continue : 7
          Input 7 Output "
          Please enter a key to continue : 8
          Input 8 Output v
          Please enter a key to continue : 9
          Input 9 Output $
          Please enter a key to continue : !
          Input ! Output @
          Please enter a key to continue : a
          Input a Output #
          Please enter a key to continue : b
          Input b Output @
          Please enter a key to continue : c
          Input c Output E
          Please enter a key to continue : d
          Input d Output @
          Please enter a key to continue : e
          Input e Output %
          Please enter a key to continue : f
          Input f Output #
          Please enter a key to continue : g
          Input g Output G
          Please enter a key to continue : h
          Input h Output #
          >
          That is a little under-specified. One possible answer is:
          >
          #include <stdio.h>
          #include <string.h>
          >
          int main(void)
          {
          char c, *r, src[] = "789!abcdef gh", dst[] = "\"v$@#@E@%#G#" ;
          while (printf("Please enter a key to continue : ") 0 &&
          fflush(stdout) == 0 &&
          scanf(" %c", &c) == 1)
          if (r = strchr(src, c))
          printf("Input %c Output %c\n", c, dst[r - src]);
          return 0;
          >
          }
          >
          but you almost certainly don't mean that. Are you asking if anyone
          can spot an "interestin g" relationship between the input and the
          output?
          >
          --
          Ben.

          Thanks, yes. I am trying to get any correlation. If you repeat the
          input then the output is not the same either. I took a rand() number,
          cast it to a char and saw the output. In this particular case, the
          output seem to be within the [0 -9] and [a-z A-Z]
          Appreciate all your replies.

          Sunny

          Comment

          • Walter Roberson

            #6
            Re: converting int values to other formats

            In article <fb91007d-cf41-408f-ad9a-06dabcee886c@r9 g2000prd.google groups.com>,
            sunny <sravipati@gmai l.comwrote:
            >Thanks, yes. I am trying to get any correlation. If you repeat the
            >input then the output is not the same either. I took a rand() number,
            >cast it to a char and saw the output. In this particular case, the
            >output seem to be within the [0 -9] and [a-z A-Z]
            "the output" is a bit ambiguous; it isn't clear if you are talking
            about the original output you showed us, or the output of the
            program that used rand(). If you are talking about the original
            output, then may you have not noticed the fact that the most common
            output was # and that " $ @ % occured as well.

            If you repeat the input and the output is different, there isn't
            necessarily any logic to the outputs. Try repeating the same
            input over and over and seeing if a pattern develops or if
            characters outside of the printable ASCII range show up.
            --
            "Walter exemplified class." -- Paul Tagliabue

            Comment

            Working...