ifstream to binary

How do I convert ifstream to binary and display the binary at then end. I have a file that when it contains numbers it can do it but when reading strings it has trouble. It acts as if there is no binary in the file.
closed account (Dy7SLyTq)
thats not converting ifstream to binary. thats converting a string to binary. i would just write a function to convert each character to its binary value. think hex editor
The conversion goes from ifstream to string to integer to binary. I need to skip the integer step somehow.
closed account (Dy7SLyTq)
no... ifstream reads in a string... ifstream isnt converted to a string... and what do you mean its going through an int phase? just dont have it do it... just read in a string and write a function to convert each character to its binary value
int stuff;
string string;
Ifstream variable("file");
getline("variable, string");
stuff = atoi(ticklebilly.c_str());
(out function)<< (bitset<8>) stuff;

Do you have any idea what I'm asking for?
What is this magic function you speak of!?!?!?!?!?!?!
closed account (Dy7SLyTq)
its actually getline(variable, string); idk why you have quotes. and remove the last line... it makes no sense. instead do itob(stuff); and define itob to take a string and change each character to its binary equivalent. its not a magic function
Sorry about the quotes, I didn't mean to put them and that is not the actual source code as it is way more complicated. the real last line is cout<< (bitset<8>) i; I need something to change a string into it's binary equivalent <-What I want)
Last edited on
closed account (Dy7SLyTq)
omfg actually read what i write
Oh, I misread sorry. What libraries do I need for itob()? Yes it is a magic function!
Last edited on
closed account (Dy7SLyTq)
no its not... alls you need is the stl and then take each int and turn that into its binary value... honestly a quick search of c++ int to binary function will give you hundreds of results.
I'm not looking for int to bin, that's easy and I had already done that. We are looking to open a file and print the contents as binary. Is what you gave me only for integers? <-for ifstream, its magic) I do have the file contents as a string so all I need is a conversion from STRING to bin.
What part of the STL do I use?
closed account (Dy7SLyTq)
ok i will say this one more time... its not fucking magic. read in a file and store each line in a string. then throw the line at a function which i have decided to call itob. take each character of the string and turn them into an int with atoi from stl's <cstdlib>. then turn that into its binary value. if you dont want ints at all just find the binary value of the character itself. append it to a string that holds the entire binary sequence. return the string. print it
It doesn't work that way. I already tried that and unless the file contains an integer digit it will just print the file as if it was blank.
closed account (Dy7SLyTq)
yes it does... i could write this easily
Oh and yes it's fucking magic! I once had a dream of Narnia where this one function did it all. I forgot 90% of the dream when I woke up and now I don't remember the header or the function.
You haven't written this yet though. I used a different process outside of the cstandardlibrary to do this and it didn't work. I'm seriously confused. If you do successfully write it can I see the source Mr. Mage? Can you teach me more magic? <-Don't call this easy. when I told my friend my goal he said "Pssh, good luck with that!")
I have these string literal and helper functions I wrote a while back. A couple users here helped me with them. These convert a C-string to binary in an unsigned long.
1
2
3
4
5
6
7
8
9
10
11
12
13
constexpr long unsigned	//Separate function that holds return value via parameter
    _bin_parser(const char* const literal, const size_t& lsize, long unsigned toreturn){
        return (lsize == 1) ?
            //Below: use conditional again because the literal might contain other numbers
            ( toreturn + (literal[0] == '1' ? 1 : 0) ) :
            ( _bin_parser(literal + 1, lsize - 1, 
                (toreturn + (literal[0] == '1' ? 1 << lsize-1 : 0)) ) )
        ;
}
constexpr long unsigned operator"" 
    _bin(const char* const literal, size_t lsize){
        return _bin_parser(literal, lsize, 0); 
}
Last edited on
Topic archived. No new replies allowed.