Hi guys help please..I have a variable string 'myVar' with value 'MYSTRING", my question is, what is the easiest way to convert each character into hexadecimal? Thanks in advance guys!
Printable View
Hi guys help please..I have a variable string 'myVar' with value 'MYSTRING", my question is, what is the easiest way to convert each character into hexadecimal? Thanks in advance guys!
Try to use the namespace
From there you can have a function Conversion to Hex.Code:using Microsoft.VisualBasic;
You don't need to use the Hex function even in VB.Quote:
Originally Posted by fret
CSharp Code:
foreach (char ch in myString) { MessageBox.Show(Convert.ToInt32(ch).ToString("X")); }
Thanks for that guys...But, i tried the code below hoping that it would work but i got no luck it has an error..Can you check guys if there is something im missing? by the way, the error occurs on the 2nd line of my code (bold text) Thanks in advance!
CODE:
ERROR:Code:string txt = "ACOS";
string ans = Convert.ToInt32(txt, 16).ToString();
//I use Convert.Toint32(string value, int frombase) overload
Quote:
Additional non-parsable characters are at the end of the string.
Look again at my code. Did I try to convert the entire string in one go?
I found the problem.(I hope its right)..since "O" & "S" in ACOS is not a valid hexadecimal value (0,1,2,3...9,A,B,C,D,E,F) it cannot convert it... thats why the error says "Additional non-parsable characters are at the end of the string." Thanks a lot guys for the help...
I don't think that is the reason. I'm pretty sure that to convert a string to hexi-decimal, you take each character in the character array (string) and convert it to its integral character value and then with that value, perform operations to convert it to its hex value. The operations used escape me. You shouldn't have to know them anyway. I'm almost positive that the .Net Framework provides an easy and convenient way to convert a string to its hexi-decimal value.
I think that we have all agreed that to convert an integral value to hex, you use ToString("X"). That being the case, the first task would be to convert each character value to its integral character value. This could be accomplished using a foreach loop to iterate through each character in the string, and then cast that character to its integral character value. Then, with that value, use the ToString("X") function to convert it to its hex value. Also, I think you can use the Convert.ToInt32(variable, 16) function as well to do such a thing.
The problem with your code is that you did not follow exactly what john provided. He gave you a completely valid solution and you tried writing your own. You cannot convert the entire string to hexidecimal in one blow. You must convert it one character at a time. This being the case, it can be very cumbersome trying to do this in code, which is why you should write your own function that takes in a string as a parameter and then iterates through each character in the string and then performs the cast to the integer and converting to the hex value, and then adding that value to a temporary string. When the foreach is finished, you should return that temporary string value.
I hope that helps :)
Thanks for that Fromethius.
Actually, i used john code/solution.Quote:
The problem with your code is that you did not follow exactly what john provided.
Im just curious using Convert.ToInt32(Variable, 16) functions...well anyway, thanks again..It reall helps..Quote:
He gave you a completely valid solution and you tried writing your own
Is it posible to instead show the hex value just save it to an array of bytes?
I've tried the code below but i got no luck...Code:foreach (char ch in myString){ MessageBox.Show(Convert.ToInt32(ch).ToString("X"));}
Error Msg:Code:string tmp = "ACOST";
byte[] tmpbyt = new byte[255];
for (int i = 0; i < tmp.Length; i++)
{
tmpbyt[i] = Convert.ToByte(Convert.ToInt32(tmp[i]).ToString("X"));
}
By the way, the error appears when it comes to convert the O from "ACOST".Quote:
Input string was not in a correct format.
Why convert a char to an int to a string to a byte when you can just convert from a char to a byte?Code:string str = "ACOST";
byte[] data = new byte[str.Length];
for (int i = 0; i < str.Length; i++)
{
data[i] = Convert.ToByte(str[i]);
}
Thanks for that John..but the value assigned to data[n] is the decimal value of "ACOST" not the Hexadecimal value.
You are confused. Hexadecimal is just a way to represent numbers. It doesn't mean the numbers are a different type of animal. Decimal is just one of many ways to represent a number. A number stored in a computer isn't decimal or hexadecimal. It's stored in binary form. If a Byte variable contains the value 11111111 then it will be displayed by the IDE as 255 because decimal is the default way to display numbers, but it's still the number FF too. They're all just different representations of the same thing. How about this:do those variables contain different things? One a decimal number and one a hexadecimal number?Code:int n1 = 255;
int n2 = 0xFF;
Yah, Im confused...That is so informative...Thanks a lot john.