|
-
Nov 23rd, 2007, 11:39 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Convert to Hexadecimal
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!
-
Nov 24th, 2007, 12:44 AM
#2
Hyperactive Member
Re: Convert to Hexadecimal
Try to use the namespace
Code:
using Microsoft.VisualBasic;
From there you can have a function Conversion to Hex.
-
Nov 24th, 2007, 06:32 AM
#3
Re: Convert to Hexadecimal
 Originally Posted by fret
Try to use the namespace
Code:
using Microsoft.VisualBasic;
From there you can have a function Conversion to Hex.
You don't need to use the Hex function even in VB.
CSharp Code:
foreach (char ch in myString)
{
MessageBox.Show(Convert.ToInt32(ch).ToString("X"));
}
-
Nov 24th, 2007, 09:45 PM
#4
Thread Starter
Fanatic Member
Re: Convert to Hexadecimal
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:
Code:
string txt = "ACOS";
string ans = Convert.ToInt32(txt, 16).ToString();
//I use Convert.Toint32(string value, int frombase) overload
ERROR:
Additional non-parsable characters are at the end of the string.
-
Nov 24th, 2007, 10:07 PM
#5
Re: Convert to Hexadecimal
Look again at my code. Did I try to convert the entire string in one go?
-
Nov 25th, 2007, 08:22 PM
#6
Thread Starter
Fanatic Member
Re: Convert to Hexadecimal
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...
-
Nov 25th, 2007, 08:55 PM
#7
Frenzied Member
Re: Convert to Hexadecimal
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
-
Nov 25th, 2007, 09:12 PM
#8
Thread Starter
Fanatic Member
Re: Convert to Hexadecimal
Thanks for that Fromethius.
The problem with your code is that you did not follow exactly what john provided.
Actually, i used john code/solution.
He gave you a completely valid solution and you tried writing your own
Im just curious using Convert.ToInt32(Variable, 16) functions...well anyway, thanks again..It reall helps..
-
Nov 28th, 2007, 02:15 AM
#9
Thread Starter
Fanatic Member
Re: Convert to Hexadecimal
Is it posible to instead show the hex value just save it to an array of bytes?
Code:
foreach (char ch in myString){ MessageBox.Show(Convert.ToInt32(ch).ToString("X"));}
I've tried the code below but i got no luck...
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"));
}
Error Msg:
Input string was not in a correct format.
By the way, the error appears when it comes to convert the O from "ACOST".
-
Nov 28th, 2007, 03:34 AM
#10
Re: Convert to Hexadecimal
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]);
}
-
Nov 28th, 2007, 04:05 AM
#11
Thread Starter
Fanatic Member
Re: Convert to Hexadecimal
Thanks for that John..but the value assigned to data[n] is the decimal value of "ACOST" not the Hexadecimal value.
-
Nov 28th, 2007, 05:50 AM
#12
Re: Convert to Hexadecimal
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:
Code:
int n1 = 255;
int n2 = 0xFF;
do those variables contain different things? One a decimal number and one a hexadecimal number?
-
Nov 28th, 2007, 08:01 PM
#13
Thread Starter
Fanatic Member
Re: Convert to Hexadecimal
Yah, Im confused...That is so informative...Thanks a lot john.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|