text to ascii number

help me am trying to create a program that converts text to ascii numbers using text file but its only reading one character

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ofstream outstream ("ascii.txt");
if(outstream.is_open())
{
{
cout<< "unable to open the file";
}
string text;
ifstream instream ("ascii.txt");
{
getline(instream,text);

cout <<text<< end1:
}

string words;
cout<< "convert input text;\n";
cout<< "enter character;\n";
getline(cin,words);
instream>>hex;

for(unsigned 1 = 0;1 < words.length();1++)
{
cout <<hex<<(int)words[i];
}
}
return 0;
}
One problem here is that you're assuming your computer is using an ASCII-compatible encoding. This is not guaranteed to be the case.

In the style of a canonical Unix filter program, try something like this:
1
2
3
4
5
6
7
8
9
10
11
# include <algorithm>
# include <iostream>
# include <iterator>
# include <locale>

int main() {
    std::cin.imbue(std::locale("C"));
    using it = std::istream_iterator<char>;
    std::for_each(it{std::cin}, it{}, [](auto c){ 
        std::cout << std::hex << static_cast<int>(c) << '\n'; });
}

Demo:
http://coliru.stacked-crooked.com/a/c8762d9ebd1b6775
Topic archived. No new replies allowed.