#define bufferSize 10
int main() {
unsigned char buffer[bufferSize]={1,2,3,4,5,6,7,8,9,10};
char converted[bufferSize*2 + 1];
int i;
for(i=0;i<bufferSize;i++) {
sprintf(&converted[i*2], "%02X", buffer[i]);
/* equivalent using snprintf, notice len field keeps reducing
with each pass, to prevent overruns
snprintf(&converted[i*2], sizeof(converted)-(i*2),"%02X", buffer[i]);
*/
}
printf("%s\n", converted);
return 0;
}
Which outputs:
0102030405060708090A
typedef unsigned char octet_t;
#define MAX_PRINT_STRING_LEN 1024;
char bit_string[MAX_PRINT_STRING_LEN];
octet_t nibble_to_hex_char(octet_t nibble) {
char buf[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
return buf[nibble & 0xF];
}
char *
octet_string_hex_string(const void *s, int length) {
const octet_t *str = s;
int i;
/* double length, since one octet takes two hex characters */
length *= 2;
/* truncate string if it would be too long */
if (length > MAX_PRINT_STRING_LEN)
length = MAX_PRINT_STRING_LEN-1;
for (i=0; i < length; i+=2) {
bit_string[i] = nibble_to_hex_char(*str >> 4);
bit_string[i+1] = nibble_to_hex_char(*str++ & 0xF);
}
bit_string[i] = 0; /* null terminate string */
return bit_string;
}
分享到:
相关推荐
实现将unsigned char数组转成string型,用16进制显示。
本文将讨论如何把 char str 或 unsigned char str 转换成 CString,详细介绍 CString 的构造函数和成员函数 Format 的使用。 一、CString 的构造函数 CString 提供了多种构造函数,可以将 char str 或 unsigned ...
此为QString无损转char*和unsigned char* 。 当QString内容包含汉字时,转换char*等会发生失败。此接口解决了该问题。使用后char*与unsigned char*的qDebug()输出与QString输出结果相同。 注意,该函数返回unsigned ...
自己写的unsigned char二维数组转8位bmp的函数。
4. **`((unsigned char volatile xdata *) 0)`**:这里创建了一个指向外部数据存储空间的`unsigned char`类型的指针,并将其初始化为地址0。通过这种方式,可以利用指针间接访问外部RAM。 #### XBYTE的应用场景 1....
在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别。 首先在内存中,char与unsigned char没有什么不同,都是一个字节,唯一的区别是,char的最高位为符号位,...
1、int ReadCard(int portnumber,int tracknumber,int timeout,unsigned char* track1,int* track1len,unsigned char* track2,int* track2len,unsigned char* track3,int* track3len) 2、int WriteCard(int port...
int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen, const unsigned char *from, int flen); int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen, const unsigned char *from, int ...
unsigned char count=0; unsigned char miao=0; sbit P2_7=P2^7; sbit P1_2=P1^2; sbit P1_3=P1^3; void timer_1() interrupt 3 { TH1 = ( 65536-50000 )/256; TL1 = ( 65536-50000 )%256; count++; if(count=...
BINTOHEX 转换源文件, void (*jmmain)(); unsigned int *p; unsigned int i; unsigned int wdclksel = 0x40000010; unsigned int wdtc = 0x40000004; unsigned int wdmod = 0x40000000; unsigned int wdfeed ...
在C语言中,`char` 和 `unsigned char` 都是用来表示字符数据类型的,但它们之间存在一些重要的差异。理解这些差异对于编写高效且安全的C代码至关重要。 首先,`char` 类型是C语言的基本数据类型之一,它用于存储...
extern const unsigned char gImage_6[261120]; //extern const unsigned char gImage_5[261120]; extern const unsigned char gImage_a[83784]; extern const unsigned char test[]; static unsigned short drawb...
减少库的使用,解决那些需要小代码量,但苦恼于没有简易的字符串处理函数的郁闷 char *itoa_private(int...int gsmBytes2String(const unsigned char* pSrc, char* pDst, int nSrcLength);//字节数据转换为可打印字符串
bool __stdcall stringtohex(unsigned char *inbuf,int inlen,unsigned char * outbuf); bool __stdcall ishex(BYTE *buf,int bl); bool __stdcall isbcd(BYTE *buf,int bl); bool __stdcall encodebcd(BYTE *buf,...
unsigned char DES_Encrypt(unsigned char *plainBuffer, unsigned char *keyBuffer, unsigned char *cipherBuffer);//加密数据 unsigned char DES_Decrypt(unsigned char *cipherBuffer, unsigned char *keyBuffer...
实验6-1数据拷贝程序将一个尺寸为256的unsigned char数组中的数据(其中数据依次为0-255),分别逆序复制到类型为int, double的两个动态缓冲区中,并分别将两个缓冲区的起始地址及其中的数据、缓冲区的最后一个单元...
string char2code(unsigned char letter); // locate the character in the tree with its corresponding binary string and return the string string char2binary(unsigned char letter); // translating the ...
void memsetEX(unsigned char *dst,unsigned char val,unsigned char len); void memcpyEX(unsigned char *dst,unsigned char *src,unsigned char len); unsigned char memcmpEX(unsigned char *dst,unsigned char...