Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an hex string
B80C001061020000690200006B0200006D0200006F i have converted this hex string into byte array using stringtobytearray method.
public static byte[] StringToByteArray(String hex)
  int NumberChars = hex.Length;
  byte[] bytes = new byte[NumberChars / 2];
  for (int j = 0; j < NumberChars; j += 2)
      bytes[j / 2] = Convert.ToByte(hex.Substring(j, 2), 16);
  return bytes;

but this byte array contains ASCII values like B8 = 184, 0c = 12, and i want to convert this byte array of ASCII value into byte array of hex value.
Updated 28-Nov-13 19:11pm
BillWoodruff 29-Nov-13 2:08am    
Is this function one that you are going to call frequently ... one that will process very large strings ? The answer to those question could have an important impact on your code strategy.
bunty swapnil 29-Nov-13 2:19am    
yes every time i am calling this function because if i will read string as a character the character array size will be the length of string but using stringtobytearray will get array half of length of string. i makes pairs of 2 char like B8 = 184,0c = 12.
in byte array values is coming in ASCII format so i want to convert byte array of ASCII values into Byte array of hex value.
And condition is
We should get equal size of ASCII value byte array and hex value byte array.
BillWoodruff 29-Nov-13 2:23am    
Every ASCII character in a string, if converted to hex, takes two bytes to represent. If you are reading a character per read, why are you converting to hex ?

I think to answer your concerns intelligently requires some knowledge of what it is you are reading, and what the frequency ... or "volume" ... of reads is.

Another important consideration is whether error checking is required.
Richard MacCutchan 29-Nov-13 5:23am    
It is already a byte array of hex values so why are you trying to convert it? Your question is not clear as to what problem you are trying to solve.

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