How do you convert a string to and from hex.
Printable View
How do you convert a string to and from hex.
k i've got the tohex
Note the ifs are not really necessary I just need to check for them in this particular case.Code:private string Hex(string sData)
{
string temp = "", newdata = "";
for (int i = 0;i < sData.Length;i++)
{
if ((sData.Length - (i + 1)) > 0)
{
temp = sData.Substring(i,2);
if (temp == @"\n") newdata += "0A";
else if (temp == @"\b") newdata += "20";
else if (temp == @"\r") newdata += "0D";
else if (temp == @"\c") newdata += "2C";
else if (temp == @"\\") newdata += "5C";
else if (temp == @"\0") newdata += "00";
else if (temp == @"\t") newdata += "07";
else
{
newdata += String.Format("{0:X}", (int)(sData.ToCharArray())[i]);
i--;
}
}
else
{
newdata += String.Format("{0:X}", (int)(sData.ToCharArray())[i]);
}
i++;
}
return newdata;
}
I still need a dehex method tho :( here is my vb one
VB Code:
Private Function DeHex(Data As String) As String Dim iCount As Integer, Temp As String For iCount = 1 To 33 Step 2 Temp = Mid$(Data, iCount, 2) If Temp = "0A" Then DeHex = DeHex & "\n" ElseIf Temp = "20" Then DeHex = DeHex & "\b" ElseIf Temp = "0D" Then DeHex = DeHex & "\r" ElseIf Temp = "2C" Then DeHex = DeHex & "\c" ElseIf Temp = "5C" Then DeHex = DeHex & "\\" ElseIf Temp = "00" Then DeHex = DeHex & "\0" ElseIf Temp = "07" Then DeHex = DeHex & "\t" Else DeHex = DeHex & Chr$(Val("&H" & Temp)) End If Next iCount End Function
What kind of hex conversion? Do you want to convert each character to its hex representation, or the number that the string represents?
I have already figured out how to convert the string to hex I still need to figure out how to convert the hex value to a string.
But after which scheme do you do the conversion?
The equivalent of the Chr function is a simple char cast:
char c = (char)65;
The Val function would be replaced by some method of int or uint, but I don't have a reference right here.
this is vb.net code and I'm lazy to change it unless you pay me but I wrote it and it's coolish... converts a string to hax and back the hard way(note there is an easy way using the framework but i don't remember it now something about IFormatProvider format X and G and some stream read as binary but this is the code i have....
VB Code:
Private Function AsciiToHex(ByVal str As String) As String Dim i As Short, sRet As String For i = 1 To CShort(Len(str)) sRet = sRet & Hex(Asc(Mid$(str, i, 1))) Next AsciiToHex = sRet End Function Private Function HexToAscii(ByVal str As String) As String Dim i As Short, sRet As String For i = 1 To CShort(Len(str)) Step 2 sRet = sRet & Chr(CInt(HexToDecimal(Mid$(str, i, 2)))) Next i HexToAscii = sRet End Function 'Note: This Function will eventualy overflow if you pass to much hex at once Private Function HexToDecimal(ByVal str As String) As Double Dim i As Short, ii As Short, dRet As Double, sRet As String, sTmp As String, a() As Double ReDim a((Len(str) - 1)) For i = CShort(Len(str)) To 1 Step -1 sTmp = UCase(Mid$(str, i, 1)) Select Case sTmp Case "0" a(ii) = 0 Case "1" a(ii) = 1 * (16 ^ ii) Case "2" a(ii) = 2 * (16 ^ ii) Case "3" a(ii) = 3 * (16 ^ ii) Case "4" a(ii) = 4 * (16 ^ ii) Case "5" a(ii) = 5 * (16 ^ ii) Case "6" a(ii) = 6 * (16 ^ ii) Case "7" a(ii) = 7 * (16 ^ ii) Case "8" a(ii) = 8 * (16 ^ ii) Case "9" a(ii) = 9 * (16 ^ ii) Case "A" a(ii) = 10 * (16 ^ ii) Case "B" a(ii) = 11 * (16 ^ ii) Case "C" a(ii) = 12 * (16 ^ ii) Case "D" a(ii) = 13 * (16 ^ ii) Case "E" a(ii) = 14 * (16 ^ ii) Case "F" a(ii) = 15 * (16 ^ ii) Case Else 'error handle coming later when i think of an error 'umm just only pass hex ok End Select ii = CShort(ii + 1) Next i For i = 0 To CShort(UBound(a)) dRet = dRet + a(i) Next i HexToDecimal = dRet End Function
sorry about the vbness of the code but you should get the ide and the Bee Man told you the important part
k I think I've got it now
Code:private string DeHex(string sData)
{
const string hexnums = "0123456789ABCDEF";
string temp = "", newdata = "";
int n = 0;
for (int i = 0;i < sData.Length;i++)
{
temp = sData.Substring(i,2);
if (temp == "0A") newdata += @"\n";
else if (temp == "20") newdata += @"\b";
else if (temp == "0D") newdata += @"\r";
else if (temp == "2C") newdata += @"\c";
else if (temp == "5C") newdata += @"\\";
else if (temp == "00") newdata += @"\0";
else if (temp == "07") newdata += @"\t";
else
{
n = (int)((hexnums.IndexOf(temp.Substring(1,1)) * Math.Pow(16,0)) + (hexnums.IndexOf(temp.Substring(0,1)) * Math.Pow(16,1)));
newdata += (char)n;
}
i++;
}
return newdata;
}
How about this :rolleyes:
Code:// to Hex
int i = 42881;
string s = Convert.ToString(i, 16);
// from Hex:
string s = "AD04";
int i = Convert.ToInt32(s, 16);
I would have posted this had I believed this is what he wants.
But...
Math.Pow(16,0)
is a constant expression with the value 1
and
Math.Pow(16,1)
is a constant expression with the value 16
so why waste processing time during execution?
int i = 10;
string hex = i.ToString("X");
will do it :wave: :wave:
PT thar's what I was talking about using the format provider but I had never done it unless I was reading with a BinaryStream....to think it's that easy is going back from hex that simple?
CornedBee what do you mean about the proccess time?
The time your app needs to run. Calculating the power of something in a generic way is slow, and calculating the power to 1 or 0 is simply stupid, because x^1 is always x and x^0 is always 1.
I see what you mean. I actually thought about that when I wrote that code year or two ago but I decieded to leave the calculation as to be clear about what was going on, and because in this day and age we don't have to worry as much as oh say if this was a 286. The original code was in VB6 and I just pasted into .net a few yers ago as one of my first .net apps and changed the types. To be honest I was never happy with the way that conversion worked because it could overflow and because it was redundant and seemed wastefull but it did do the job and I didn't know about the IFormatProvider or the Math namespace back then.
I seem to remember think something along the lines of if I was going for fast code I wouldn't be using VB anyway.
I used that code in an app that was simpley for lite encryption for posting private messages in public and it never seemed to suffer from being slow. Ha. Infact I wrote for a thread on this foroum called a big project it has something lke 300 posts and is pretty funny. the thread
hellabantarious as hellswraith said
Just because you use a bad language is no reason to write bad code ;)
Very true. :)