8 Replies - 2513 Views - Last Post: 10 July 2009 - 10:22 PM Rate Topic: -----

#1 csharpcoder2   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 07-July 09

Converting a Sequence of Numbers into Bytes

Post icon  Posted 07 July 2009 - 03:26 PM

I am trying to send a UDP packet of bytes corresponding to the numbers 1-1000 in sequence.

I've tried the following with no success. Any help would be greatly appreciated!

Thank you.

 List<byte> byteList = new List<byte>();

			for (int i = 1; i <= 1000; i++)
			{
				byte[] nByte = BitConverter.GetBytes(i);
				foreach (byte b in nByte)
				{
					byteList.Add(b);
				}
			}


			byte[] sendMsg = byteList.ToArray();


This post has been edited by csharpcoder2: 08 July 2009 - 11:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Converting a Sequence of Numbers into Bytes

#2 Dantheman   User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Converting a Sequence of Numbers into Bytes

Posted 07 July 2009 - 04:01 PM

A byte can only represent 256 values. Describe your problem in greater detail.
Was This Post Helpful? 0
  • +
  • -

#3 csharpcoder2   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 07-July 09

Re: Converting a Sequence of Numbers into Bytes

Posted 07 July 2009 - 04:27 PM

Each number 1 through 1000 should be represented in the minimum number of bytes required. They should be in sequence. So suppose numbers (1-500) were able to be represented in one byte and numbers 501-1000 require 3 bytes. It would look something like:

(In example, bytes are numbered starting at 1, not 0).
Byte 1: <1>
Byte 2: <2>
....
Byte 500: <500>
Byte 501, 502, 503: <501>
Byte 504, 505, 506: <502>
.... <continue to 1000>

View PostDantheman, on 7 Jul, 2009 - 03:01 PM, said:

A byte can only represent 256 values. Describe your problem in greater detail.

Was This Post Helpful? 0
  • +
  • -

#4 Renagado   User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Converting a Sequence of Numbers into Bytes

Posted 07 July 2009 - 05:00 PM

The method Convert.ToByte() will convert your number to a 8-bit(that's 1 byte number). And what Dantheman means is that 8 bits can only hold 2^8=256 different values.

If you want to represent a binary number you could do that using Convert.ToString(number,2).
Was This Post Helpful? 0
  • +
  • -

#5 csharpcoder2   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 07-July 09

Re: Converting a Sequence of Numbers into Bytes

Posted 07 July 2009 - 06:44 PM

I understand. What I'm having trouble with is generating a single UDP packet (in bytes) for the numbers in order 1-1000. Any ideas?

View PostRenagado, on 7 Jul, 2009 - 04:00 PM, said:

The method Convert.ToByte() will convert your number to a 8-bit(that's 1 byte number). And what Dantheman means is that 8 bits can only hold 2^8=256 different values.

If you want to represent a binary number you could do that using Convert.ToString(number,2).

Was This Post Helpful? 0
  • +
  • -

#6 firexware   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 06-July 09

Re: Converting a Sequence of Numbers into Bytes

Posted 07 July 2009 - 07:43 PM

If your going to use a changing size to hold the value, you will need to have a pattern of bits between the numbers to tell you how many bits make up that number. I would just use 2 bytes, can hold values up to 65535. This way would use a lot less space then having a split pattern.

I'm assuming your numbers arnt just simply 1 to 1000, but you have a list of numbers you want to encode. To do that would look something like this
		byte[] EncodeShortArray(short[] numbers)
		{
			byte[] encoded = new byte[numbers.Length * 2]; // 2 bytes for every number;
			for (int i = 0; i < numbers.Length; i++)
			{
				Buffer.BlockCopy(BitConverter.GetBytes(numbers[i]), 0, encoded, i * 2,2); //copy the byte[] value of the number into the big byte array
			}
			return encoded;
		}

		short[] DecodeShortArray(byte[] encoded) //remember to add error handling because this will most likely fail if encoded.length is not a multiple of 2
		{
			short[] numbers = new short[encoded.Length / 2];
			for (int i = 0; i < encoded.Length; i += 2)
			{
				numbers[i / 2] = BitConverter.ToInt16(encoded, i); //this converts the 2 bytes after index i into a short, giving you your original value
			}
			return numbers;
		}


Was This Post Helpful? 1
  • +
  • -

#7 csharpcoder2   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 07-July 09

Re: Converting a Sequence of Numbers into Bytes

Posted 08 July 2009 - 10:28 AM

Sorry, I'm still not getting it. How do I convert each number (1,2,3,4,...,998,999,1000) into the minimum number of bytes required and put them in a sequence that I can send as a UDP packet?

I appreciate your help!



View Postfirexware, on 7 Jul, 2009 - 06:43 PM, said:

If your going to use a changing size to hold the value, you will need to have a pattern of bits between the numbers to tell you how many bits make up that number. I would just use 2 bytes, can hold values up to 65535. This way would use a lot less space then having a split pattern.

I'm assuming your numbers arnt just simply 1 to 1000, but you have a list of numbers you want to encode. To do that would look something like this
		byte[] EncodeShortArray(short[] numbers)
		{
			byte[] encoded = new byte[numbers.Length * 2]; // 2 bytes for every number;
			for (int i = 0; i < numbers.Length; i++)
			{
				Buffer.BlockCopy(BitConverter.GetBytes(numbers[i]), 0, encoded, i * 2,2); //copy the byte[] value of the number into the big byte array
			}
			return encoded;
		}

		short[] DecodeShortArray(byte[] encoded) //remember to add error handling because this will most likely fail if encoded.length is not a multiple of 2
		{
			short[] numbers = new short[encoded.Length / 2];
			for (int i = 0; i < encoded.Length; i += 2)
			{
				numbers[i / 2] = BitConverter.ToInt16(encoded, i); //this converts the 2 bytes after index i into a short, giving you your original value
			}
			return numbers;
		}


Was This Post Helpful? 0
  • +
  • -

#8 Renagado   User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Converting a Sequence of Numbers into Bytes

Posted 08 July 2009 - 03:17 PM

Basically...for the nr's 0-255 you need one byte. For the rest of your nr's you need 2. So make two loops, the first simply casts an int to byte for the numbers up to 255.Something like:

for (int i = 1; i < 256; i++)
			{
				byteList.Add((byte)i);
			}


Then for the rest, instead of using int i, use Int16, so your code becomes then something like:

for (Int16 i = 256; i <= 1000; i++)
			{
				byte[] nByte = BitConverter.GetBytes(i);
				 //add rest of code



Because you used int(short for Int32) it took 4 bytes for every number. By using Int16 that becomes 2.

So this way you get the least amount of bytes. Although it would make things much easier if you just used 2 bytes for all numbers. (especially when recieving it's much easier to know how many bytes there are for each number, imagine the numbers 1-1000 being put randomly into the bytelist, how are you going to decode that?)
Anyway, good luck with your project :D
Was This Post Helpful? 1
  • +
  • -

#9 firexware   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 06-July 09

Re: Converting a Sequence of Numbers into Bytes

Posted 10 July 2009 - 10:22 PM

if you can sort the list the most space efficient way to do this would be
bytes containing numbers 0-255|separator|2 bytes containing numbers 256-1000

edit: actually now that i think of it just put a number on the front telling it the index that it starts using 2 bytes
so you dont waste time searching for it and less of a chance of the actual numbers being the same as your separator

This post has been edited by firexware: 10 July 2009 - 10:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1