C#数据类型所占用的字节数以及位操作类 BitConverter

82 篇文章 3 订阅

在计算机的内存中,所有数据都是使用字节(byte)来存储的。不同的CPU对于字节的存储顺序可能是不一致的。

比如1000在某些CPU上存储顺序是0x03,0xE8。而有些CPU存储顺序是0xE8,0x03。

下面演示C#基础数据类型所占用的字节数,以及转化为相应的字节数组。

一、测试源程序如下:

using System;
using System.Collections.Generic;
using System.Linq;

namespace BasicDataTypeDemp
{
    class Program
    {
        static void Main(string[] args)
        {            
            Console.WriteLine($"【bool】所占用的字节数【{sizeof(bool)}】");
            Console.WriteLine($"【sbyte】所占用的字节数【{sizeof(sbyte)}】");
            Console.WriteLine($"【byte】所占用的字节数【{sizeof(byte)}】");
            Console.WriteLine($"【short】所占用的字节数【{sizeof(short)}】");
            Console.WriteLine($"【ushort】所占用的字节数【{sizeof(ushort)}】");
            Console.WriteLine($"【char】所占用的字节数【{sizeof(char)}】");
            Console.WriteLine($"【int】所占用的字节数【{sizeof(uint)}】");
            Console.WriteLine($"【uint】所占用的字节数【{sizeof(uint)}】");
            Console.WriteLine($"【float】所占用的字节数【{sizeof(float)}】");
            Console.WriteLine($"【long】所占用的字节数【{sizeof(long)}】");
            Console.WriteLine($"【ulong】所占用的字节数【{sizeof(ulong)}】");
            Console.WriteLine($"【double】所占用的字节数【{sizeof(double)}】");
            Console.WriteLine($"【decimal】所占用的字节数【{sizeof(decimal)}】");
            byte[] bufferBool = BitConverter.GetBytes(true);
            Console.WriteLine($"【bool】类型转化为【1】个字节:{string.Join(",", bufferBool)}");
            byte[] bufferShort = BitConverter.GetBytes((short)-20000);
            Console.WriteLine($"【short】类型转化为【2】个字节:{string.Join(",", bufferShort)}");
            byte[] bufferUshort = BitConverter.GetBytes((ushort)45536);
            Console.WriteLine($"【ushort】类型转化为【2】个字节:{string.Join(",", bufferUshort)}");
            byte[] bufferChar = BitConverter.GetBytes('中');
            Console.WriteLine($"【char】类型转化为【2】个字节:{string.Join(",", bufferChar)}");

            byte[] bufferInt = BitConverter.GetBytes(-1234567);
            Console.WriteLine($"【int】类型转化为【4】个字节:{string.Join(",", bufferInt)}");
            byte[] bufferUint = BitConverter.GetBytes((uint)1234567);
            Console.WriteLine($"【uint】类型转化为【4】个字节:{string.Join(",", bufferUint)}");
            byte[] bufferFloat = BitConverter.GetBytes(12345.3456F);
            Console.WriteLine($"【float】类型转化为【4】个字节:{string.Join(",", bufferFloat)}");

            byte[] bufferLong = BitConverter.GetBytes(-123456789876L);
            Console.WriteLine($"【long】类型转化为【8】个字节:{string.Join(",", bufferLong)}");
            byte[] bufferUlong = BitConverter.GetBytes((ulong)1234567898764321L);
            Console.WriteLine($"【ulong】类型转化为【8】个字节:{string.Join(",", bufferUlong)}");
            byte[] bufferDouble = BitConverter.GetBytes(12345678.40625);
            Console.WriteLine($"【double】类型转化为【8】个字节:{string.Join(",", bufferDouble)}");
            decimal dec = 123456789.123456789M;
            int[] arrayFourInt = decimal.GetBits(dec);
            Console.WriteLine($"【decimal】类型转化为【4】个整数【int】:{string.Join(",", arrayFourInt)}");
            IEnumerable<byte> bufferDecimal = arrayFourInt.SelectMany(element => BitConverter.GetBytes(element));
            Console.WriteLine($"【decimal】类型转化为【16】个字节:{string.Join(",", bufferDecimal)}");

            Console.WriteLine($"本计算机是否是低字节在前:【{BitConverter.IsLittleEndian}】");
            short test = 1000;
            Console.WriteLine($"【{test}】使用格式化字符串是:{test.ToString("X4")}");
            List<string> list = new List<string>();
            BitConverter.GetBytes(test).ToList().ForEach(num => list.Add(num.ToString("X2")));
            Console.WriteLine($"【{test}】使用字节转化类是:{string.Join("", list)}");
            Console.WriteLine($"可以看出【普通格式化是高字节在前】,使用【BitConverter是低字节在前的】,字节顺序恰好相反");
            Console.ReadLine();
        }
    }
}
 

二、程序运行如图:

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#,可以使用BitConverter来将十六进制字符串转换为字节数组。首先,我们需要将十六进制字符串转换为字节数组,然后再将字节数组转换为十六进制字符串表示。下面是一个示例代码: ```csharp string hexString = "3132333435363738393a3b3c3d3e3f40"; byte[] byteArray = new byte = Convert.ToByte(hexString.Substring(i, 2), 16); } string result = BitConverter.ToString(byteArray).Replace("-", ""); Console.WriteLine(result); ``` 在上面的代码,我们首先定义了一个十六进制字符串hexString。然后,我们创建了一个与十六进制字符串长度一半大小的字节数组byteArray。接下来,使用一个循环将每两个字符转换为一个字节,并将其存储到字节数。最后,使用BitConverter.ToString方法将字节数组转换为十六进制字符串表示,并使用Replace方法去除其的分隔符“-”。最终,将转换后的结果打印出来。 注意,上述代码的hexString长度必须为偶数,否则会引发异常。另外,如果需要将转换后的字节数组以其他形式使用,可以根据具体需求进行进一步操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [16进制字符串转字节字节数组](https://blog.csdn.net/weixin_28289089/article/details/117018463)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斯内科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值