Results 1 to 15 of 15

Thread: Hex Conversion

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188

    Hex Conversion

    How do you convert a string to and from hex.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    k i've got the tohex

    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;
    }
    Note the ifs are not really necessary I just need to check for them in this particular case.

    I still need a dehex method tho here is my vb one

    VB Code:
    1. Private Function DeHex(Data As String) As String
    2.   Dim iCount As Integer, Temp As String
    3.   For iCount = 1 To 33 Step 2
    4.     Temp = Mid$(Data, iCount, 2)
    5.     If Temp = "0A" Then
    6.       DeHex = DeHex & "\n"
    7.     ElseIf Temp = "20" Then
    8.       DeHex = DeHex & "\b"
    9.     ElseIf Temp = "0D" Then
    10.       DeHex = DeHex & "\r"
    11.     ElseIf Temp = "2C" Then
    12.       DeHex = DeHex & "\c"
    13.     ElseIf Temp = "5C" Then
    14.       DeHex = DeHex & "\\"
    15.     ElseIf Temp = "00" Then
    16.       DeHex = DeHex & "\0"
    17.     ElseIf Temp = "07" Then
    18.       DeHex = DeHex & "\t"
    19.     Else
    20.       DeHex = DeHex & Chr$(Val("&H" & Temp))
    21.     End If
    22.   Next iCount
    23. End Function
    Last edited by Tewl; Feb 2nd, 2004 at 10:19 PM.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What kind of hex conversion? Do you want to convert each character to its hex representation, or the number that the string represents?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    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.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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:
    1. Private Function AsciiToHex(ByVal str As String) As String
    2.         Dim i As Short, sRet As String
    3.         For i = 1 To CShort(Len(str))
    4.             sRet = sRet & Hex(Asc(Mid$(str, i, 1)))
    5.         Next
    6.         AsciiToHex = sRet
    7.     End Function
    8.  
    9.     Private Function HexToAscii(ByVal str As String) As String
    10.         Dim i As Short, sRet As String
    11.         For i = 1 To CShort(Len(str)) Step 2
    12.             sRet = sRet & Chr(CInt(HexToDecimal(Mid$(str, i, 2))))
    13.         Next i
    14.         HexToAscii = sRet
    15.     End Function
    16.  
    17.     'Note: This Function will eventualy overflow if you pass to much hex at once
    18.     Private Function HexToDecimal(ByVal str As String) As Double
    19.         Dim i As Short, ii As Short, dRet As Double, sRet As String, sTmp As String, a() As Double
    20.         ReDim a((Len(str) - 1))
    21.         For i = CShort(Len(str)) To 1 Step -1
    22.             sTmp = UCase(Mid$(str, i, 1))
    23.             Select Case sTmp
    24.                 Case "0"
    25.                     a(ii) = 0
    26.                 Case "1"
    27.                     a(ii) = 1 * (16 ^ ii)
    28.                 Case "2"
    29.                     a(ii) = 2 * (16 ^ ii)
    30.                 Case "3"
    31.                     a(ii) = 3 * (16 ^ ii)
    32.                 Case "4"
    33.                     a(ii) = 4 * (16 ^ ii)
    34.                 Case "5"
    35.                     a(ii) = 5 * (16 ^ ii)
    36.                 Case "6"
    37.                     a(ii) = 6 * (16 ^ ii)
    38.                 Case "7"
    39.                     a(ii) = 7 * (16 ^ ii)
    40.                 Case "8"
    41.                     a(ii) = 8 * (16 ^ ii)
    42.                 Case "9"
    43.                     a(ii) = 9 * (16 ^ ii)
    44.                 Case "A"
    45.                     a(ii) = 10 * (16 ^ ii)
    46.                 Case "B"
    47.                     a(ii) = 11 * (16 ^ ii)
    48.                 Case "C"
    49.                     a(ii) = 12 * (16 ^ ii)
    50.                 Case "D"
    51.                     a(ii) = 13 * (16 ^ ii)
    52.                 Case "E"
    53.                     a(ii) = 14 * (16 ^ ii)
    54.                 Case "F"
    55.                     a(ii) = 15 * (16 ^ ii)
    56.                 Case Else
    57.                     'error handle coming later when i think of an error
    58.                     'umm just only pass hex ok
    59.             End Select
    60.             ii = CShort(ii + 1)
    61.         Next i
    62.         For i = 0 To CShort(UBound(a))
    63.             dRet = dRet + a(i)
    64.         Next i
    65.  
    66.         HexToDecimal = dRet
    67.     End Function

    sorry about the vbness of the code but you should get the ide and the Bee Man told you the important part
    Magiaus

    If I helped give me some points.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    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;
    }

  8. #8
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    How about this

    Code:
    // to Hex
    int i = 42881;
    string s = Convert.ToString(i, 16);
    
    // from Hex:
    string s = "AD04";
    int i = Convert.ToInt32(s, 16);
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    int i = 10;
    string hex = i.ToString("X");

    will do it
    \m/\m/

  11. #11
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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?
    Magiaus

    If I helped give me some points.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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
    Magiaus

    If I helped give me some points.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Just because you use a bad language is no reason to write bad code
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    Very true.
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width