bsd socket 網絡通信必備工具類

傳輸數據的時候都要帶上包頭,包頭有簡單的又複雜的,簡單的只要能指明數據的長度就夠了。設計模式

這裏我寫了一個工具類,能夠方便地將整型的數據長度轉換爲長度爲 4 的字節數組。數組

另外一方面,能夠方便的將長度爲 4 的字節數組轉換爲整型的數據長度。網絡

還提供了2進制數據和16進制字符串相互轉換的兩個方法。app

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.cppspa

//
//  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;
}
相關文章
相關標籤/搜索