字符串操作类,主要用在808协议数据的处理方面
2013-11-19 11:50
411 查看
808协议数据处理类
直接上代码,头文件如下:/* * File: ByteBuffer.h * Author: shaozg * * Created on 2013年11月19日, 上午10:04 */ #ifndef BYTEBUFFER_H #define BYTEBUFFER_H class ByteBuffer { public: ByteBuffer(); ByteBuffer(const ByteBuffer& orig); virtual ~ByteBuffer(); void appendChar(const char ch); char getChar(const int index); void appendShort(const short sh); short getShort(const int index); void appendInt(const int i); int getInt(const int index); void appendStr(const char * str, const int count); char * subBytes(const int index, const int count); void insertFirstChar(const char ch); void reduce(); void escapse(); private: char *m_data; int m_size; }; #endif /* BYTEBUFFER_H */
源文件内容如下:
/* * File: ByteBuffer.cpp * Author: shaozg * * Created on 2013年11月19日, 上午10:04 */ #include "ByteBuffer.h" #include <string.h> #include <stdio.h> #include <stdlib.h> #define DELETE_BUFFER(poi) if (poi != NULL) { \ delete poi; \ poi = NULL; \ } ByteBuffer::ByteBuffer() { m_data = NULL; m_size = 0; } ByteBuffer::ByteBuffer(const ByteBuffer& orig) { if (orig.m_data != NULL && strlen(orig.m_data) > 0) { const int len = strlen(orig.m_data); m_size = len; m_data = (char *)malloc(len); memset(m_data, 0, len); memcpy(m_data, orig.m_data, len); } } ByteBuffer::~ByteBuffer() { fprintf(stdout, "Destructor"); DELETE_BUFFER(m_data); } void ByteBuffer::appendChar(const char ch) { if (m_data == NULL) { m_data = (char *)malloc(1); *m_data = ch; } else { char *p = (char *)malloc(strlen(m_data) + 1); memcpy(p, m_data, strlen(m_data)); p[strlen(m_data)] = ch; DELETE_BUFFER(m_data); m_data = p; } m_size += 1; } char ByteBuffer::getChar(const int index) { char ch = 0; if (m_data == NULL || m_size < index) { fprintf(stderr, "error index."); ch = 0; } else { ch = m_data[index]; } return ch; } void ByteBuffer::appendShort(const short sh) { if (m_data == NULL) { m_data = (char *)malloc(sizeof(short)); m_data[0] = sh >> 8; m_data[1] = sh & 0xff; } else { const int len = m_size; char *p = (char *)malloc(len + 2); memcpy(p, m_data, len); p[len] = sh >> 8; p[len + 1] = sh & 0xff; DELETE_BUFFER(m_data); m_data = p; } m_size += 2; } short ByteBuffer::getShort(const int index) { short sh = 0; if (m_data == NULL || m_size < index + 1) { fprintf(stderr, "error index."); sh = 0; } else { sh = getChar(index) & 0xff; sh <<= 8; sh |= getChar(index + 1) & 0xff; } return sh; } void ByteBuffer::appendInt(const int i) { if (m_data == NULL) { m_data = (char *)malloc(sizeof(int)); m_data[0] = i >> 24; m_data[1] = (i >> 16) & 0xff; m_data[2] = (i >> 8) & 0xff; m_data[3] = i & 0xff; } else { const int len = m_size; char *p = (char *)malloc(len + 4); memcpy(p, m_data, len); p[len] = i >> 24; p[len + 1] = (i >> 16) & 0xff; p[len + 2] = (i >> 8) & 0xff; p[len + 3] = i & 0xff; DELETE_BUFFER(m_data); m_data = p; } m_size += 4; } int ByteBuffer::getInt(const int index) { int i = 0; if (m_data == NULL || m_size < index + 1) { fprintf(stderr, "error index."); i = 0; } else { i = getChar(index) & 0xff; i <<= 8; i |= getChar(index + 1) & 0xff; i <<= 8; i |= getChar(index + 2) & 0xff; i <<= 8; i |= getChar(index + 3) & 0xff; } return i; } void ByteBuffer::appendStr(const char * str, const int count) { if (str != NULL && count > 0) { char *p = (char *)malloc(m_size + count); memcpy(p, m_data, m_size); memcpy(&p[m_size], str, count); DELETE_BUFFER(m_data); m_data = p; m_size += count; } } char * ByteBuffer::subBytes(const int index, const int count) { char *p = (char *)malloc(count); bcopy(&m_data[index], p, count); return p; } void ByteBuffer::insertFirstChar(const char ch) { if (m_size == 0) { appendChar(ch); } else { char *p = (char *)malloc(m_size + 1); bcopy(p, m_data, m_size); *(p + m_size) = ch; m_size += 1; DELETE_BUFFER(m_data); m_data = p; } } // replace 0x7d 0x01 with 0x7d and 0x0d 0x02 with 0x7e void ByteBuffer::reduce() { if (m_size == 0) { return ; } char *p = (char *)malloc(m_size); int data_len = 0; char *q = p; for (int i = 0; i < m_size - 1; i ++) { if (getChar(i) == 0x7d && getChar(i + 1) == 0x01) { *q ++ = 0x7d; } else if (getChar(i) == 0x7d && getChar(i + 1) == 0x02) { *q ++ = 0x7e; } else { *q ++ = getChar(i); } data_len ++; } DELETE_BUFFER(m_data); m_data = (char *)malloc(data_len); memcpy(m_data, p, data_len); m_size = data_len; DELETE_BUFFER(p); } // loop every character, replace 0x7d with 0x7d 0x01 and 0x7e with 0x7d 0x02 void ByteBuffer::escapse() { if (m_size == 0) { return ; } char * p = (char *)malloc(m_size * 2); int data_len = 0; memset(p, 0, m_size * 2); char * q = p; for (int i = 0; i < m_size; i ++) { if (getChar(i) == 0x7d) { *q ++ = 0x7d; *q ++ = 0x01; data_len += 2; } else if (getChar(i) == 0x7e) { *q ++ = 0x7d; *q ++ = 0x02; data_len += 2; } else { *q ++ = getChar(i); data_len += 1; } } DELETE_BUFFER(m_data); m_data = (char *)malloc(data_len); memcpy(m_data, p, data_len); DELETE_BUFFER(p); m_size = data_len; }
测试函数如下:
/* * File: main.cpp * Author: dev * * Created on 2013年11月19日, 上午10:03 */ #include <cstdlib> #include <stdio.h> #include "ByteBuffer.h" using namespace std; /* * */ int main(int argc, char** argv) { ByteBuffer b; b.appendChar('a'); b.appendShort(12); b.appendInt(456); b.appendStr("hello", 5); int index = 0; char ch = b.getChar(index); index += 1; short sh = b.getShort(index); index += 2; int i = b.getInt(index); index += 4; char * str = b.subBytes(index, 5); printf("ch = %c\nsh = %d\ni = %d\nstr = ", ch, sh, i); for (int i = 0; i < 5; i ++) { printf("%c", str[i]); } printf("\n"); free(str); return 0; }
测试结果如下:
注意:reduce和escape这两个函数没有测试!
谢谢大家!!
相关文章推荐
- spring mvc源码-》MultipartReques类-》主要是对文件上传进行的处理,在上传文件时,编码格式为enctype="multipart/form-data"格式,以二进制形式提交数据,提交方式为post方式。
- 通信(Netty、Mina2)【通信粘包的处理】、【数据协议】、【网络系统的安全性】
- RS485通信字符串数据接收处理的问题
- [Android]JSON数据传输出错——JSONObject将对象数据(Map,List)当成字符串处理
- GPS数据处理——字符串编程练习(C语言)
- sql server 字符串处理和数据类型转换几点体会
- 把输入的整数或者实数数据当成字符串来处理,方便
- c# 关于WCF 接口参数 为 xml 格式的 字符串(string) 的 数据 处理
- Http协议中,主要常见的传送数据到服务器有哪两种方式,这两种方式的特点和区别,以及其在Http协议中的位置
- AJAX提交到Handler.ashx一般处理程序返回json数据 (字符串拼接方式)
- 实例:Netty 处理 TCP协议数据分包问题
- 实例:Netty 处理 TCP协议数据分包问题
- js获取springmvc的controller返回的数据,可以是对象,也可以处理成字符串返回给js
- GoLang基础数据类型--->字符串处理大全
- Objective-C几种字符串处理速度的测试数据
- 字符串处理大数据小结
- Rope --高效字符串处理数据结构
- 目前大数据在金融行业的应用主要体现在哪些方面?
- Data Analysis学习笔记 --- Python数据清洗对字符串处理
- CArchive::WriteString 处理 Unicode 字符串数据