logo elektroda
logo elektroda
X
logo elektroda

[C++] Which function converts an int decimal number to a binary string in C++?

dodda19 25410 10
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 8023420
    dodda19
    Level 10  
    what function should i use to convert a decimal number to a binary string??
  • ADVERTISEMENT
  • Helpful post
    #2 8035600
    wrych
    Level 20  
    Reply to dodda19 (maybe not very relevant to the current topic, although the problem is the same barrel)...
    A decimal number can be converted into a binary string according to the algorithm contained in the example below:
    [code:1:2f814e4152]
    #include
    #include

    using namespace std;

    int main()
    {
    int n; string bin="";
    cin >> n;
    while (n!=0)
    {
    if (n % 2 == 0){bin="0"+bin;} else {bin="1"+bin;};
    n = n /2;
    };
    cout
  • #3 8037602
    dodda19
    Level 10  
    Hello :-) I have a question - is there any function in Borland C++ that converts decimal values to binary and vice versa??, something like DecToBen???
  • ADVERTISEMENT
  • Helpful post
    #4 8037667
    mf18
    Level 12  
    Hello, is it I don't remember... isn't it faster to write such a function yourself?

    [code:1:6e25b63d42]
    void dec2bin(long n) {
    int p;

    if (n
  • #5 8037675
    arnoldziq
    VIP Meritorious for electroda.pl
    To the author of the topic.
    Please do not refresh archived posts.
    There will be a warning next time.
    I have split and merged the post with the original topic.
  • #6 8037740
    dodda19
    Level 10  
    Thanks :-) and the other way? when i have a string of e.g. 40 bits how to convert it to decimal values
  • ADVERTISEMENT
  • #7 8037782
    arnoldziq
    VIP Meritorious for electroda.pl
    You have to divide those 40 bits first somehow into groups of 8 or 16 or 32 (etc.) bits.
    Well, unless these 40b are one number.
  • ADVERTISEMENT
  • #8 8037808
    dodda19
    Level 10  
    I wanted to divide it into 8 bits, so I wrote:
    int wartosc;
    for(int i=1;iGetTextLen();i=i+8)
         { wartosc=0;
           for(int k=0;kLines->Text=zapisywanawartosc->Lines->Text+ wartosc;
    
        }


    Please remember to use tags code . - arnoldziq

    Added after 17 [minutes]:

    int value;
    for(int i=1;iGetTextLen();i=i+8)
    { value=0;
    for(int k=0;kLines->Text=value written->Lines->Text+ value;

    }

    Added after 45 [seconds]:

    sorry i'm a beginner here :-)

    Added after 38 [minutes]:

    Forgot to add above:

    char dw[200]; 
    int wartosc;
    for(int i=1;iGetTextLen();i++)
            dw[i]=(char)wczytywanyciag->Lines->Text[i];
  • #9 8038160
    several
    Level 15  
    If you want simplicity and good computational complexity, use bitset and its function to_ulong(). After that, it's just a simple cast if you absolutely need a regular integer.
  • #10 8038520
    dodda19
    Level 10  
    Can you tell me more about this feature? I searched the net but honestly I don't understand how it works :-(

Topic summary

The discussion revolves around converting decimal numbers to binary strings in C++. A user inquires about functions available for this conversion, specifically asking for a function like DecToBen in Borland C++. Several responses suggest implementing a custom function for conversion, with an example provided that uses a loop to build the binary string. Another user mentions the use of the C++ STL's `bitset` class and its `to_ulong()` method for converting binary strings back to decimal values, highlighting its simplicity and efficiency. The conversation also touches on the need for dividing binary strings into manageable groups for conversion back to decimal.
Summary generated by the language model.
ADVERTISEMENT