Results 1 to 3 of 3

Thread: Hex / Binary

  1. #1

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Hex / Binary

    Code:
    byte[] buff = new byte[4];
    buff[0] = 0x34;
    buff[1] = 0x23;
    buff[2] = 0x00;
    buff[3] = 0xF2;
    
    
    FileStream fs = new FileStream("hex.bin", FileMode.Create, FileAccess.ReadWrite);
    BinaryWriter bw = new BinaryWriter(fs);
    bw.Write(buff);
    bw.Close(); 
    Console.WriteLine("Written to file : hex.bin");
    Right. That writes (34 23 00 F2) to a file in binary (specifically NOT text).

    I would like to read a hex number (lets just say 342300F2) from a text file and encode that same hex number to a binary file. I cant work out how to process the text into binaray. Any ideas?
    Last edited by francisstokes; Nov 3rd, 2009 at 10:23 AM.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Hex / Binary

    Something like this?


    csharp Code:
    1. public void ProcessHex()
    2.         {
    3.             string yourHex = "342300F2";
    4.             string binary = string.Empty;
    5.             if (!yourHex.TryBinaryParse(ref binary))
    6.             {
    7.                 Console.WriteLine("Invalid hex format");
    8.             }
    9.             else
    10.             {
    11.                 Console.WriteLine(binary);
    12.             }
    13.         }


    csharp Code:
    1. public static class Extensions
    2.     {
    3.         public static bool TryBinaryParse(this string hexInput, ref string result)
    4.         {
    5.             try
    6.             {
    7.                 if (!System.Text.RegularExpressions.Regex.Match(hexInput,
    8.                                                                 "^[A-Fa-f0-9]+$").Success)
    9.                 {
    10.                     return false;
    11.                 }
    12.  
    13.                 hexInput = hexInput.ToUpper();
    14.  
    15.                 Dictionary<char, string> lookup = new Dictionary<char, string>(16);
    16.                 {
    17.                     lookup.Add('0', "0000");
    18.                     lookup.Add('1', "0001");
    19.                     lookup.Add('2', "0010");
    20.                     lookup.Add('3', "0011");
    21.                     lookup.Add('4', "0100");
    22.                     lookup.Add('5', "0101");
    23.                     lookup.Add('6', "0110");
    24.                     lookup.Add('7', "0111");
    25.                     lookup.Add('8', "1000");
    26.                     lookup.Add('9', "1001");
    27.                     lookup.Add('A', "1010");
    28.                     lookup.Add('B', "1011");
    29.                     lookup.Add('C', "1100");
    30.                     lookup.Add('D', "1101");
    31.                     lookup.Add('E', "1110");
    32.                     lookup.Add('F', "1111");
    33.                 }
    34.  
    35.                 result = string.Join(string.Empty,
    36.                                      hexInput.ToCharArray().Select((c) => lookup[c]).ToArray());
    37.  
    38.                 return true;
    39.             }
    40.             catch
    41.             { return false; }
    42.         }
    43.  
    44.     }

    I thought it might be cool as an extension.

  3. #3

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: Hex / Binary

    Ok i dont think ive been clear. What im looking for is if i were to open a file in a hex editor, i would see this:

    Code:
    00000: 34 23 00 F2  xx xx xx xx    ..... ...B.
    I want to take text versions of those hex symbols [34, 23, 00, F2] and encode them in hex. If i were encoding in text the file would look like this:

    Code:
    00000: 33 34 32 33 30  30 46 32      34230 0F2..
    I hope that clears things up.

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