2

I guess my main question is, will this always work as long as I don't re-initialize (new byte[#]) the array that was passed as the parameter?

static unsafe decimal GetDecimal(byte[] ba)
{
    decimal* decimal_PTR;
    fixed (byte* byte_PTR = &ba[0])
    {
        decimal_PTR = ((decimal*)byte_PTR);
    }
    return *decimal_PTR;
}

I'm not sure how C# handles arrays in memory. I didn't even know they were managed types until about an hour ago. I just want to know if I pass in a decimal as a byte[], will it always return the correct value? Any other information you can provide is appreciated.

5
  • Also, Would it be better to create a decimal at the beginning of the function, then in the fixed block, make it equal the value pointed to by ((decimal*)byte_PTR) then return the decimal that was created? Commented Jun 27, 2012 at 4:58
  • May I know how you obtain the byte[]?. Answer would depend on this information. Commented Jun 27, 2012 at 11:35
  • the byte[] is part of a message sent over a network connection using NetworkStream.Read(byte[] buffer, int offset, int size). The example method above is slightly different then the implementation but the array passed to the method is the same array that was passed to NetworkStream.Read. This is a windows only program so I do believe that I do not need to worry about endianness. Commented Jun 28, 2012 at 9:44
  • I guess my question is will the array of bytes always be stored in order right next to each other in memory or is it possible for the bytes to be stored in separate "chunks" of memory. I want to make sure that part of the array can't be stored somewhere else causing my decimal pointer to return an incorrect value. Commented Jun 28, 2012 at 9:45
  • I guess an example of what I am trying to say is: Will each byte of byte[5] be stored as such. Address 00000000 = byte[0] Address 00000001 = byte[1] Address 00000002 = byte[2] Address 00000003 = byte[3] Address 00000004 = byte[4] Or can it be: Address 00000000 = byte[0] Address 00000001 = byte[1] Address 00000005 = byte[2] Address 00000007 = byte[3] Address 00000008 = byte[4] My main goal is to retrieve a decimal from a byte[], which BitConverter does not contain a method for this. Commented Jun 28, 2012 at 9:54

1 Answer 1

3

After @MJLaukala's clarification through comments:

  1. An Array in .Net is a block of contiguous memory just like any other language. But unlike C/C++ this block not only stores the array's elements but also other 'information' including rank and length of the array. Having said this, &ba[0] will return the pointer to the first element of the array and you can safely increment the pointer to go till the last element. ba.Length will provide the number of elements in the array.

  2. Now if you are sure that the byte array represents the decimal exactly then your code will work fine.

Memory layout of decimal (128 bits):

First 2 bytes are 0

3rd byte contain a value between 0 and 28, indicating the power of 10 to divide the 96-bit integer part by to produce the Decimal value

4th byte : first 7 bits are zero, 8th bit indicates the sign of the decimal (1 meaning negative)

Next 12 bytes: Integer part of the Decimal.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for the clarification! This is info i definitely will not forget.
Why couldn't it just be struct Decimal { long integer; long fractional; } and is represented by integer.fractional? 42.13 would be { integer = 42; fractional = 13 }.
This is a classic 'why is floating-point representation preferred over fixed-point representation' question. Your notation is a fixed point notation with max value of the order of 10^18 (check max value of long). Floating-point representations (such as decimal's) allow greater range. Max value of decimal is of the order of 10^29.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.