Convert integers into binary values and display them. Convert integers into binary values.
During development, you often encounter multiple logo selections. For example, if you save the listbox selection result to the database, the best way is to use a binary flag for each option and set each flag to 1, you only need to accumulate the results of each selected option to save it to the database.
It can be encapsulated as follows:
Public static int getSum (this ListControl listCtrl) {int li_value = 0; foreach (ListItem item in listCtrl. items) {if (item. selected) {li_value + = item. value. toInt () ;}} return li_value ;}
Binding display:
Public static void bindInt (this ListControl listCtrl, int li_int) {foreach (ListItem item in listCtrl. items) {int li_value = item. value. toInt (); if (li_value & li_int) = li_value) {item. selected = true;} else item. selected = false ;}}
In the simple display table, numbers and letters are used to indicate which digits of the binary number are marked as 1. (More than 10 digits, subsequently indicated by letters ).
Public static string showStr (this int li_int) {StringBuilder ls_ret = new StringBuilder (""); string [] arr = new string [] {"A", "B ", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L ", "M", "N", "O"}; int li_len = Convert. toString (li_int, 2 ). length; // convert to binary get Length for (int I = 0; I <li_len; I ++) {if (li_int & 1) = 1) {if (I <10) {ls_ret.Append (I. toString ();} else ls_ret.Append (arr [(I-10)]);} li_int = li_int> 1;} return ls_ret.ToString ();}