better int to hex
This commit is contained in:
		
							parent
							
								
									bed0b55e70
								
							
						
					
					
						commit
						4e96a41b4e
					
				@ -4,6 +4,9 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace omni
 | 
					namespace omni
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  // This might be unused but I don't care to search the entire source code
 | 
				
			||||||
 | 
					  // So it will remain here (lol)
 | 
				
			||||||
 | 
					  // The actual int to hex conversion is now done properly
 | 
				
			||||||
  char halfword_to_hex_char(unsigned int input)
 | 
					  char halfword_to_hex_char(unsigned int input)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    if (input > 127)
 | 
					    if (input > 127)
 | 
				
			||||||
@ -53,31 +56,12 @@ namespace omni
 | 
				
			|||||||
    if (input > 255)
 | 
					    if (input > 255)
 | 
				
			||||||
      return "FF";
 | 
					      return "FF";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    std::bitset<8> whole_byte(input);
 | 
					    std::stringstream stream;
 | 
				
			||||||
    //240 represents 11110000, our needed bitmask
 | 
					    stream << std::setfill ('0') << std::setw(sizeof(char)*2) 
 | 
				
			||||||
    uint8_t left_mask_int = 240;
 | 
					       << std::hex << your_int;
 | 
				
			||||||
    std::bitset<8> left_mask(left_mask_int);
 | 
					    std::string result(stream.str());
 | 
				
			||||||
    std::bitset<8> left_halfword((whole_byte & left_mask) >> 4);
 | 
					    std::transform(str.begin(), str.end(), str.begin(), ::toupper);
 | 
				
			||||||
    //likewise, 15 represents 00001111
 | 
					 | 
				
			||||||
    uint8_t right_mask_int = 15;
 | 
					 | 
				
			||||||
    std::bitset<8> right_mask(right_mask_int);
 | 
					 | 
				
			||||||
    std::bitset<8> right_halfword((whole_byte & right_mask));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    unsigned int left = left_halfword.to_ulong();
 | 
					    return result;
 | 
				
			||||||
    unsigned int right = right_halfword.to_ulong();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //std::cout << "now have have " << left << " and " << right << '\n';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    char a = halfword_to_hex_char(left);
 | 
					 | 
				
			||||||
    char b = halfword_to_hex_char(right);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    std::string left_string(1, a);
 | 
					 | 
				
			||||||
    std::string right_string(1, b);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    std::string final_byte = left_string + right_string;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //std::string final_byte = halfword_to_hex_char(left) + "" + halfword_to_hex_char(right);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return final_byte;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
} //namespace omni
 | 
					} //namespace omni
 | 
				
			||||||
 | 
				
			|||||||
@ -4,6 +4,8 @@
 | 
				
			|||||||
#include <bitset>
 | 
					#include <bitset>
 | 
				
			||||||
#include <cstdint>
 | 
					#include <cstdint>
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					#include <algorithm>
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace omni
 | 
					namespace omni
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user