Created
April 1, 2020 09:28
-
-
Save openroomxyz/e343f98470dcc293c9750683ac0b04e4 to your computer and use it in GitHub Desktop.
Unity Editor : How to convert between Int <-> Hex <-> Bin ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using UnityEditor; | |
| public class IntHexBinConverter : EditorWindow | |
| { | |
| //Converter 1 Variables | |
| int converter_1_input_int = 0; | |
| string converter_1_output_hex = ""; | |
| string converter_1_output_bool = ""; | |
| //Converter 2 Variables | |
| string converter_2_input_hex = "00000000"; | |
| int converter_2_output_int = 0; | |
| string converter_2_output_bool = ""; | |
| //Converter 3 Variables | |
| string converter_3_input_bool = "00000000"; | |
| string converter_3_output_hex = ""; | |
| int converter_3_output_int = 0; | |
| [MenuItem("Tools/Converters/IntHexBinConverter")] | |
| public static void ShowWindow() | |
| { | |
| GetWindow(typeof(IntHexBinConverter)); //GetWindow is a method inherited from the EditorWindow class | |
| } | |
| public static int FromHexS(string value) | |
| { | |
| // strip the leading 0x | |
| if (value.StartsWith("0x", System.StringComparison.OrdinalIgnoreCase)) | |
| { | |
| value = value.Substring(2); | |
| } | |
| return System.Int32.Parse(value, System.Globalization.NumberStyles.HexNumber); | |
| } | |
| static int BoolArrayToInt(bool[] arr) | |
| { | |
| if (arr.Length > 31) | |
| { | |
| throw new System.ApplicationException("too many elements to be converted to a single int"); | |
| } | |
| int val = 0; | |
| for (int i = 0; i < arr.Length; ++i) | |
| { | |
| if (arr[i]) val |= 1 << i; | |
| } | |
| return val; | |
| } | |
| static string FromIntToHex(int value) | |
| { | |
| return value.ToString("X"); | |
| } | |
| public static byte[] StrToByteArray(string str) | |
| { | |
| Dictionary<string, byte> hexindex = new Dictionary<string, byte>(); | |
| for (int i = 0; i <= 255; i++) | |
| hexindex.Add(i.ToString("X2"), (byte)i); | |
| List<byte> hexres = new List<byte>(); | |
| for (int i = 0; i < str.Length; i += 2) | |
| hexres.Add(hexindex[str.Substring(i, 2)]); | |
| return hexres.ToArray(); | |
| } | |
| public static string HexToBin(string hex) | |
| { | |
| Dictionary<string, string> dic = new Dictionary<string, string>(); | |
| dic.Add("0", "0000"); | |
| dic.Add("1", "0001"); | |
| dic.Add("2", "0010"); | |
| dic.Add("3", "0011"); | |
| dic.Add("4", "0100"); | |
| dic.Add("5", "0101"); | |
| dic.Add("6", "0110"); | |
| dic.Add("7", "0111"); | |
| dic.Add("8", "1000"); | |
| dic.Add("9", "1001"); | |
| dic.Add("A", "1010"); | |
| dic.Add("B", "1011"); | |
| dic.Add("C", "1100"); | |
| dic.Add("D", "1101"); | |
| dic.Add("E", "1110"); | |
| dic.Add("F", "1111"); | |
| string result = ""; | |
| for(int i = 0; i<hex.Length; i++) | |
| { | |
| string key = hex[i] + ""; | |
| if(i == (hex.Length - 1)) //last element in array | |
| { | |
| result += dic[key]; | |
| } | |
| else | |
| { | |
| result += dic[key] + "_"; | |
| } | |
| } | |
| return result; | |
| } | |
| public static string remove0x(string input) | |
| { | |
| input = input.ToUpper(); | |
| if(input.Length>2) | |
| { | |
| if ( (input[0] + "")== "0" && (input[1] + "") == "X") | |
| { | |
| return input.Substring(2); | |
| } | |
| } | |
| return input; | |
| } | |
| public static bool isValidHexString(string input) | |
| { | |
| input = input.ToUpper(); | |
| string[] arr1 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; | |
| var h = new HashSet<string>(arr1); | |
| for (int i = 0; i < input.Length; i++) | |
| { | |
| string key = (input[i] + ""); | |
| if(!h.Contains(key)) //string in char is not in hex aphabet so it's not valid! | |
| { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| void C1() | |
| { | |
| //Converter 1 | |
| GUILayout.Label("1) Int32bit Signed -> (Hex, Bool)", EditorStyles.boldLabel); | |
| GUILayout.Label("input", EditorStyles.label); | |
| converter_1_input_int = EditorGUILayout.IntField("Int base10 : ", converter_1_input_int); | |
| GUILayout.Label("output", EditorStyles.label); | |
| converter_1_output_hex = EditorGUILayout.TextField("Hex representation : ", converter_1_output_hex); | |
| converter_1_output_bool = EditorGUILayout.TextField("Bool representation : ", converter_1_output_bool); | |
| converter_1_output_hex = FromIntToHex(converter_1_input_int); //number.ToString("X"); // | |
| converter_1_output_bool = HexToBin(converter_1_output_hex); | |
| GUILayout.Space(10); | |
| } | |
| void C2() | |
| { | |
| //Converter 2 | |
| GUILayout.Label("2) Hex -> (Int32bit Signed, Bool)", EditorStyles.boldLabel); | |
| GUILayout.Label("input", EditorStyles.label); | |
| converter_2_input_hex = EditorGUILayout.TextField("Hex : ", converter_2_input_hex); | |
| GUILayout.Label("output", EditorStyles.label); | |
| if (converter_2_input_hex.Length > 0) | |
| { | |
| converter_2_input_hex = remove0x(converter_2_input_hex); | |
| bool isValid_2_Input = isValidHexString(converter_2_input_hex); | |
| bool number_to_long = false; | |
| if (converter_2_input_hex.Length > 8) | |
| { | |
| number_to_long = true; | |
| } | |
| if (!isValid_2_Input) | |
| { | |
| GUILayout.Label("ERROR not a valid Hex string", EditorStyles.largeLabel); | |
| } | |
| else if (number_to_long) | |
| { | |
| GUILayout.Label("ERROR not number to long to fit into INT", EditorStyles.largeLabel); | |
| } | |
| else | |
| { | |
| converter_2_output_bool = HexToBin(converter_2_input_hex); | |
| converter_2_output_int = FromHexS(converter_2_input_hex); | |
| converter_2_output_int = EditorGUILayout.IntField("Int base10 : ", converter_2_output_int); | |
| converter_2_output_bool = EditorGUILayout.TextField("Bool representation : ", converter_2_output_bool); | |
| } | |
| GUILayout.Space(10); | |
| } | |
| } | |
| string BoolToHex(string input) | |
| { | |
| string result = ""; | |
| input = input.Replace("_", ""); | |
| Dictionary<string, string> dic = new Dictionary<string, string>(); | |
| dic.Add("0000", "0"); | |
| dic.Add("0001", "1"); | |
| dic.Add("0010", "2"); | |
| dic.Add("0011", "3"); | |
| dic.Add("0100", "4"); | |
| dic.Add("0101", "5"); | |
| dic.Add("0110", "6"); | |
| dic.Add("0111", "7"); | |
| dic.Add("1000", "8"); | |
| dic.Add("1001", "9"); | |
| dic.Add("1010", "A"); | |
| dic.Add("1011", "B"); | |
| dic.Add("1100", "C"); | |
| dic.Add("1101", "D"); | |
| dic.Add("1110", "E"); | |
| dic.Add("1111", "F"); | |
| string tmp = ""; | |
| for (int i=0; i < input.Length; i++) | |
| { | |
| tmp += input[i]; | |
| if(tmp.Length == 4) | |
| { | |
| result += dic[tmp]; | |
| tmp = ""; | |
| } | |
| } | |
| return result; | |
| } | |
| bool IsValidBoolString(string input) | |
| { | |
| string[] arr1 = { "0", "1", "_"}; | |
| var h = new HashSet<string>(arr1); | |
| for(int i = 0; i < input.Length; i++) | |
| { | |
| string key = input[i] + ""; | |
| if (!h.Contains(key)) //string in char is not in bool aphabet so it's not valid! | |
| { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| void C3() | |
| { | |
| GUILayout.Label("3) Bool -> (Hex, Int32bit Signed)", EditorStyles.boldLabel); | |
| GUILayout.Label("input", EditorStyles.label); | |
| converter_3_input_bool = EditorGUILayout.TextField("Bool representation : ", converter_3_input_bool); | |
| bool bool_to_big = (converter_3_input_bool.Length > 39); | |
| bool bool_is_valid = IsValidBoolString(converter_3_input_bool); | |
| GUILayout.Label("output", EditorStyles.label); | |
| if (bool_to_big) | |
| { | |
| GUILayout.Label("ERROR Bool is to big to fit into int!", EditorStyles.largeLabel); | |
| } | |
| else if(!bool_is_valid) //not valid | |
| { | |
| GUILayout.Label("ERROR Bool is not valid!", EditorStyles.largeLabel); | |
| } | |
| else | |
| { | |
| converter_3_output_hex = BoolToHex(converter_3_input_bool); | |
| converter_3_output_int = FromHexS(converter_3_output_hex); | |
| converter_3_output_hex = EditorGUILayout.TextField("Hex representation : ", converter_3_output_hex); | |
| converter_3_output_int = EditorGUILayout.IntField("Int base10 : ", converter_3_output_int); | |
| } | |
| } | |
| public void OnGUI() | |
| { | |
| GUILayout.Label("Converters", EditorStyles.boldLabel); | |
| C1(); | |
| C2(); | |
| C3(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment