bsd socket 网络通讯必备工具类
传输数据的时候都要带上包头,包头有简单的又复杂的,简单的只要能指明数据的长度就够了。
这里我写了一个工具类,可以方便地将整型的数据长度转换为长度为 4 的字节数组。
另一方面,可以方便的将长度为 4 的字节数组转换为整型的数据长度。
还提供了2进制数据和16进制字符串相互转换的两个方法。
ConvertUtil.h
//
// ConvertUtil.h
// MinaCppClient
//
// Created by yang3wei on 7/22/13.
// Copyright (c) 2013 yang3wei. All rights reserved.
//
#ifndef __MinaCppClient__ConvertUtil__
#define __MinaCppClient__ConvertUtil__
#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;
#pragma mark int2bytes() & bytes2int()
/**
* int 转 byte
* 方法无返回的优点:做内存管理清爽整洁
* 如果返回值为 int,float,long,double 等简单类型的话,直接返回即可
* 总的来说,这真心是一种很优秀的方法设计模式
*/
void int2bytes(int in_iValue, byte* out_pArrBytes, int in_iSize = 4);
// byte 转 int
int bytes2int(byte* in_pArrBytes, int in_iSize = 4);
#pragma mark hexStr2bytes() & bytes2hexStr()
// char 转 int
int hexChar2int(char c);
// 十六进制字符串转字节数组
void hexStr2bytes(std::string in_oStrHex, byte* out_pArrBytes, int& out_iSize);
/**
* 字节数组转十六进制字符串
* string str;
* bytes2hexStr(t_oArrCharBuf, tmp_iRecvLen, str);
* printf("%s\n", str.c_str());
*/
void bytes2hexStr(byte* in_pArrBytes, int in_iSize, std::string& out_oStrHex);
#endif /* defined(__MinaCppClient__ConvertUtil__) */ConvertUtil.cpp
//
// ConvertUtil.cpp
// MinaCppClient
//
// Created by yang3wei on 7/22/13.
// Copyright (c) 2013 yang3wei. All rights reserved.
//
#include "ConvertUtil.h"
#define HEX_ELEMENTS "0123456789ABCDEF"
// 已测,稳定~
void int2bytes(int in_iValue, byte* out_pArrBytes, int in_iSize) {
memset(out_pArrBytes, 0, sizeof(byte) * in_iSize);
out_pArrBytes[0] = (byte) (0xff & in_iValue);
out_pArrBytes[1] = (byte) ((0xff00 & in_iValue) >> 8);
out_pArrBytes[2] = (byte) ((0xff0000 & in_iValue) >> 16);
out_pArrBytes[3] = (byte) ((0xff000000 & in_iValue) >> 24);
}
// 已测,稳定~
int bytes2int(byte* in_pArrBytes, int in_iSize) {
int t_iRetVal = in_pArrBytes[0] & 0xFF;
t_iRetVal |= ((in_pArrBytes[1] << 8) & 0xFF00);
t_iRetVal |= ((in_pArrBytes[2] << 16) & 0xFF0000);
t_iRetVal |= ((in_pArrBytes[3] << 24) & 0xFF000000);
return t_iRetVal;
}
// 已测,稳定~
int hexChar2int(char in_oChar) {
if (in_oChar >= '0' && in_oChar <= '9') {
return (in_oChar - '0');
}
if (in_oChar >= 'A' && in_oChar <= 'F') {
return (in_oChar - 'A' + 10);
}
if (in_oChar >= 'a' && in_oChar <= 'f') {
return (in_oChar - 'a' + 10);
}
return 0;
}
void hexStr2bytes(std::string in_oStrHex, byte* out_pArrBytes, int& out_iSize) {
out_iSize = (int)in_oStrHex.length();
out_pArrBytes = new byte[out_iSize];
for (int i = 0; i < out_iSize; i += 2) {
out_pArrBytes[i/2] = (char)((hexChar2int(in_oStrHex.at(i)) << 4) | hexChar2int(in_oStrHex.at(i + 1)));
}
}
// 已测,稳定~
void bytes2hexStr(byte* in_pArrBytes, int in_iSize, std::string& out_oStrHex) {
std::string strHexElements(HEX_ELEMENTS);
for (int i = 0; i < in_iSize; i ++) {
out_oStrHex.append(1, strHexElements.at(0x0f & (in_pArrBytes[i] >> 4)));
out_oStrHex.append(1, strHexElements.at(0x0f & in_pArrBytes[i]));
}
}
main.cpp
//
// main.cpp
// MinaCppClient
//
// Created by yang3wei on 7/22/13.
// Copyright (c) 2013 yang3wei. All rights reserved.
//
#include "TestConvertUtil.h"
#include "ConvertUtil.h"
using namespace std;
int main(int argc, const char * argv[]) {
int iValue = 19880607;
unsigned char bytes[4];
int2bytes(iValue, bytes);
string str;
bytes2hexStr(bytes, 4, str);
printf("%d 的十六进制字符串表示为:%s\n", iValue, str.c_str());
return 0;
}

浙公网安备 33010602011771号