C++: byte 和 int 的相互转化
原文链接:http://blog.csdn.net/puttytree/article/details/7825709
NumberUtil.h
//
// NumberUtil.h
// MinaCppClient
//
// Created by yang3wei on 7/22/13.
// Copyright (c) 2013 yang3wei. All rights reserved.
// #ifndef __MinaCppClient__NumberUtil__
#define __MinaCppClient__NumberUtil__ #include <string> /**
* htonl 表示 host to network long ,用于将主机 unsigned int 型数据转换成网络字节顺序;
* htons 表示 host to network short ,用于将主机 unsigned short 型数据转换成网络字节顺序;
* ntohl、ntohs 的功能分别与 htonl、htons 相反。
*/ /**
* byte 不是一种新类型,在 C++ 中 byte 被定义的是 unsigned char 类型;
* 但在 C# 里面 byte 被定义的是 unsigned int 类型
*/
typedef unsigned char byte; /**
* int 转 byte
* 方法无返回的优点:做内存管理清爽整洁
* 如果返回值为 int,float,long,double 等简单类型的话,直接返回即可
* 总的来说,这真心是一种很优秀的方法设计模式
*/
void int2bytes(int i, byte* bytes, int size = 4); // byte 转 int
int bytes2int(byte* bytes, int size = 4); #endif /* defined(__MinaCppClient__NumberUtil__) */
NumberUtil.cpp
//
// NumberUtil.cpp
// MinaCppClient
//
// Created by yang3wei on 7/22/13.
// Copyright (c) 2013 yang3wei. All rights reserved.
// #include "NumberUtil.h" void int2bytes(int i, byte* bytes, int size) {
// byte[] bytes = new byte[4];
memset(bytes,0,sizeof(byte) * size);
bytes[0] = (byte) (0xff & i);
bytes[1] = (byte) ((0xff00 & i) >> 8);
bytes[2] = (byte) ((0xff0000 & i) >> 16);
bytes[3] = (byte) ((0xff000000 & i) >> 24);
} int bytes2int(byte* bytes, int size) {
int iRetVal = bytes[0] & 0xFF;
iRetVal |= ((bytes[1] << 8) & 0xFF00);
iRetVal |= ((bytes[2] << 16) & 0xFF0000);
iRetVal |= ((bytes[3] << 24) & 0xFF000000);
return iRetVal;
}
C++: byte 和 int 的相互转化的更多相关文章
- byte数组和int之间相互转化的方法
Java中byte数组和int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送者接收的数据都是byte数组,但是int类型是4个byte组成的,如何把一个整形in ...
- [java]byte和byte[]与int之间的转换
1.byte与int转换 public static byte intToByte(int x) { return (byte) x; } public static int byteTo ...
- java中byte转换int时为何与0xff进行与运算
在剖析该问题前请看如下代码 public static String bytes2HexString(byte[] b) { String ret = ""; for (int ...
- Java 字节数组类型(byte[])与int类型互转
代码如下: public class CommonUtils { //高位在前,低位在后 public static byte[] int2bytes(int num){ byte[] result ...
- 基于java的InputStream.read(byte[] b,int off,int len)算法学习
public int read(byte[] b, int off, int len) throws IOException 将输入流中最多 len 个数据字节读入字节数组.尝试读取多达 len 字节 ...
- Java将byte[]和int的互相转换
/** * 将整数转换为byte数组并指定长度 * @param a 整数 * @param length 指定长度 * @return */ public static byte[] intToBy ...
- java中write(byte[] b)与write(byte[] b,int off,int len)区别
public static void copyInputStreamT0OutputStream(InputStream in, OutputStream out) { byte[] buffer = ...
- JAVA实体类不要使用基本类型,基本类型包含byte、int、short、long、float、double、char、boolean
由于JAVA的基本类型会有默认值,例如当某个类中存在private int age;字段时,创建这个类时,age会有默认值0.当使用age属性时,它总会有值.因此在某些情况下,便无法实现age为nu ...
- Java中byte转换int时与0xff进行与运算的原因
http://w.baike.com/LGAdcWgJBBQxRAHUf.html 转帖 java中byte转换int时为何与0xff进行与运算 在剖析该问题前请看如下代码 public static ...
随机推荐
- iOS 8自动调整UITableView和UICollectionView布局
本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...
- c# 解决IIS写Excel的权限问题
c# 解决IIS写Excel的权限问题 from: http://www.jb51.net/article/31473.htm 发布:mdxy-dxy 字体:[增加 减小] 类型:转载 使用以上方法必 ...
- javaScript 获取主机地址,项目名等
//获取当前网址 var curWwwPath=window.document.location.href; alert(curWwwPath); //http://localhost:8080/ ...
- 编译cscope-15.8a遇到的问题与解决方案
1)环境 主机:Linux ubuntu 3.2.0-32-generic-pae #51-Ubuntu SMP Wed Sep 26 21:54:23 UTC 2012 i686 i686 i386 ...
- cocos2dx json数据解析
转自:http://blog.csdn.net/wangbin_jxust/article/details/9707873 cocos2dx本身没有json解析类库,我们这里引入libjson进行解析 ...
- Response.Redirect 打开新窗体的两种方法
普通情况下,Response.Redirect 方法是在server端进行转向,因此,除非使用 Response.Write("<script>window.location=' ...
- ReactNative学习实践--动画初探之加载动画
学习和实践react已经有一段时间了,在经历了从最初的彷徨到解决痛点时的兴奋,再到不断实践后遭遇问题时的苦闷,确实被这一种新的思维方式和开发模式所折服,react不是万能的,在很多场景下滥用反而会适得 ...
- codeforces Gym 100187F F - Doomsday 区间覆盖贪心
F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...
- rqnoj-217-拦截导弹-最长不上升子序列以及不上升子序列的个数
最长上升子序列的O(n*log(n))算法. 不上升子序列的个数等于最长上升子序列的长度. #include<string.h> #include<stdio.h> #incl ...
- MySQL数据库最大连接数
MYSQL数据库安装完毕后,默认最大连接数是100. 命令: show processlist; 假设是root帐号,你能看到全部用户的当前连接.假设是其他普通帐号,仅仅能看到自己占用的连接. sho ...