1

I am using BitConverter.ToString(bytes) for converting by string to hexadecimal string which I further convert it into integer or float.

But the input stream consist of 0 to show that byte value is 0. So suppose I have an integer which is represented by 2 bytes of input starting at position x and the first consist of EE while 2nd byte is 00. Now when I use BitConverter.ToString(bytes, x, 2).Replace ("-","") I get output as EE00 whose integer value is 60928 but in this case the output should be 238 that is converting only first byte EE to integer.

But in some other case the 2 bytes might be EE01 whose integer value will 60929 which is correct in this case.

Any suggestion how can I solve my problem?

Since some people are saying that question is confusing I will restate my problem I have long hexadecimal string as input. In hexadecimal string the

1) First 12 bytes represent string. 2) next 11 bytes also represent some other string. 3) Next 1 byte represent integer. 4) Next 3 bytes represent integer. 5) Next 4 bytes represent integer. 6) Next 4 bytes represent float. 7) Next 7 bytes represent string. 8) Next 5 bytes represent integer.

So for 4th case if bytes are ee 00 00 then I should neglect 0's and convert ee to integer. But if it ee 00 ee then I should convert ee00ee to integer. Also every time I will be following same pattern as mentioned above.

prattom
  • 1,625
  • 11
  • 42
  • 67
  • You're post is a little confusing - are you saying that "00" marks the end of the stream? If so, you shouldn't even be passing it to the converter... – RB. Aug 19 '13 at 10:23
  • 1
    Have a look at this http://en.wikipedia.org/wiki/Endianness – LunicLynx Aug 19 '13 at 10:23
  • `int result = int.Parse(string.Join("", input.Select((item, inx) => new { item, inx }).GroupBy(x => x.inx / 2).Select(g => new string(g.ToArray())).Where(s => s != "00" && s.Length != 2));` – It'sNotALie. Aug 19 '13 at 10:25
  • I want to say suppose I have long hexadecimal string like 77 77 87 53 23 24 00 00 00 29 84 84 ee 00 ee. Now suppose I need some int values from this string like first 4 bytes represent some int so I will take 77 77 87 53 and I will convert it into integer or float. Now suppose for some other case I take again 4 bytes 24 00 00 00 so in this case I should neglect 0's after 24 and convert 24 hexadecimal into integer but in some other input this value might be 24 00 00 01 so in this case I need to consider hexadecimal 24000001 and convert it into integer. – prattom Aug 19 '13 at 10:32
  • @user2673943 Why don't you use simply `BitConverter.ToInt32(buf,0);`. It would work as you described. – I4V Aug 19 '13 at 10:35
  • But I think it will not work in case I need only 3 or 5 bytes of input for some int value. – prattom Aug 19 '13 at 10:39
  • @user2673943 `0x23,0,0,0` will be converted to `0x23` and `0x23,0,0,1` to `16777251`. What exactly do you want... – I4V Aug 19 '13 at 10:41
  • suppose I need to convert 0x23,0,0 only to integer or 0x23,0,1. – prattom Aug 19 '13 at 10:43
  • http://stackoverflow.com/a/5919521/659190 – Jodrell Aug 19 '13 at 10:45
  • @user2673943 append 1 more byte and then call ToInt32 :) – I4V Aug 19 '13 at 10:46
  • Your question is very confused, or perhaps thats just me. – Jodrell Aug 19 '13 at 10:46

2 Answers2

0

This method converts a hex string to a byte array.

public static byte[] ConvertHexString(string hex)
{ 
  Contract.Requried(!string.IsNullOrEmpty(hex));

  // get length
  var len = hex.Length;

  if (len % 2 == 1)
  {
    throw new ArgumentException("hexValue: " + hex);
  }

  var lenHalf = len / 2;

  // create a byte array
  var bs = new byte[lenHalf];

  try
  {
    // convert the hex string to bytes
    for (var i = 0; i != lenHalf; i++)
    {
      bs[i] = (byte)int.Parse(hex.Substring(i * 2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
    }
  }
  catch (Exception ex)
  {
    throw new ParseException(ex.Message, ex);
  }

  // return the byte array
  return bs;
}

From the other side:

public static string ConvertByteToHexString(byte num)
{
  var text = BitConverter.ToString(new[] { num });

  if (text.Length == 1)
  {
    text = "0" + text;
  }

  return text;
}
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • 1
    There is already one built-in. `var byteArray = SoapHexBinary.Parse(hexString).Value` – I4V Aug 19 '13 at 11:41
  • 1
    but here you need a referance to using System.Runtime.Remoting.Metadata.W3cXsd2001; which some ppl do not want this link. – Bassam Alugili Aug 19 '13 at 11:46
0

My problem has been solved. I was making a mistake of Endianness. I was receiving the data as EE 00 and I should have taken it as 00 EE before converting to integer. Thanks to all who gave me solution for my problem and sorry for missing out this important fact from question.

prattom
  • 1,625
  • 11
  • 42
  • 67