Ascii to binary translator, has any one heard this? - Unix

This is a discussion on Ascii to binary translator, has any one heard this? - Unix ; For the sake of something to do I'm working on a sixteen instruction virtual machine at the moment. To program it though I threw together a tool that reads in a file of ascii 0's and 1's (with white space) ...

+ Reply to Thread
Results 1 to 13 of 13

Thread: Ascii to binary translator, has any one heard this?

  1. Ascii to binary translator, has any one heard this?

    For the sake of something to do I'm working on a sixteen instruction
    virtual machine
    at the moment.
    To program it though I threw together a tool that reads in a file of
    ascii 0's and 1's
    (with white space) and output's the numeric equivalents assuming that
    each
    non-whitespace block is a single value.

    For instance a file reading:

    1000 101010 101

    would result in the output file having three 8-bit values:
    8, 42 & 7

    Have I wasted time in making this or is it original anyone?

  2. Re: Ascii to binary translator, has any one heard this?

    2008-08-11, 00:33(-07), RyanMcCoskrie:
    [...]
    > For instance a file reading:
    >
    > 1000 101010 101
    >
    > would result in the output file having three 8-bit values:
    > 8, 42 & 7
    >
    > Have I wasted time in making this or is it original anyone?


    You could do it like:
    perl -pe 's/[01]+/"0"x(8-length$&).$&/ge;s/[^01]//g;$_=pack"B*",$_'

    $ echo 1000 101010 101 | perl -pe '
    s/[01]+/"0"x(8-length$&).$&/ge;s/[^01]//g;$_=pack"B*",$_' |
    od -tu1
    0000000 8 42 5
    0000003

    --
    Stéphane

  3. Re: Ascii to binary translator, has any one heard this?

    RyanMcCoskrie wrote:
    > For the sake of something to do I'm working on a sixteen instruction
    > virtual machine
    > at the moment.
    > To program it though I threw together a tool that reads in a file of
    > ascii 0's and 1's
    > (with white space) and output's the numeric equivalents assuming that
    > each
    > non-whitespace block is a single value.
    >
    > For instance a file reading:
    >
    > 1000 101010 101
    >
    > would result in the output file having three 8-bit values:
    > 8, 42 & 7
    >
    > Have I wasted time in making this or is it original anyone?


    Well, there is a relatively easy solution in C++:


    #include
    #include



    int main()
    {
    while(!std::cin.eof())
    {
    std::string strVal;
    std::cin >> strVal;
    std::bitset<8> set(strVal);

    unsigned long value = set.to_ulong();

    std::cout << value << std::endl;
    }


    return 0;
    }


    At the end, you find the 8-bit values in the variable "value".


    hth,
    Michael

  4. Re: Ascii to binary translator, has any one heard this?

    Michael Oswald writes:
    > RyanMcCoskrie wrote:
    >> For the sake of something to do I'm working on a sixteen instruction
    >> virtual machine
    >> at the moment.
    >> To program it though I threw together a tool that reads in a file of
    >> ascii 0's and 1's
    >> (with white space) and output's the numeric equivalents assuming that
    >> each
    >> non-whitespace block is a single value.
    >> For instance a file reading:
    >> 1000 101010 101
    >> would result in the output file having three 8-bit values:
    >> 8, 42 & 7
    >> Have I wasted time in making this or is it original anyone?

    >
    > Well, there is a relatively easy solution in C++:
    >
    >
    > #include
    > #include
    >
    >
    >
    > int main()
    > {
    > while(!std::cin.eof())
    > {
    > std::string strVal;
    > std::cin >> strVal;
    > std::bitset<8> set(strVal);
    >
    > unsigned long value = set.to_ulong();
    >
    > std::cout << value << std::endl;
    > }
    >
    >
    > return 0;
    > }


    Ugh. Thanks for the nice demonstration that the (somewhat mythical)
    'C++-problem' is really caused by people who simply don't know the
    language and have very little general clue about 'anything computer'.

    unsigned bintoi(char const *s)
    {
    unsigned v, c;

    v = 0;

    while ((c = *s)) {
    v = (v << 1) + (c - '0');
    ++s;
    }

    return v;
    }

    NB: This will not work when the input string contains anything
    except the '0' and '1' (and if its length is > sizeof(unsigned) *
    CHAR_BIT, obviously).


  5. Re: Ascii to binary translator, has any one heard this?

    Stephane Chazelas wrote:
    > 2008-08-11, 00:33(-07), RyanMcCoskrie:
    > [...]
    >> For instance a file reading:
    >>
    >> 1000 101010 101
    >>
    >> would result in the output file having three 8-bit values:
    >> 8, 42 & 7
    >>
    >> Have I wasted time in making this or is it original anyone?

    >
    > You could do it like:
    > perl -pe 's/[01]+/"0"x(8-length$&).$&/ge;s/[^01]//g;$_=pack"B*",$_'
    >
    > $ echo 1000 101010 101 | perl -pe '
    > s/[01]+/"0"x(8-length$&).$&/ge;s/[^01]//g;$_=pack"B*",$_' |
    > od -tu1
    > 0000000 8 42 5
    > 0000003


    $ echo "1000 101010 101" | perl -pe'$_=join"",map
    chr(oct"0b$_"),/[01]+/g' | od -tu1
    0000000 8 42 5
    0000003



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall

  6. Re: Ascii to binary translator, has any one heard this?

    On Aug 11, 9:15*am, "John W. Krahn" wrote:
    > Stephane Chazelas wrote:
    > > 2008-08-11, 00:33(-07), RyanMcCoskrie:
    > > [...]
    > >> For instance a file reading:

    >
    > >> 1000 101010 101

    >
    > >> would result in the output file having three 8-bit values:
    > >> 8, 42 & 7

    >
    > >> Have I wasted time in making this or is it original anyone?

    >
    > > You could do it like:
    > > perl -pe 's/[01]+/"0"x(8-length$&).$&/ge;s/[^01]//g;$_=pack"B*",$_'

    >
    > > $ echo 1000 101010 101 | perl -pe '
    > > * s/[01]+/"0"x(8-length$&).$&/ge;s/[^01]//g;$_=pack"B*",$_' |
    > > * od -tu1
    > > 0000000 * 8 *42 * 5
    > > 0000003

    >
    > $ echo "1000 101010 101" | perl -pe'$_=join"",map
    > chr(oct"0b$_"),/[01]+/g' | od -tu1
    > 0000000 * 8 *42 * 5
    > 0000003
    >
    > John


    Or shell & dc:

    prompt$ for bnum in 1000 101010 101; do echo "2i${bnum}p" | dc; done

    8
    42
    5
    prompt$

  7. Re: Ascii to binary translator, has any one heard this?

    On Mon, 11 Aug 2008 00:33:19 -0700, RyanMcCoskrie wrote:

    > For the sake of something to do I'm working on a sixteen instruction
    > virtual machine
    > at the moment.
    > To program it though I threw together a tool that reads in a file of
    > ascii 0's and 1's
    > (with white space) and output's the numeric equivalents assuming that
    > each
    > non-whitespace block is a single value.
    >
    > For instance a file reading:
    >
    > 1000 101010 101
    >
    > would result in the output file having three 8-bit values: 8, 42 & 7
    >
    > Have I wasted time in making this or is it original anyone?



    So I would say no, your program is not original. Your implementation may
    be original. If you learned anything, or at least enjoyed yourself, then
    your time was not wasted.



  8. Re: Ascii to binary translator, has any one heard this?

    RyanMcCoskrie writes:
    >For the sake of something to do I'm working on a sixteen instruction
    >virtual machine
    >at the moment.
    >To program it though I threw together a tool that reads in a file of
    >ascii 0's and 1's
    >(with white space) and output's the numeric equivalents assuming that
    >each
    >non-whitespace block is a single value.
    >
    >For instance a file reading:
    >
    >1000 101010 101
    >
    >would result in the output file having three 8-bit values:
    >8, 42 & 7
    >
    >Have I wasted time in making this or is it original anyone?


    ksh$ printf '%d %d %d\n' $(( 2#1000 )) $(( 2#101010 )) $(( 2#101 ))
    8 42 5

    scott

  9. Re: Ascii to binary translator, has any one heard this?

    On Aug 11, 3:23*pm, sc...@slp53.sl.home (Scott Lurndal) wrote:
    > RyanMcCoskrie writes:
    > >For the sake of something to do I'm working on a sixteen instruction
    > >virtual machine
    > >at the moment.
    > >To program it though I threw together a tool that reads in a file of
    > >ascii 0's and 1's
    > >(with white space) and output's the numeric equivalents assuming that
    > >each
    > >non-whitespace block is a single value.

    >
    > >For instance a file reading:

    >
    > >1000 101010 101

    >
    > >would result in the output file having three 8-bit values:
    > >8, 42 & 7

    >
    > >Have I wasted time in making this or is it original anyone?

    >
    > ksh$ printf '%d %d %d\n' $(( 2#1000 )) $(( *2#101010 )) $(( 2#101 ))
    > 8 42 5
    >
    > scott


    Neat, I didn't know "math expansion" (or whatever it's called) could
    do that.

    For that matter:
    prompt$ echo "$(( 2#1000 )) $(( 2#101010 )) $(( 2#101 ))"
    8 42 5

  10. Re: Ascii to binary translator, has any one heard this?

    Michael Oswald wrote:
    > while(!std::cin.eof())


    *meeep* Wrong! You first do input, then validate the streamstate to see if
    input succeeded and only then do you actually use the data you have read,
    provided you successfully read it!

    while( in >> value)
    use(value);

    Uli


  11. Re: Ascii to binary translator, has any one heard this?

    Rainer Weikusat wrote:
    > Ugh. Thanks for the nice demonstration that the (somewhat mythical)
    > 'C++-problem' is really caused by people who simply don't know the
    > language and have very little general clue about 'anything computer'.


    Haven't had your coffee?

    What drives you to make such assumptions?


    > unsigned bintoi(char const *s)
    > {
    > unsigned v, c;
    >
    > v = 0;
    >
    > while ((c = *s)) {
    > v = (v << 1) + (c - '0');
    > ++s;
    > }
    >
    > return v;
    > }


    So what?

    That's valid C++ code like mine (except the eof checking like Ulrich
    pointed out). But there is no reading from the file (the OP said, he
    wanted to parse the numbers from a file). Of course I could write
    bit-shifting code like yours but for what, if I already have components
    which do this? The OP didn't specify anything about performance, if you
    are concerned about this.

    I don't know what you are programming normally, but in my company I
    would already have to write some unit tests for this conversion
    function, while I don't have to do it for STL components. Therefore your
    version would effectively result in significant code and time increase.

    All in all I don't see anything in my code which renders your
    assumptions that I don't know C++ or I have "very little general clue
    about 'anything computer'" valid.



    Michael


  12. Re: Ascii to binary translator, has any one heard this?

    Michael Oswald writes:
    > Rainer Weikusat wrote:
    >> Ugh. Thanks for the nice demonstration that the (somewhat mythical)
    >> 'C++-problem' is really caused by people who simply don't know the
    >> language and have very little general clue about 'anything computer'.

    >
    > Haven't had your coffee?
    > What drives you to make such assumptions?


    You are aware what 'demonstration' means, are you?

    >> unsigned bintoi(char const *s)
    >> {
    >> unsigned v, c;
    >> v = 0;
    >> while ((c = *s)) {
    >> v = (v << 1) + (c - '0');
    >> ++s;
    >> }
    >> return v;
    >> }

    >
    > So what?
    >
    > That's valid C++ code like mine (except the eof checking like Ulrich
    > pointed out). But there is no reading from the file (the OP said, he
    > wanted to parse the numbers from a file). Of course I could write
    > bit-shifting code like yours but for what, if I already have
    > components which do this?


    The more interesting question would be 'why are you using generalized
    container class templates to emulate elementary arithmetic operations
    the language already provides'?

    [...]

    > I don't know what you are programming normally, but in my company I
    > would already have to write some unit tests for this conversion
    > function, while I don't have to do it for STL components.


    And the answer is: A simple, arithmetic algorithm is already so much
    beyond your comprehension that you would have to write test code to
    verify what it actually does.

  13. Re: Ascii to binary translator, has any one heard this?

    Rainer Weikusat wrote:

    >>I don't know what you are programming normally, but in my company I
    >>would already have to write some unit tests for this conversion
    >>function, while I don't have to do it for STL components.

    >
    >
    > And the answer is: A simple, arithmetic algorithm is already so much
    > beyond your comprehension that you would have to write test code to
    > verify what it actually does.


    Yes, Master! I bow for you Master!


    Sorry, you've lost my interest.


    Michael


+ Reply to Thread