Help with Error - Binary to Character Converter

This is a discussion on Help with Error - Binary to Character Converter within the C++ Programming forums, part of the General Programming Boards category; Hi. I wrote a binary to character converter program but I have a problem - after I type in the ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    8

    Help with Error - Binary to Character Converter

    Hi. I wrote a binary to character converter program but I have a problem - after I type in the binary (e.g. 01000001) the program stops with an error. I ran it in win XP and linux but both give error.

    The errors:
    XP:

    F:\>bin2char-0.1
    Enter string to convert : 01000001

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.


    and linux:

    [dvldrmmr@myhost /]# ./bin2char-0.1
    Enter string to convert : 01000001
    Aborted


    Any ideas why I get an error? I wrote an *almost* identical app in VB (basic vs. c++) and it worked fine.

    My code (I know its not too pretty ):
    Code:
    /***********************************************************/
    /* bin2char-0.1.cpp - Binary to Character Converter v. 0.1 */
    /***********************************************************/
    #include <iostream>
    #include <cmath>
    #include <string>
    using namespace std;
    
    int main()
    { string bin_stream, char_stream;
      int ascii_num, char_start;
      int count = 0;
      char ch;
    
      cout << "Enter string to convert : ";
      cin >> bin_stream;
      if(bin_stream.length() % 8 != 0){
        cout << "Incorrect input string!!\n";
        return 0; }
      for(int i = bin_stream.length(); i > 0; i - 8){
        for(int j = 0; j <= 7; j++){
          char_start = count * j + 1;
          ch = bin_stream.at(char_start + j);
          if(ch == '1'){ ascii_num += (int)(pow((double)2,j)); }
        }
        count++;
        char_stream += (char)ascii_num;
      }
      cout << "Converted string:\n" << char_stream << endl;
      return 0;
    }
    Is it my code?

    Any help appreciated. Thanks.
    Last edited by dvldrmmr; 04-30-2004 at 12:16 AM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Welcome to the forums!
    Code:
      for(int i = bin_stream.length(); i > 0;i - 8)
    The expression i - 8 does not change the value of i, so your loop is endless. You are adding to a std::string in this endless loop, so eventually it will run out of memory and the result will likely be non-optimal.

    Possibly, you want i -= 8 which is the equivalent of i = i - 8.
    Code:
    char_start = count * j + 1;
    I think you want count * 8 instead of count * j.
    Last edited by anonytmouse; 04-30-2004 at 03:25 AM.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    8
    Thanks. It runs in windows now, I'll check linux later after I fix the conversion - it ouputs a bunch of gibberish. Thanks again for your help.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    8
    Ok. I fixed the conversion. Thanks again for your help anonytmouse. Much appreciated.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb ...And now for the easy way!

    FYI - There are two secret functions in <cstdlib> that will convert from any* base: strtoul() = String To Unsigned Long, and strtol() = String to Long.

    I call them secret functions, because I ran-across strtoul() in a program, but couldn't find it in any of my C++ books. So I started searching for a complete C++ reference book. I've never found a complete book, but I did find Dinkumware.com, which does seem to be a complete online reference.

    There is no complimentary standard C++ function to output/display a number in any base. If you have a Microsoft compiler, _itoa() = Integer To ASCII, will do it. For binary display/output <bitset> is standard.

    *Any base between 2 and 36 (base-2 to base-z.) [Corrected. Thanks, Dave.]
    Last edited by DougDbug; 04-30-2004 at 01:10 PM.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    8
    Thanks. I'll have to look into that. Do you happen to know any tutorials about <bitset>?
    Last edited by dvldrmmr; 04-30-2004 at 12:54 PM.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I don't know of any tutorials, but here's a sample program that uses both strtoul() and <bitset>. I think I learned about <bitset> from a book: The C++ Standard Library, A Tutorial and Reference, by Nicolai Josuttis. There is much more to <bitset>, as it's a special class, not just a function.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    8
    Thanks. That helps a lot. One ? though. in this code
    Code:
    cout << "\t" << dec << x << "\t\t Decimal" << endl;
    cout << "\t" << oct << x << "\t\t Octal" << endl;
    cout << "\t" << hex << x << "\t\t Hex" << endl;
    cout << "\t" << bitset<MAX_BITS>(x) << "\t Binary (LSB)" << endl;
    what are dec, oct, and hex. Are they similar to doing this
    Code:
    printf("%X",num); // outputs num in hex format
    for hex anyway? So therefore kinda like format specifiers?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. syntax error when defining vectors
    By starkhorn in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2004, 12:46 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21