Thread: Uppercase Hex

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Uppercase Hex

    I am trying to output some hex values into a file. Every way i try to make the hex letters uppercase just puts 200 infront of the values and doesnt capitalize it...

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    unsigned char Memory[0xFFF];
    unsigned short OpCode;
    
    void LoadRom(char *sFileName)
    {
      ifstream Rom(sFileName);
    
      if(!Rom.is_open()) {
        cout << "Error Opening " << sFileName;
        exit(1);
      }
    
      long Start = Rom.tellg();
      Rom.seekg(0, ios::end);
      long End = Rom.tellg();
      Rom.seekg(0, ios::beg);
      long FileSize = End - Start;
    
      Rom.read(Memory, FileSize);
    
      Rom.close();
    }
    
    void WriteFile(char *sFileName)
    {
      ofstream Log(sFileName, ios::app);
    
      if(!Log.is_open()) {
        cout << "Error Opening " << sFileName;
        exit(1);
      }
    
      for(int loop = 0x000; loop < 0xFFF; loop+=2) {
        OpCode = (Memory[loop] << 8) + Memory[loop + 1];
        Log << hex;
        Log << "0x" << OpCode << endl;
      }
    
      Log.close();
    }
    
    int main()
    {
      char sFileName[80];
    
      cout << endl;
      cout << "Chip8 DisAssem" << endl;
      cout << "--------------------" << endl << endl;
      cout << "Enter Rom Name: ";
      cin  >> sFileName;
    
      LoadRom(sFileName);
    
      cout << "Writing...";
    
      WriteFile("Asm.txt");
    
      cout << endl;
      return 0;
    }
    How can I make the hex letters uppercase?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe take a look at C++ I/O Flags?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I know "how" to set uppercase... but when I do.. it doesnt...
    All it does is either

    a) messes up the values
    b) Adds 200 to every line....

    !?!?! its freaking me out

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
        Log << hex << uppercase << "0x" << OpCode << endl;

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    ok...

    Code:
    Log << hex << ios::uppercase << "0x" << OpCode << endl;
    then in the "log" file its

    2000x6e05

    I want it to be

    0x6E05

    That 200 only goes there if i do the ios::uppercase

    edit: My compiler(gcc/Dev-C++) wont accept just uppercase... it has to be ios::uppercase.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >ios::uppercase

    You don't want the ios:: there, just uppercase. ios::uppercase outputs the value of the ios::uppercase flag, which apparently is 200 hex. ios::uppercase is used with the setf() or setiosflags() function:
    cout.setf(ios::uppercase);

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ah, thank you

    One more thing...

    How can I stop it from dropping 0's off the end of the values.

    like if the value is 0xEE00 it prints 0xEE, how can I save my zero's!
    Last edited by Vicious; 07-11-2004 at 01:47 AM.

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    like if the value is 0xEE00 it prints 0xEE, how can I save my zero's!
    You are writing the file with one byte per line, so why would you want those zeros there? A hex byte only has 2 digits, so the value will never be 0xEE00.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Something like this?
    Code:
        Log << hex << uppercase << "0x" << setfill('0') << setw(4) << OpCode << endl;

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    The local fstream tutorial isn't exactly indepth on i/o stream functions. So while I was still stuck with getch(ch), I wrote this function:
    Code:
    string itox( unsigned int i )
    {
    	char x[7] = "000000";
    	int j = 0;
    	
    	// lookup table
    	const char xtable[17] = "0123456789ABCDEF";
    
    	// Get hex from int
    	for (j = 6; (j >= 0) && i; j--)
    	{
    		x[j] = xtable[i & 0xF];
    		i = i >> 4;
    	}
    
    	return x;
    }
    It can easily be modified to output more or less digits, or even a completely different base system. Maybe even a dynamic base system...

  11. #11
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    so the value will never be 0xEE00.
    Yes, but if you look carefuly, each "value" im writing to the file is compose of two bytes.

    This program is takin a Chip8 rom and writing the instructions out into a log file. Each instruction is 2 bytes long.

  12. #12
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Well, 0xEE == 0x00EE, as opposed to 0xEE00.

  13. #13
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    yeah 0x00EE... didnt i say that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 07:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM