Answered by:
how to convert a char or string to hex?

Question
-
Hi,
I want to convert a string to hexadecimal in vc++. Is there any built in function in vc++ to do this. I am working on visual studio 2008. Just like VB has some in built some in built function. otherwise if there are no built in functions, any code which anyone can share is appreciated.
Thanks & Regards,
P.Karthik
Answers
-
Hi Karthik,
Try this:
string input = "Hello World!"; char[] values = input.ToCharArray(); foreach (char letter in values) { // Get the integral value of the character. int value = Convert.ToInt32(letter); // Convert the decimal value to a hexadecimal value in string form. string hexOutput = String.Format("{0:X}", value); Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput); }
Regards, http://shwetamannjain.blogspot.com- Proposed as answer by Shweta Jain (Lodha) Wednesday, September 14, 2011 9:03 AM
- Marked as answer by Karthik Agarwal Wednesday, September 14, 2011 9:43 AM
-
Why you appended 2 with X.
Try it without 2 as
string ^ hexOutput = System::String:Format("{0:X}", value);
Regards, http://shwetamannjain.blogspot.com- Marked as answer by Karthik Agarwal Wednesday, September 14, 2011 9:43 AM
All replies
-
-
Hi Karthik,
Try this:
string input = "Hello World!"; char[] values = input.ToCharArray(); foreach (char letter in values) { // Get the integral value of the character. int value = Convert.ToInt32(letter); // Convert the decimal value to a hexadecimal value in string form. string hexOutput = String.Format("{0:X}", value); Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput); }
Regards, http://shwetamannjain.blogspot.com- Proposed as answer by Shweta Jain (Lodha) Wednesday, September 14, 2011 9:03 AM
- Marked as answer by Karthik Agarwal Wednesday, September 14, 2011 9:43 AM
-
-
Hi,
I am not sure about VC++. But check out this link:
http://www.codeguru.com/forum/showthread.php?t=231054
Regards, http://shwetamannjain.blogspot.com- Proposed as answer by Shweta Jain (Lodha) Wednesday, September 14, 2011 9:12 AM
-
or you can try converter to convert C# code to VC++
- Edited by Shweta Jain (Lodha) Wednesday, September 14, 2011 9:12 AM
- Proposed as answer by ichiethel Sunday, February 16, 2020 6:20 AM
-
-
Why you appended 2 with X.
Try it without 2 as
string ^ hexOutput = System::String:Format("{0:X}", value);
Regards, http://shwetamannjain.blogspot.com- Marked as answer by Karthik Agarwal Wednesday, September 14, 2011 9:43 AM
-
Hey sorry i am trying to print hex value to output file as 0x46 something like that so. If the value it is returning is lessthan 10 then i need to append another 0 na. Its like getting 2 values after a decimal point for float values. thanks for the help. Got it finally. its {0:X2} rather {0:2X}. The former gives hexadecimal where as later gives 2X as output all the time. I appreciate your help Shweta Jain. Thanks a lot.
-
Try this code.
#include<iostream.h> int main() { unsigned long temp=0, counter=0, OutPut=0; char Letters[10]= "abcd"; // letter characters cout<<Letters; for (counter=1; counter<=strlen(Letters); counter++) { temp = Letters[counter-1]; // get character of char OutPut=OutPut +temp; // add result (temp) to OutPut } OutPut=OutPut ^ 1234; //means1234 h cout<<OutPut<<"\n"; // decimal output by default cout<<hex<<OutPut; // Hex output return 0; }
-
-
-
Thank you all for your time and effort!
Hi Karthik,
Welcome to the MSDN forum!You are more likely to get in touch with more VC++ experts in the Visual C++ forum.Thank you for your understanding!
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Edited by Dummy yoyo Sunday, September 18, 2011 1:02 PM