原文链接: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 的相互转化的更多相关文章

  1. byte数组和int之间相互转化的方法

    Java中byte数组和int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送者接收的数据都是byte数组,但是int类型是4个byte组成的,如何把一个整形in ...

  2. [java]byte和byte[]与int之间的转换

    1.byte与int转换 public static byte intToByte(int x) {   return (byte) x;   }   public static int byteTo ...

  3. java中byte转换int时为何与0xff进行与运算

    在剖析该问题前请看如下代码 public static String bytes2HexString(byte[] b) {  String ret = "";  for (int ...

  4. Java 字节数组类型(byte[])与int类型互转

    代码如下: public class CommonUtils { //高位在前,低位在后 public static byte[] int2bytes(int num){ byte[] result ...

  5. 基于java的InputStream.read(byte[] b,int off,int len)算法学习

    public int read(byte[] b, int off, int len) throws IOException 将输入流中最多 len 个数据字节读入字节数组.尝试读取多达 len 字节 ...

  6. Java将byte[]和int的互相转换

    /** * 将整数转换为byte数组并指定长度 * @param a 整数 * @param length 指定长度 * @return */ public static byte[] intToBy ...

  7. java中write(byte[] b)与write(byte[] b,int off,int len)区别

    public static void copyInputStreamT0OutputStream(InputStream in, OutputStream out) { byte[] buffer = ...

  8. JAVA实体类不要使用基本类型,基本类型包含byte、int、short、long、float、double、char、boolean

    由于JAVA的基本类型会有默认值,例如当某个类中存在private  int age;字段时,创建这个类时,age会有默认值0.当使用age属性时,它总会有值.因此在某些情况下,便无法实现age为nu ...

  9. Java中byte转换int时与0xff进行与运算的原因

    http://w.baike.com/LGAdcWgJBBQxRAHUf.html 转帖 java中byte转换int时为何与0xff进行与运算 在剖析该问题前请看如下代码 public static ...

随机推荐

  1. qq邮箱发送

    454 Authentication failed, please open smtp flag first!用QQ邮箱测试报错 我用QQ邮箱测试javamail发送邮件的功能,用户名密码设置正确,却 ...

  2. UVa11235 FrequentValues(RMQ)

    Problem F: Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasi ...

  3. How Tomcat Works(十四)

    我们已经知道,在tomcat中有四种类型的servlet容器,分别为Engine.Host.Context 和Wrapper,本文接下来对tomcat中Wrapper接口的标准实现进行说明. 对于每个 ...

  4. sqlserver 自增字段修改为普通主键字段

    --增加备份字段alter Table tableName add columnNameBak bigint ; --将主键自增字段 赋值到备份字段update tableNameset column ...

  5. Minus-C 一个最小化的C语言规范

    资深C++程序员都不会对C++编程规范太陌生,C++实在太复杂,以至于所有项目都需要裁剪一个子集共项目组内使用.经过在家休息这一小段时间,我发现其实C语言更需要一个相同的规范,这就是本文的目标,最大可 ...

  6. DeleteDC() 与 ReleaseDC() 的区别 [转]

    DeleteDC 该函数删除指定的设备上下文环境(DC). 原型: BOOL DeleteDC(HDC hdc): 参数: hdc:设备上下文环境的句柄. 返回值: 成功,返回非零值:失败,返回零.调 ...

  7. c++训练营--重载

    // demo1.cpp : 定义控制台应用程序的入口点. //通过此例程了解重载 #include "stdafx.h" #include <iostream> us ...

  8. Codeforces Round #335 (Div. 2) D. Lazy Student 构造

    D. Lazy Student Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/606/probl ...

  9. Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题

    A. Ilya and Diplomas Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/ ...

  10. Approaching the Fun Factor in Game Design

    I recently did some research on this and talked to Dr. Clayton Lewis (computer Scientist in Residenc ...