4

Possible Duplicates:
Good way to convert between short and bytes?
How can I combine 4 bytes into a 32 bit unsigned integer?

Alright , so I am developing this virtual machine and it has 64 kbs of memory. I am using a byte[] array for the memory and I have one problem. How would I convert 2 bytes to a short or 4 bytes to a Int32?

Community
  • 1
  • 1
Grunt
  • 173
  • 2
  • 2
  • 11

3 Answers3

12

Others suggested BitConverter.
Here is a different solution

Short:

var myShort = (short) (myByteArray[0] << 8 | myByteArray[1]);

Int32

var myint = myByteArray[0] << 24 | myByteArray[1] << 16 | myByteArray[2] << 8 | myByteArray[3];

Mind the endianness though.

Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
  • 1
    This is the solution for big endian numbers. For little Endian you will need to pair up the shifts and indizes differently. – CodesInChaos Jul 24 '11 at 15:04
  • 1
    Why write code that is exactly the same as what the framework already provides for you? – Sven Jul 24 '11 at 15:04
  • 3
    @Sven the framework does not provide you with an architecture independent array to integer conversion. – CodesInChaos Jul 24 '11 at 15:05
  • 1
    @Sven: What CodeInChaos said and also that it is always good to learn how things are done. If you have a method, SolveAllMathProblems(), you will still not know math if you know how to use that method. – Sani Huttunen Jul 24 '11 at 15:13
  • My vote here. Although this is not an elegant way to convert since it's not reliable across different platforms, I tested several way to do it time ago and it's the fastest. On some platforms it far exceed the equivalent C code. – Mario Vernari Jul 24 '11 at 15:39
  • Doing the conversion explicitly is the most reliable way **if** you are receiving the data from an external source, since the endianness has to be explicit. – João Portela Mar 28 '14 at 18:27
  • +1 I do not think BitConverter is available on the .Net MicroFramework – dumbledad Apr 07 '14 at 16:32
8

You can use BitConverter. If it's a virtual machine, you'll want to double check the expected endian-ness (in case it's reversed from your PC's endian-ness.)

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
3
// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
    Array.Reverse(bytes);

int i = BitConverter.ToInt32(bytes, 0);

Where bytes is your array of bytes that is to be converted.

Source: http://msdn.microsoft.com/en-us/library/bb384066.aspx

K Mehta
  • 10,323
  • 4
  • 46
  • 76
  • 1
    reversing the array representing the memory of the VM just to read a value is insane(and makes the function im`Pure`) . If you want to go that way you need to copy out the part of the array containing the number. – CodesInChaos Jul 24 '11 at 15:01
  • If you're doing that for all memory stored as a byte array, then I agree. But I would assume you'd only be doing that for the accessed portion of memory, so it wouldn't be so bad. The reason I included that if statement was to show that BitConverter has an IsLittleEndian property – K Mehta Jul 24 '11 at 15:04
  • @CodesInChaos: I believe `bytes` is supposed to represent the byte array to convert. Not the entire 64kB memory. – Sani Huttunen Jul 24 '11 at 15:07
  • 1
    @Sani made that clarification in the answer as well :) – K Mehta Jul 24 '11 at 15:09