常用数据格式转换函数

来源:互联网 发布:家装网络布线图 编辑:程序博客网 时间:2024/06/04 19:13

字符串数字16进制表达

DSP_2_HEX(UCHAR * dsp,UCHAR * hex,long count)

{
int i;
int sTmpVal;
UCHAR buf[3];
for (i = 0 ; i < count ; i++)
{
memcpy(buf, &dsp[i * 2], 2);//memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。
buf[2] = 0;
sscanf((char *)buf, "%02X", &sTmpVal);sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。
hex[i] = (UCHAR) sTmpVal;
}

}

16进制字符串表达

/*
Function: HEX_2_DSP
hex(binary) data to dsp data
for example: hex={0x1A,0x2B,0x33,0x44},count=4,dsp="1A2B3344";
In Parameter:   hex   UCHAR[count]hex data
count long  
    Out Parameter:  dsp  UCHAR[2*count+1] dsp data  以null字符结束
Return: No return
*/
void CGwixfsToolView::HEX_2_DSP(UCHAR * hex,UCHAR * dsp,long count)
{
char buf[3];
int i;
for(i=0;i<count;i++){
sprintf(buf,"%02X",hex[i]);
memcpy(&dsp[i*2],buf,2);
}
dsp[2*count]=0x0;
}


字符数组转换为字符串 

  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. int main(){  
  6.     char a[10]="aaaabbbba";  
  7.     string s(&a[0],&a[strlen(a)]);  
  8.     cout<<s<<endl;  
  9.     system("pause");  
  10. }