int, long和byte[]的互换

本文介绍了一个实用的Java工具类ConvertUtil,该类提供了将基本数据类型如int和long与字节数组进行相互转换的方法。这对于在网络传输中保持数据的一致性和正确性尤为重要。
  1. public class ConvertUtil {
  2.     // bytes[4] to int
  3.     public static int bytesToInt(byte[] intBytes) {
  4.         return (int)( (intBytes[0] & 0xff) << 24) |
  5.                     ( (intBytes[1] & 0xff) << 16) |
  6.                     ( (intBytes[2] & 0xff) << 8 ) |
  7.                     ( (intBytes[3] & 0xff) << 0 ) ;
  8.     }
  9.    
  10.     // int to bytes[4]
  11.     public static byte[] intToBytes(int i) {
  12.         byte[] intBytes = new byte[4];
  13.         intBytes[0] = (byte) (i >> 24);
  14.         intBytes[1] = (byte) (i >> 16);
  15.         intBytes[2] = (byte) (i >> 8);
  16.         intBytes[3] = (byte) (i >> 0);
  17.         return intBytes;
  18.     }
  19.     public static byte[] longToBytes(long num) {
  20.         byte[] b = new byte[8];
  21.         for (int i = 0; i < 8; i++) {
  22.             b[i] = (byte) (num >>> (56 - i * 8));
  23.         }
  24.         return b;
  25.     }
  26.    
  27.     public static long bytesToLong(byte[] b) {
  28.         int mask = 0xff;
  29.         long temp = 0;
  30.         long res = 0;
  31.         for (int i = 0; i < 8; i++) {
  32.             res <<= 8;
  33.             temp = b[i] & mask;
  34.             res |= temp;
  35.         }
  36.         return res;
  37.     }
  38.    
  39.     public static void main(String[] args) {
  40.         // define int i
  41.         int i = 168;
  42.         // int to bytes
  43.         byte[] intBytes = intToBytes(i);
  44.         // bytes to int
  45.         int j = bytesToInt(intBytes);
  46.         // print result
  47.         System.out.println(j);
  48.        
  49.         long l = 24L;
  50.         byte[] lbytes = longToBytes(l);
  51.         long ls = bytesToLong(lbytes);
  52.         System.out.println("long = " + ls);
  53.     }
  54. }
数据驱动的两阶段分布鲁棒(1-范数∞-范数约束)的电热综合能源系统研究(Matlab代码实现)内容概要:本文围绕“数据驱动的两阶段分布鲁棒(1-范数∞-范数约束)的电热综合能源系统研究”展开,提出了一种结合数据驱动与分布鲁棒优化方法的建模框架,用于解决电热综合能源系统在不确定性环境下的优化调度问题。研究采用两阶段优化结构,第一阶段进行预决策,第二阶段根据实际场景进行调整,通过引入1-范数∞-范数约束来构建不确定集,有效刻画风电、负荷等不确定性变量的波动特性,提升模型的鲁棒性实用性。文中提供了完整的Matlab代码实现,便于读者复现验证算法性能,并结合具体案例分析了不同约束条件下系统运行的经济性与可靠性。; 适合人群:具备一定电力系统、优化理论Matlab编程基础的研究生、科研人员及工程技术人员,尤其适合从事综合能源系统、鲁棒优化、不确定性建模等相关领域研究的专业人士。; 使用场景及目标:①掌握数据驱动的分布鲁棒优化方法在综合能源系统中的应用;②理解1-范数∞-范数在构建不确定集中的作用与差异;③学习两阶段鲁棒优化模型的建模思路与Matlab实现技巧,用于科研复现、论文写作或工程项目建模。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现细节,重点关注不确定集构建、两阶段模型结构设计及求解器调用方式,同时可尝试更换数据或调整约束参数以加深对模型鲁棒性的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值