Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have data in hex form when i convert in C# windows form it give
Name
Mobile
CNIC
R-ID

Name:Wajahat Abbas
35 37 20 36 31 20 36 41 20 36 31 20 36 38 20 36 31 20 37 34 20 32 30 20 34 31 20 36 32 20 36 32 20 36 31 20 37 33 20 30 30 20 30 30 20 30 30 20

Cellno:03455172815
33 30 20 33 33 20 33 34 20 33 35 20 33 35 20 33 31 20 33 37 20 33 32 20 33 38 20 33 31 20 33 35 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30

CNIC:61101-0310105-1
33 36 20 33 31 20 33 31 20 33 30 20 33 31 20 32 44 20 33 30 20 33 33 20 33 31 20 33 30 20 33 31 20 33 30 20 33 35 20 32 44 20 33 31 20 30 30

ID:T-000039
35 34 20 32 44 20 33 30 20 33 30 20 33 30 20 33 30 20 33 33 20 33 39 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30

All data present in 16 bytes for e.g name = 16 bytes , cell = 16 bytes ...
If i want to convert it in hexa how can i get the bytes for e.g i want to pick the data
from 1 to 16 is for NAME, then 17 to 32 is fir CELL .... and so on
Guide me in a code how i get this bytes in sequence so that i can add the data in database.
Thanks
Posted

The following code will convert a string of hex characters into their actual values. You may need to modify according to the format of your source string.
C#
// source = hex bytes 
int i = 0;
int index = 0;
byte[] hexBytes = new byte[source.Length];
do
{
    string hs = source.Substring(index, 2);
    hexBytes[i] = (byte)int.Parse(hs, System.Globalization.NumberStyles.AllowHexSpecifier);
    i++;
    index += 2;
} while (index < source.Length);
 
Share this answer
 
This
C#
public static string from_hex(string s)
    {
      StringBuilder sb = new StringBuilder();
      string [] a = s.Split( new  char[]{' '});
      foreach (var h in a)
      {
        sb.Append((char) int.Parse(h, System.Globalization.NumberStyles.HexNumber));
      }
      return sb.ToString();
    }

should do the trick.
Please note, however, in your examples, hexadecimal data do NOT match with text.
 
Share this answer
 
Comments
Muhammad Amman 31-Oct-13 9:47am    
I have done conversion but i want take values from packet how can i detect
for e.g. byte 1-16 is name and byte 17-32 is cell no and byte 33-48 is cnic.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900