Using Bitconverter.getbytes to convert double to byte[], how does the data get understood

Source: Internet
Author: User
I. Access to the key entered by the user

I am here to discuss the use of Platform C #.
My original purpose was to make a simple byte[or encryption of a group of people, and the key was entered from the user. So what kind of controls do you use to get user input? 1. Mode one, Textbox

The first is a string that receives user input directly using a textbox, and then the byte[] key array is obtained using the encoding getbytes[] method. This advantage is very intuitive, the disadvantage is that the user does not know the final key byte[] content is what; 2. Mode two, Numericupanddown

The second way is to let the user enter byte[] directly, then you can use the digital control, and the property hex open, then the user's input will be 16, and then we just have to convert the 16 number to byte[] just fine, The Bitconverter.getbytes function is used. Here's the question: 1. The size of the array of key byte[]

As we all know, the size of the array obtained by bitconverter.getbytes is related to what type of parameter is filled in. If the int GetBytes (), then the array size is 4, because int is 4 bytes; if it's short, it's 2, of course. So here's the problem. How do we know the size of the key array the user wants when the user, for example, enters F?
Workaround Of course, directly determine the extreme value of the data, such as f is obviously a byte can be fixed, then the size of the array is 1,7fff array size is short can be done, then (short st = (short) Numericupanddown.value; byte[] bs = System.BitConverter.GetBytes (ST)) the converted array size is 2. The digression, why I want to give a 7fff example, because for Int16 (short), the maximum number of his positive number is 2 31 times 1, is 7FFF. If a larger number, such as to 8000, and then want to convert the value of this input to short will be an error.
Upgrade Resolution This is a bit of a hassle, and the direct way is to convert it directly to the largest data type double, and then remove the 0 byte of the high position. This way, of course, has to be agreed by the user. The number of byte[that are treated in this manner is not controllable. (The master must have found my thinking has a problem, Mo laughed at, the following details) 2). To convert double to byte[] The problem that you encounter when you

My intention is to convert user input 0xFAADCEAE to byte[] array, then the conversion should logically be
Byte[0] AE
Byte[1] CE
Byte[2] AD
Byte[3] FA
by TE[4] 0
byte[5] 0
byte[6] 0
byte[7] 0
Result I got this thing:
Byte[0] oct:000 hex:0
Byte[1] oct:000 hex:0
Byte[2] oct:192 hex:c0
Byte[3] oct:213 hex:d5
Byte[4] oct:185 hex:b9
Byte[5] oct:085 HEX : The
Byte[6] oct:239 hex:ef
Byte[7] oct:065 hex:41
is seriously inconsistent with my expectations. So I did a simple test, enter 1, and try to get the following value
Byte[0] oct:000 hex:0
Byte[1] oct:000 hex:0
Byte[2] oct:000 hex:0
BYTE[3] oct:000 hex:0
Byte[4] oct:000 hex:0
Byte[5] oct:000 hex:0
Byte[6] oct:240 hex:f0
byte[ 7] oct:063 hex:3f
A 1, how can there be a high so large number. The reason for the
is that I have forgotten what double is.
Double is a double-precision floating-point number is a floating-point number ah, how can it be the general form of expression, obviously have what symbol bit AH, index AH, the mantissa ah. At this point, such a strange byte[] has been found out, in order to live up to the hard work of university teachers, I picked up the textbook, and then recalled the next simple 1 how to become like this:

Refer to the Double data format:

So using double is not correct, should use Int64, can achieve our goal. three. Mode three, custom input

I used the most intuitive way, that is, to use a textbox to let the user enter a 16-form number of bits in the key. Then the user enters a few, then I store a few. The partial reference code is shown below:

        private void Textbox_key_textchanged (object sender, EventArgs e) {lock (this) {
                String value = Textbox_key.text;
                if (value = = "") {textbox_key.text = _lastkeyvaluestr; //Check if the input is legitimate, the legal value before the rule is restored if (!
                Checkhexvalue (value)) {textbox_key.text = _lastkeyvaluestr;
                } _lastkeyvaluestr = Textbox_key.text;
                list<byte> bytelist = new list<byte> ();
                int i = _lastkeyvaluestr.length-1;  A byte is formed per two 16 numbers, so two hexadecimal numbers are combined to parse while (i-1 >= 0) {byte b = byte.
                    Parse (_lastkeyvaluestr[i-1] + "" + _lastkeyvaluestr[i], System.Globalization.NumberStyles.HexNumber);
                    Bytelist.add (b);
                I-= 2; }//If the datais odd, there must be a 16 number (and the 16 number is already the highest bit, that is, when I encounter 0xABC this situation, I will parse into byte[0]:0a BYTE[1]:BC) alone to handle if (i = = 0) {byte b = byte. Parse (_lastkeyvaluestr[i].
                    ToString (), System.Globalization.NumberStyles.HexNumber);
                Bytelist.add (b);
                }//Flip, because I was upside down to add to the list, the data is reversed, need to flip back.
                Bytelist.reverse ();

            _key = Bytelist.toarray (); }
        }

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.