Click here to Skip to main content
Click here to Skip to main content

Beware of Decimal Bytes

By , 1 Jun 2012
 

Introduction

This small article will present how surprising can be conversion of decimals to bytes.

Background 

Sometimes we just want the array of bytes instead of some other structure, for example to calculate MD5 hash. If you are not careful, you can get different hashes for the same decimal numbers. Even worse: you can get different hashes for the same numbers calculated in the same way in case if one number calculation is compiled on the Visual Studio 2010 and the other on Visual Studio 2005.

Using the code 

This is a simple example that the bytes representation of decimal can be different for the same number.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
 
namespace DecimalBits
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal one = 1m;
 
            PrintBytes(one);
            PrintBytes(one + 0.0m);
            PrintBytes(1m + 0.0m);
 
            Console.ReadKey();
        }
 
        public static void PrintBytes(decimal d)
        {
            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
            
            binaryWriter.Write(d);
            
            byte[] decimalBytes = memoryStream.ToArray();
 
            Console.WriteLine(BitConverter.ToString(decimalBytes) + " (" + d + ")");
        }
    }
} 

This code will print different binary representation for number one and the result will be different on Visual Studio 2005 and different on Visual Studio 2010. 

Visual Studio 2005:

01-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 (1)
0A-00-00-00-00-00-00-00-00-00-00-00-00-00-01-00 (1,0)
0A-00-00-00-00-00-00-00-00-00-00-00-00-00-01-00 (1,0)

Visual Studio 2010 (no matter which .Net framework):

01-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 (1)
01-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 (1)
0A-00-00-00-00-00-00-00-00-00-00-00-00-00-01-00 (1,0) 

As you can see decimal number one is represented in different way depending on how the calculation was made and depending on which compiler was used. You probably can find more inconsistency. Decimal number can be represented in different way. 1m is not exactly the same as 1.0m, 1.00m. etc.. You can probably find some rules example 1.0m + 1.00m = 2.00m (one zero + two zeros = two zeros). Maybe you can find some normalization of decimals. I found very simple one: d = d / 1.0000000000000000000000000m can you find better? Just leave the comment.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

CoperNick
Software Developer
Poland Poland
Member
I graduated from the Jagiellonian University (and yes — I'm proud of this — and it was sometimes hard for me). After defence of master thesis (link to presentation) I have started professional programming in C and C++ using OpenGL. After that, my Company gave me a chance to switch to .Net framework and C# and I have betrayed my passion for computer graphics. Now I'm pure and happy C#, SQL and sometimes ASP.NET programmer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionreal numbers are not suitable for hash (neither hash-code nor hash-table)memberAndreas Gieriet1 Jun '12 - 9:22 
AnswerRe: real numbers are not suitable for hash (neither hash-code nor hash-table)memberCoperNick3 Jun '12 - 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 1 Jun 2012
Article Copyright 2012 by CoperNick
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid