Converting HEX to String ?  
Author Message
robben07160





PostPosted: Visual C++ Language, Converting HEX to String ? Top

Hi,

 

I have an hexadecimal value stored as an unsigned long (C++ .NET),  I use the value to extract data from an XML file therefore the file contains hexadecimal values also.

I use SelectNodes to extract the necessary information and therefore I need to change the Hex value to a string.  However, as I convert it to a String the value changes, and as the XML file contains hexadecimal it can’t find the hex value in the XML file.  My question is therefore can I convert a Hex value in .NET to a string without changing its value otherwise I can’t extract the necessary information from the file.

 

Thanks in advance for your help!



Visual C++9  
 
 
Brian Kramer





PostPosted: Visual C++ Language, Converting HEX to String ? Top

"I have an hexadecimal value stored as an unsigned long "

This is sort of confusing.  You have an integer, basically.  The actual representation of integers are always binary, but we never "see" it, so it's not interesting to talk about.  When output (say by the de****, or by printf, or to a string), this integer can be shown in decimal (base-10) or hexadecimal (base-16), among other possibilities.

So I think your question really is: "How do I convert the integer to a string of hexadecimal digits "  With C++/CLI, you can use String.Format.

Example:

int main(array<System::String ^> ^args)
{
  int myval = 0xABBA;
  String^ mystr = String::Format("As decimal: {0}, as hex: {1:X}", myval, myval );
  Console::WriteLine( mystr );
  return 0;
}

This outputs:

As decimal: 43962, as hex: ABBA

 


 
 
Marius Bancila





PostPosted: Visual C++ Language, Converting HEX to String ? Top

I have an hexadecimal value stored as an unsigned long

See this FAQ: what are different numbers representations



 
 
robben07160





PostPosted: Visual C++ Language, Converting HEX to String ? Top

Many thanks to both of you for your help!