0

I want to build DHCP packet and in the build process i am get params from user for example Agent Circuit Id (string) - attribute number 82. After build my packet i am send this packet and see the result via Wireshark My problem is that when i see my packet i see my Agent Circuit Id value as hex and i want to see this value as byte for example:

string agentCircuitId = "33445566778899"; 

i want this field will be 2 bytes 33 44 55 66 77 88 99

in my result i try the string "1234" and the result is 31 32 33 34

http://s27.postimg.org/bdhe9j4hv/123.png

Any idea how to do that ?

  • Should the values of the bytes be decimal 12 and 34? Or should the byte values be hexadecimal 0x12 and 0x34 (i.e., the string being a hex number)? –  Jun 08 '14 at 17:44
  • the string is hex number – user3637066 Jun 08 '14 at 17:46
  • You might look at http://stackoverflow.com/questions/1139957/c-sharp-convert-integer-to-hex-and-back-again for converting the hex-string to a number and set that number for the "Agent Circuit ID" option. Make sure you use the `short` type (= 16-bit integer) –  Jun 08 '14 at 17:48
  • I didn't understand why is good for my purpose – user3637066 Jun 08 '14 at 17:53
  • What did you not understand? –  Jun 08 '14 at 17:54
  • My input is string, in order to convert it i need int/byte[]/string ? – user3637066 Jun 08 '14 at 18:01
  • No, look at the SO question i linked to more carefully. It is called "convert integer to hex and **back again**", that means it (and its answers) cover both conversion from *int* to *hex string*, and conversion from **hex string to int**. The latter you are interested in (in your case just use *short* instead of *int*, because you are apparently dealing with a 16-bit value) –  Jun 08 '14 at 18:04
  • Please see my update, this is what i need (i have changed the example) - your given link is still what i need ? – user3637066 Jun 08 '14 at 18:05
  • Ah, i see. You can actually have an arbitrary number of bytes, correct? (please also update the text below the example - it still mentions "12" and "34") –  Jun 08 '14 at 18:06
  • Alright. SO also has already a topic with plenty of answers (one surely will fit your needs): [How do you convert Byte Array to Hexadecimal String, and vice versa?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa) –  Jun 08 '14 at 18:10
  • I am using byte[] StringToByteArray(String hex) and it works fine if my string is "1234" and return 2 bytes but in case my string is "12345" the return byte[] is still 2 bytes so i have 1 byte missing because now i need to insert 3 values (12, 34, 5) – user3637066 Jun 08 '14 at 18:31
  • A byte in a sequence of hex-notation characters has always two digits. If you get an input with an odd number of digits, cut of the last digit from the string, convert the string as usual and convert the last digit separately... –  Jun 08 '14 at 18:38
  • No problem. On a side note: Inputting "12345" should perhaps rather be treated as "012345", and thus result in bytes 01,23,45. A byte sequence of 12,34,5 would normally correspond to the hex string "123405". –  Jun 08 '14 at 20:04

0 Answers0