C语言char*字符串数组和unsigned char[]数组的相互转换

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. void convertUnCharToStr(char* str, unsigned char* UnChar, int ucLen)
  5. {
  6. int i = 0;
  7. for(i = 0; i < ucLen; i++)
  8. {
  9. //格式化输str,每unsigned char 转换字符占两位置%x写输%X写输
  10. sprintf(str + i * 2, "%02x", UnChar[i]);
  11. }
  12. }
  13. void convertStrToUnChar(char* str, unsigned char* UnChar)
  14. {
  15. int i = strlen(str), j = 0, counter = 0;
  16. char c[2];
  17. unsigned int bytes[2];
  18. for (j = 0; j < i; j += 2)
  19. {
  20. if(0 == j % 2)
  21. {
  22. c[0] = str[j];
  23. c[1] = str[j + 1];
  24. sscanf(c, "%02x" , &bytes[0]);
  25. UnChar[counter] = bytes[0];
  26. counter++;
  27. }
  28. }
  29. return;
  30. }
  31. int main()
  32. {
  33. unsigned char src[6] = {0x12, 0x32,0x56,0x78,0x90,0xab};
  34. char buffer[20];//维数定义些
  35. convertUnCharToStr(buffer, src, 6);
  36. printf("%s\n", buffer);
  37. unsigned char dst[6];
  38. int len = strlen(buffer);
  39. cout << len << endl;
  40. convertStrToUnChar(buffer, dst);
  41. int i = 0;
  42. for(i = 0; i < 6; i++)
  43. {
  44. printf("%x ", dst[i]);
  45. }
  46. cout << endl;
  47. return 0;
  48. }
时间: 2024-08-07 23:17:05

C语言char*字符串数组和unsigned char[]数组的相互转换的相关文章

【C语言】请编写实现以下功能函数:实现对一个8bit数据(unsigned char)的指定位(例如第8位)的置0或置1操作,并保持其他位不变

/*请编写实现以下功能函数:实现对一个8bit数据(unsigned char)的指定位(例如第8位)的置0或置1操作,并保持其他位不变. 函数原型:void bit_set(unsigned char *p_date,unsigned char position,int flag). 函数参数说明:p_date是指定数据源,position是指定位(1~8),flag是置0或置1. */ #include <stdio.h> void bit_set(unsigned char *p_dat

图像处理中像素点的问题:unsigned char 和 char

以前在做图像处理的时候,一直不太在意这个问题,对图像每个像素点的灰度值,总是认为char也可,unsigned char也可.尽管它们都是8位,但是表示的数的范围却不相同:char: -128~127, unsigned char: 0~255.很明显,unsigned char才是正确的选择. 你可以这样定义: 1 struct { 2     char r; 3     char g; 4     char b; 5 }pixel_t; 6 也可以这样定义: 8 struct { 9    

unsigned char 无符号整形 减法运算

对于一个字节来说: unsigned char :     0  ~  255              0000 0000  ~ 1111 1111 char :-128  ~  127              -128  ~  -1     1000 0000  ~ 1111 1111                     0  ~  127     0000 0000  ~   0111 1111 (-128 的补码是1000 0000,它没有对应的原码.反码,其推导是根据 -128

关于 char 和 unsigned char 的区别

首先卖个关子: 为什么网络编程中的字符定义一般都为无符号的字符? char buf[16] = {0}; unsigned char ubuf[16] = { 0 }; 上面两个定义的区别是: buf 是有符号类型的字符 ubuf 是五符号的字符 示例: int main ( int argc, char *argv[] ) { unsigned char str[] = {0xde, 0xad, 0x2b, 0x6f}; char buf[16] = {0}; unsigned char ub

QString unsigned char* 的转换

QString -> unsigned char* : QString str = "ABCD";  int length = str.length(); unsigned char* sequence = NULL;sequence =(unsigned char*)qstrdup(str.toAscii().constData()); delete[] sequence; - sequence length = 5 --> ['A'] ['B'] ['C'] ['D']

C语言里字符串的解析

原文网摘:http://www.cnblogs.com/yi-meng/p/3620244.html#undefined 根据给定的字符串,按照一定规则解析字符串,卡住好几次,这次做个笔记,以供参考 函数名称:   strtok 函数原型:   char *strtok(char *s1, const char *s2) 函数功能:   分解s1字符串为用特定分隔符分隔的多个字符串(一般用于将英文句分解为单词) 函数返回:   字符串s1中首次出现s2中的字符前的子字符串指针 参数说明:   s

C语言用字符串操作函数

头文件:my_string.h #ifndef __MY_STRING__ #define __MY_STRING__ /*十六进制数值字符串转整数*/ int ch_to_hex(const char* val_in,int size); /*判断给定字符串是否是十六进制数*/ int is_hex(const char* val,int size); /*判断给定字符串是否是数字*/ /*R_V:1(数字) 0(非数字)*/ int is_num(const char* val); /*字符

C语言对字符串的一些操作

1. 字符串中移除一个字符 1 void removeChar(char *str, char c) 2 { 3 char *s = str; 4 int j, k; 5 6 for(j=k=0; s[j]!='\0'; j++) { 7 if(s[j]!=c) 8 s[k++]=s[j]; 9 } 10 11 s[k]= '\0'; 12 } 2. 字符串转为16进制整数 1 unsigned int StrToHex(char *pszSrc, int nLen) 2 { 3 char h1

【c语言】实现对一个8bit数据(unsigned char 类型)的指定位(例如第n位)置0或者置1操作,并保持其他位不变

// 实现对一个8bit数据(unsigned char 类型)的指定位(例如第n位)置0或者置1操作,并保持其他位不变 #include <stdio.h> void bit_set(unsigned char *p_data, unsigned char position, int flag) { unsigned c; unsigned char a = 1; a = a << (position - 1); if (flag == 1) { *p_data = *p_dat