C#数字转字节数组类BitConverter

BitConverter用于基础数据类型与字节数组相互转换
在vs2005中,新建控制台应用程序TestBitConvert,测试静态类BitConverter的使用情况。
★源代码:
using System;
using System.Collections.Generic; using System.Text;
namespace TestBitConvert {
class Program {
static void Main(string[] args) {
Console.WriteLine("boolean占1个字节"); bool[] bl = new bool[] { true, false }; for (int j = 0; j < bl.Length; j++)
{
byte[] bufferC = BitConverter.GetBytes(bl[j]); string s = string.Empty;
for (int i = 0; i < bufferC.Length; i++) {
s += (s.Length == 0 ? "" : ",") + bufferC[i];
}
Console.WriteLine("[{0}]转换为字节数组为:{1}", bl[j], s); }
Console.WriteLine();
Console.WriteLine("字符转换为2个字节.(C#中字符时unicode编码,占2个字节)");
char[] ch = new char[] { 'A', '我' }; for (int j = 0; j < ch.Length; j++) {
byte[] bufferC = BitConverter.GetBytes(ch[j]); string s = string.Empty;
for (int i = 0; i < bufferC.Length; i++)
{
s += (s.Length == 0 ? "" : ",") + bufferC[i];
}
Console.WriteLine("字符[{0}]转换为字节数组为:{1}", ch[j], s); }
Console.WriteLine();
Console.WriteLine("double类型转换为8个字节");

(function() {        var impMonitorUrls = [];        var clickMonitorUrls = [];        function visitUrl(url) {            var img = new Image();            img.src = url;            return img;        }        function visitAllUrls(urls) {            for (var i = 0; i < urls.length; i++) {                visitUrl(urls[i]);            }        }        function addEventListener(node, event, func, useCapture) {            node = node || document;            useCapture = useCapture || false;            if (node.addEventListener) {                node.addEventListener(event, func, useCapture);            } else {                node.attachEvent('on' + event, func);            }        }        function init() {            var imgLink = document.getElementById('img_link');            if (imgLink) {                addEventListener(imgLink, 'click', function() {                            visitAllUrls(clickMonitorUrls);                        }, false);            }        }        function req_imp12() {            visitUrl("https://eduad.baidu.com/impression/wenku_post_json?p=MREB1l12QoZ5h_Qi8UXLT8uEDCmW6jS-tBFInr9YzcjLItSapVr81SJPtD71f13of7vWjaQoqdE2twSVcTanv0JuykqcafB9vVR0mpgOD3xgw6pohyjWt2AQmJ8jhSAMsWz9WCFHAKortQjVuEhXQhsSzBEbfp5W8NP3ygmkstLiZX-anjbDbyj6WhuUJ4GFi5i--pBzXRrNfuC-mQjRv1ZsyhEbykKGoxDp5CH-WF1a1w1py5ET8gluO3SndDlAj-VU_g1OypTF2vHz6Gh_40BBdjwPOwDfcQ_d8SVtV-9ujrNwpo4aoPglk_8sOKNcsVtS1huac2A_1F6SYW9FFUWlMxCHMORWmFAkv0b2JHnNOoNxKQRqA7ziILgx4Wl7uD3-pXWCvONeJBaCLaYBjepziV5dl-T2wO45ONTdUt5LUot4h9wNP3nVaRAdwWQxuEtZvMn44p5I4grhBX_fSxRYayzJFdiWDTaEaimLdAv2x34_C9dpfS3AQdgPM52L-zATp0oKmAdEmqseAP-9W5B5cFHsBsmLX9vQJ4bpVOrgL0gKyDCuKSlVQIWUFUEEU1Wi9yULbuDB--Q7zTyDVmGJwz6HfHNwCVD8zcGI7dTrXi_9AMz6fXVUNeqHsEahnpj7ftp1aAwZi8E83cTZ8bejzAxLSOY9RVym4_HEuML67QeYuub-Zbd9-d98Evexik73miD4myqYJwsxDB_Lug==&price=0");            visitAllUrls(impMonitorUrls);            init();        }        if (window.attachEvent) {            window.attachEvent('onload', req_imp12);        }        else if (window.addEventListener) {           window.addEventListener('load', req_imp12, false);        }    }) ();    .product {        position: relative;    }    .adv-logo {        position: absolute;        bottom: 1px;        z-index: 100;    }    .ad-label {        left: 1px;    }    .dsp-logo {        right: 1px;    }                







        double[] dl = new double[] { 21.3, 12.345, 1.0, 8, 1.59 };             for (int j = 0; j < dl.Length; j++)             { 
            byte[] bufferC = BitConverter.GetBytes(dl[j]);                 string s = string.Empty; 
            for (int i = 0; i < bufferC.Length; i++)                 { 
                s += (s.Length == 0 ? "" : ",") + bufferC[i];                 } 
            Console.WriteLine("[{0}]转换为字节数组为:{1}", dl[j], s);             } 
        Console.WriteLine(); 
        Console.WriteLine("下面测试int与unit的字节数组的问题,测试低位在前和高位在前情况:"); 
        Console.WriteLine("本地计算机的字节顺序是否是低位在前:{0}", BitConverter.IsLittleEndian);//是否低位在前 

        int number = 88888888; 
        byte[] buffer = BitConverter.GetBytes(number);//低位在前:本机的默认设置 
        string s1 = ""; 
        for (int i = 0; i < buffer.Length; i++)             { 
            s1 += buffer[i].ToString("X2");//低位在前             } 
        string s2 = Convert.ToString(number, 16);//高位在前             string s3 = number.ToString("X8");//高位在前 
        Console.WriteLine("s1={0},s2={1},s3={2}", s1, s2, s3);  
        int x = -1234567890; 
        byte[] bytes = BitConverter.GetBytes(x);             Console.Write("整数转化为字节数组为:");             for (int i = 0; i < bytes.Length; i++)             { 
            Console.Write(bytes[i] + ",");             } 
        Console.WriteLine(); 
        uint y = BitConverter.ToUInt32(bytes, 0);             uint z = (uint)x; 
        Console.WriteLine("x={0},y={1},z={2}, y-x={3} y和z相等:{4}", x, y, z, (long)y-x, y==z); 
        Console.ReadLine();         }     } 

(window.cproArray = window.cproArray || []).push({ id: "u3054369" });

C#数字转字节数组类BitConverter_第1张图片
Paste_Image.png

你可能感兴趣的:(C#数字转字节数组类BitConverter)