Convert integers into binary values and display them. Convert integers into binary values.

Source: Internet
Author: User

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 ();}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.