Exchange

A tools for programmer ONLY Convert between

  • Binary
  • Decimal
  • Hexadecimal
  • Bytes
  • BitArray

If you need to convert different units frequently, This tool may save your time. DEMO HERE.

Tutorial :

// Exchange between Dec/Hex/Binary. here is the example code.

Exchange.Dec.ToHex(255) == "0xFF";

Exchange.Dec.ToBinary(255) == "11111111";

Exchange.Hex.ToDec("ff") == 255;

Exchange.Hex.ToBinary("ff") == "11111111";

Exchange.Bin.ToDec("11111111") == 255;

Exchange.Bin.ToHex("11111111") == "0xFF";

// Example for the Bytes.

Byte[] _yourBytesArray = Exchange.Hex.ToBytes("1127"); // byte{ 11,27 }
Exchange.Bytes.ToHex(_yourBytesAraay) = "1127";

Exchange.Bytes.ToDex(_yourBytesAraay) = 4391;

Exchange.Bytes.ToBinary(_yourBytesAraay) = "1000100100111";

// Example for the BitArray

BitArray _arr = Exchange.Bin.ToBitArray("1000100100111");

Exchange.BitArrays.ToDec(_arr) == 4391;

Exchange.BitArrays.ToHex(_arr) == "0x1127";

Exchange.BitArrays.ToBinary(_arr) == "1000100100111");

// Enjoy coding.

The sample code used in the Demo  :

using UnityEngine;
using System.Collections;

public class ExchangeDebug : MonoBehaviour
{
	public int mForDecimal = 100;

	private string BytesEncode (byte[] _bytes)
	{
		string _ascii = Exchange.Bytes.ToString (_bytes, Exchange.Bytes.EncodingType.ASCII);
		string _unicode = Exchange.Bytes.ToString (_bytes, Exchange.Bytes.EncodingType.Unicode);
		string _utf32 = Exchange.Bytes.ToString (_bytes, Exchange.Bytes.EncodingType.UTF32);
		string _utf7 = Exchange.Bytes.ToString (_bytes, Exchange.Bytes.EncodingType.UTF7);
		string _utf8 = Exchange.Bytes.ToString (_bytes, Exchange.Bytes.EncodingType.UTF8);
		return "ASCII :\t"+_ascii+"\n"+
			"Unicode :\t"+_unicode+"\n"+
				"UTF32 :\t"+_utf32+"\n"+
				"UTF7 :\t"+_utf7+"\n"+
				"UTF8 :\t"+_utf8+"\n";
	}

	[ContextMenu("Test Dec")]
	public string TestDec()
	{
		string _bin = Exchange.Dec.ToBinary(mForDecimal);
		string _hex = Exchange.Dec.ToHex(mForDecimal);
		byte[] _bytes = Exchange.Dec.ToByte( System.Convert.ToInt64(mForDecimal) );
		string _bytesStr = Exchange.Bytes.ToString(_bytes);
		if( Exchange.Bytes.ToDec(_bytes) != mForDecimal )
			Debug.LogWarning("Bytes to decimal have error.");

		BitArray _ba = Exchange.Dec.ToBitArray( mForDecimal );
		string _baStr = Exchange.BitArrays.ToString( _ba );

		BytesEncode (_bytes);

		string _out = "\\\\\\\\\\\\\\\\\\\\\\\\\n\n" +
			"*Decimal\t\t\t= "+mForDecimal+"\n"+
				"Hexadecimal\t= "+_hex+"\n"+
				"Binary\t\t\t= "+_bin+"\n"+
				"BitArray\t\t\t= "+_baStr+"\n"+
				"Bytes\t\t\t= "+_bytesStr+"\n"+
				BytesEncode(_bytes);
		Debug.Log(_out);
		return _out;
	}

	public string mForBinary = "11110000";
	[ContextMenu("Test Binary")]
	public string TestBinary()
	{
		long _dec = Exchange.Bin.ToDec(mForBinary);
		string _hex = Exchange.Bin.ToHex(mForBinary);
		byte[] _bytes = Exchange.Bin.ToByte(mForBinary);
		string _bytesStr = Exchange.Bin.ToString(_bytes);
		if( Exchange.Bytes.ToBinary(_bytes) != mForBinary )
			Debug.LogWarning("Bytes to binary have error.");

		BitArray _ba = Exchange.Bin.ToBitArray( mForBinary );
		string _baStr = Exchange.BitArrays.ToString( _ba );
		string _out = "\\\\\\\\\\\\\\\\\\\\\\\\\n\n" +
			"Decimal\t\t\t= "+_dec+"\n"+
				"Hexadecimal\t= "+_hex+"\n"+
				"*Binary\t\t\t= "+mForBinary+"\n"+
				"BitArray\t\t\t= "+_baStr+"\n"+
				"Bytes\t\t\t= "+_bytesStr+"\n"+
				BytesEncode(_bytes);
		Debug.Log(_out);
		return _out;
	}

	public string mForHex = "F0F0";
	[ContextMenu("Test Hexadecimal")]
	public string TestHex()
	{
		long _dec = Exchange.Hex.ToDec(mForHex);
		string _bin = Exchange.Hex.ToBinary(mForHex);
		byte[] _bytes = Exchange.Hex.ToByte(mForHex);
		string _bytesStr = Exchange.Bytes.ToString(_bytes);

		mForHex = Exchange.Hex.ToString(mForHex);
		if( Exchange.Bytes.ToHex(_bytes) != mForHex )
			Debug.LogWarning("Bytes to hexadecimal have error.");

		BitArray _ba = Exchange.Hex.ToBitArray( mForHex );
		string _baStr = Exchange.BitArrays.ToString( _ba );
		string _out = "\\\\\\\\\\\\\\\\\\\\\\\\\n\n" +
			"Decimal\t\t\t= "+_dec+"\n"+
				"*Hexadecimal\t= "+mForHex+"\n"+
				"Binary\t\t\t= "+_bin+"\n"+
				"BitArray\t\t\t= "+_baStr+"\n"+
				"Bytes\t\t\t= "+_bytesStr+"\n"+
				BytesEncode(_bytes);
		Debug.Log(_out);
		return _out;
	}

	private string mOutput = string.Empty;

	void OnGUI()
	{
		GUILayout.BeginHorizontal();
		{
			GUILayout.BeginVertical();
			{
				GUILayout.Label("Decimal : ");
				int _num=0;
				if( !int.TryParse( GUILayout.TextField(mForDecimal.ToString()) ,out _num) )
					_num=0;
				mForDecimal = _num;
				if( GUILayout.Button("Test Decimal" ) )
					mOutput += TestDec();

				GUILayout.Label("Hexadecimal : ");
				mForHex = GUILayout.TextField(mForHex);
				if( GUILayout.Button("Test Hexadecimal" ) )
					mOutput += TestHex();

				GUILayout.Label("Binary : ");
				mForBinary = GUILayout.TextField(mForBinary);
				if( GUILayout.Button("Test Binary" ) )
					mOutput += TestBinary();
			}
			GUILayout.EndVertical();

			GUILayout.BeginVertical();
			{
				if( GUILayout.Button("Clear") ) mOutput=string.Empty;
				mOutput = GUILayout.TextArea(mOutput);
			}
			GUILayout.EndVertical();
		}
		GUILayout.EndHorizontal();
	}
}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

*