sscanf又一个技巧-抛弃部分

前面的测试代码 又有变化了 如下

抛弃的技巧

#include 
#include 
typedef unsigned char  uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int   uint32_t;


//CASE1
int main(void)
{
	int guid=0;
	char villageID[33]={0}, buildingID[33]={0};
	char data[]={"003B67372A020000 0e989b50a49711e98f8a878ebe027dbb 1f111160a49711e98f8a878ebe027dbb"};
	sscanf( (const char*)data,"%x%s%s",&guid,villageID,buildingID) ; 
	printf("%X-%s-%s",guid,villageID,buildingID);
}
//2A020000-0e989b50a49711e98f8a878ebe027dbb-1f111160a49711e98f8a878ebe027dbb[Finished in 0.6s]



int main(void)
{
	int guid=0;
	char villageID[33]={0}, buildingID[33]={0};
	char data[]={"003B67372A020000 0e989b50a49711e98f8a878ebe027dbb 1f111160a49711e98f8a878ebe027dbb"};
	sscanf( (const char*)data,"%d%s%s",&guid,villageID,buildingID) ; 
	printf("%d-%s-%s",guid,villageID,buildingID);
}
//3-B67372A020000-0e989b50a49711e98f8a878ebe027dbb[Finished in 0.7s]

//CASE3
int main(void)
{
	int guid=0;
	char villageID[33]={0}, buildingID[33]={0};
	char data[]={"003B67372A020000 0e989b50a49711e98f8a878ebe027dbb 1f111160a49711e98f8a878ebe027dbb"};
	data[8]=' ';
	sscanf( (const char*)data,"%x%*s%s%s",&guid,villageID,buildingID) ; 
	printf("%X-%s-%s",guid,villageID,buildingID);
}
//3B6737-0e989b50a49711e98f8a878ebe027dbb-1f111160a49711e98f8a878ebe027dbb[Finished in 0.7s]

我原计划是8个字节表示UID 实际上来料是16那么我暴力丢失后面8个 %*s非常不错

 

sscanf又一个技巧-抛弃部分_第1张图片

 

sscanf又一个技巧-抛弃部分_第2张图片

 

sscanf又一个技巧-抛弃部分_第3张图片

你可能感兴趣的:(C语言基础)