Jump to content

bit representation

- - - - -

  • Please log in to reply
8 replies to this topic

#1
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
hi
I wrote this to represent bit combination of double:

public static void BitDouble(double x)

{

    byte[] b = BitConverter.GetBytes(x);


    for (int i = b.Length - 1; 0 <= i; i--)

    {

        for (int j = 8; 1 <= j; j--)

            Console.Write(((byte)(b[i] << (8 - j))) >> 7);

    }

}


can I convert it to something like template in C++ for other types eg decimal & int.
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
public static void ToBit<T>(T x)

#3
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
but this has an error:

public static void Bits<T>(T x)

{

    byte[] b = BitConverter.GetBytes(x);

    for (int i = b.Length - 1; 0 <= i; i--)

    {

        for (int j = 8; 1 <= j; j--)

            Console.Write(((byte)(b[i] << (8 - j))) >> 7);

    }

}  

I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#4
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
You are correct. BitConverter will not work with Generics, unless you get into Marshalling, etc. I am working on a workaround that will eliminate the need for all of that extra marshalling code.

#5
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
finally I wrote this class

using System;


class B

{

    static void print(byte[] b)

    {

        for (int i = b.Length - 1; 0 <= i; i--)

        {

            for (int j = 8; 1 <= j; j--)

                Console.Write(((byte)(b[i] << (8 - j))) >> 7);

        }

    }

    public static void Bit(float x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(double x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(sbyte x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(short x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(int x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(long x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(byte x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(ushort x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(uint x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(ulong x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(char x)

    {

        print(BitConverter.GetBytes(x));

    }

    public static void Bit(bool x)

    {

        print(BitConverter.GetBytes(x));

    }

}


class Program

{    

    static void Main()

    {        

        double a = 10.2;

        B.Bit(a);

        Console.ReadKey();

    }

}

but I don't know I can add a merhod for decimal!
and I think the class should be more compact!:sleep:
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#6
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Something like this might be cleaner:



using System;


namespace TestGenericBitConversion

{


	public static class Extensions

	{

		public static byte[] GenericGetBytes<T>(this T value) where T : struct

		{

			return GenericBitConverter<T>.GetBytes(value) ;

		}

	}


	public static class GenericBitConverter<T> where T: struct

	{

		internal delegate byte[] GetBytesFunc(T value);


		static internal readonly GetBytesFunc GetBytes = FetchGetBytesFunc();

		

		private static GetBytesFunc FetchGetBytesFunc()

		{

			return (GetBytesFunc)GetBytesFunc.CreateDelegate(typeof(GetBytesFunc), typeof(BitConverter).GetMethod("GetBytes", new Type[] { typeof(T) }));

		}

	}

}


Usage:


using System;

using System.Text;


namespace TestGenericBitConversion

{

	class Program

	{

		public static void Main(string[] args)

		{

			double test = 999.09;

			byte[] bytes = test.GenericGetBytes();

			

			foreach (byte Byte in bytes)

			{

				Console.WriteLine(ToBin(Byte));

			}

			

			Console.Write("Press any key to continue . . . ");

			Console.ReadKey(true);

		}

		

		public static String ToBin(byte b)

		{

			StringBuilder str = new StringBuilder(8);

			int[] bl  = new int[8];


			for (int i = 0; i < bl.Length; i++)

			{

				bl[bl.Length - 1 - i] = ((b & (1 << i)) != 0) ? 1 : 0;

			}


			foreach ( int num in bl) str.Append(num);


			return str.ToString();

		}

	}

}



#7
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
Thanks
using marshal I also wrote another class:
http://forum.codecal...tation-2-a.html
However I think your code is safer.
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#8
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
I tested your code. I think bytes should be written in reversed order:
using System;

using System.Text;


public static class Extensions

{

    public static byte[] GenericGetBytes<T>(this T value) where T : struct

    {

        return GenericBitConverter<T>.GetBytes(value);

    }

}

public static class GenericBitConverter<T> where T : struct

{

    internal delegate byte[] GetBytesFunc(T value);


    static internal readonly GetBytesFunc GetBytes = FetchGetBytesFunc();


    private static GetBytesFunc FetchGetBytesFunc()

    {

        return (GetBytesFunc)GetBytesFunc.CreateDelegate(typeof(GetBytesFunc), typeof(BitConverter).GetMethod("GetBytes", new Type[] { typeof(T) }));

    }

}

class Program

{

    public static String ToBin(byte b)

    {

        StringBuilder str = new StringBuilder(8);

        int[] bl = new int[8];


        for(int i = 0; i < bl.Length; i++)

        {

            bl[bl.Length - 1 - i] = ((b & (1 << i)) != 0) ? 1 : 0;

        }


        foreach(int num in bl) str.Append(num);


        return str.ToString();

    }

    public static void Main(string[] args)

    {

        decimal test = 999;

        byte[] bytes = test.GenericGetBytes();        

        for(int i = bytes.Length - 1; 0 <= i; i--)       

            Console.Write(ToBin(bytes[i]) + " ");           

        Console.ReadKey();

    }   

}
And there's an exception for decimal. I could not solve the problem for decimal. because I'm a beginner in C# and your code is sophisticated.
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#9
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
The problem is that Bitconverter has no implementation for decimal. Check out the info on MSDN: BitConverter Methods (System)

---------- Post added at 09:50 AM ---------- Previous post was at 09:46 AM ----------

from the MSDN site comments:


public static decimal ToDecimal(byte[] bytes)
{
int[] bits = new int[4];
bits[0] = ((bytes[0] | (bytes[1] << 8)) | (bytes[2] << 0x10)) | (bytes[3] << 0x18); //lo
bits[1] = ((bytes[4] | (bytes[5] << 8)) | (bytes[6] << 0x10)) | (bytes[7] << 0x18); //mid
bits[2] = ((bytes[8] | (bytes[9] << 8)) | (bytes[10] << 0x10)) | (bytes[11] << 0x18); //hi
bits[3] = ((bytes[12] | (bytes[13] << 8)) | (bytes[14] << 0x10)) | (bytes[15] << 0x18); //flags

return new decimal(bits);
}

public static byte[] GetBytes(decimal d)
{
byte[] bytes = new byte[16];

int[] bits = decimal.GetBits(d);
int lo = bits[0];
int mid = bits[1];
int hi = bits[2];
int flags = bits[3];

bytes[0] = (byte)lo;
bytes[1] = (byte)(lo >> 8);
bytes[2] = (byte)(lo >> 0x10);
bytes[3] = (byte)(lo >> 0x18);
bytes[4] = (byte)mid;
bytes[5] = (byte)(mid >> 8);
bytes[6] = (byte)(mid >> 0x10);
bytes[7] = (byte)(mid >> 0x18);
bytes[8] = (byte)hi;
bytes[9] = (byte)(hi >> 8);
bytes[10] = (byte)(hi >> 0x10);
bytes[11] = (byte)(hi >> 0x18);
bytes[12] = (byte)flags;
bytes[13] = (byte)(flags >> 8);
bytes[14] = (byte)(flags >> 0x10);
bytes[15] = (byte)(flags >> 0x18);

return bytes;
}




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users