0

I'm getting inoput from a binary file, in hex.
And I want it to say the first nine characters... like "0x05 0x53,0x84 " ... and so on. So how do I turn a char in hex to a string in text?

3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by firstPerson
0
cin >> hex; //set the input stream to read in hexadecimal 
long num = 0;
cin >> num; //input a number, example 15
cout<<hex<<showbase; // shows output as  0xsomeNumber
cout << num; //prints  0xf instead of 15

you can do the same with ifstream iFile("number.txt")
or ofstream oFile("numbers.txt");

change cout to oFile, and cin to iFile.

0

Try this...

#include <iostream>
#include <string>
#include <sstream>

char HexStrToChar(std::string HexStr)
{
    std::istringstream ist(HexStr);
    int value;
    ist >> std::hex >> value;
    return value;
}

int main()
{
   std::string text = "0x05 0x53 0x84";
   std::istringstream iss(text);
   while(iss >> text)
   {
      std::cout << HexStrToChar(text);
   }
}
0

I can't use cin or cout, it's an SDL program.
Also, it's char TO string, not the other way around.

0

I can't use cin or cout, it's an SDL program.
Also, it's char TO string, not the other way around.

Explain a bit more. Why cant you use C++ with sdl.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.