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.
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
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
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)
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.
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
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
constexprlongunsigned //Separate function that holds return value via parameter
_bin_parser(constchar* const literal, const size_t& lsize, longunsigned 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)) ) )
;
}
constexprlongunsignedoperator""
_bin(constchar* const literal, size_t lsize){
return _bin_parser(literal, lsize, 0);
}