Conversion between string and byte stream

Before the conversion, we need to make a clear conclusion:
computer storage characters are stored in ASCII code. When converting, we must know how to calculate the corresponding integer value from a single character using the original ASCII value? eg:

ASCII='c'-'0'

How to understand it? The ASCII value is equal to the ASCII value of the currently stored character minus 0 to obtain another ASCII value. This value can be used as the size of the value converted from a string to a byte stream. The ASCII value of '0' is hexadecimal 0x30, if'c' is the character 6, then the example can be equal to:

0x06=0x36-0x30

The ASCII value corresponding to character 6 is 0x36, which can be calculated

Convert a string to a byte stream (convert a decimal string to an integer byte stream)

Atoi realization principle:

isspace(int x)
{
    
    
 if(x==' '||x=='\t'||x=='\n'||x=='\f'||x=='\b'||x=='\r')
  return 1;
 else  
  return 0;
}
isdigit(int x)
{
    
    
 if(x<='9'&&x>='0')         
  return 1; 
 else 
  return 0;
 
}
int atoi(const char *nptr)
{
    
    
        int c;              
        int total;         
        int sign;           
        while ( isspace((int)(unsigned char)*nptr) )
            ++nptr;
        c = (int)(unsigned char)*nptr++;
        sign = c;          
        if (c == '-' || c == '+')
            c = (int)(unsigned char)*nptr++;   
        total = 0;
        while (isdigit(c)) {
    
    
            total = 10 * total + (c - '0');    
            c = (int)(unsigned char)*nptr++;    
        }
        if (sign == '-')
            return -total;
        else
            return total;  
}

There are 128 characters represented by ASCII, so there is no discussion of the highest bit. Regarding how to transform and store the highest bit, the previous blog "Bit Operations in C Language" has an analysis, and each escape character can be Baidu.
In a word : it is the algorithm before the software constructs the character stream to the byte stream

Convert byte stream to string (convert decimal integer byte stream to string)

Source code realization principle:

void atoi(int n,char s[])  
{
    
      
    int i,j,sign;  
  
    if((sign=n)<0)    //记录符号  
        n=-n;         //使n成为正数  
    i=0;  
    do{
    
      
        s[i++]=n%10+'0';    //取下一个数字  
    }while((n/=10)>0);      //循环相除  
  
    if(sign<0)  
        s[i++]='-';  
    s[i]='\0';         
        printf("%s",s);  //生成的数字是逆序的,所以要逆序输出  
}  

Adapt and output to the array in positive order.
Adapt in reverse order:

void _itoa(int b,char f[])//十进制字节流转化为字符串 
{
    
    
	int i=0;
	int j;
	int sign;
	if((sign=b)<0)
	{
    
    
		b=-b;
	}
	for(;;)
	{
    
    
		if(b>0)
		{
    
    
			f[i++]=b%10+'0';
		    b=b/10;
		}
		else
		{
    
    
			break;
		}
	}
	if(sign<0)
	{
    
    
		f[i++]='-';
	}
	f[i]='\0';
	printf("%s",f);//输出被转化为字符串存储的倒序序列
	str_put_upside_down(f);//下面的倒序处理函数
}

Reverse processing function

void str_put_upside_down(char f[]) 
{
    
    
	int i;
	int j;
	while(f[i]!='\0')
	{
    
    
		i++;
	} 
	printf("颠倒的字符串个数为:%d\r\n",i);
		for(j=0;j<i/2;j++)//
		{
    
    
			char a;
			a=f[i-j-1];
			f[i-j-1]=f[j];
			f[j]=a;
		} 
		printf("%s",f);

} 

Output effect:
if the integer value of the incoming value is: -123456 (decimal integer), the
output result is in reverse order: "654321-" (string format), and the
final result: "-123456" (string format)

Convert hexadecimal string to byte stream

#define UNUSED(var) do { (void)var; } while(0)
int8_t HexStrToByte(uint8_t *dest, char *source, int32_t sourceLen)
{
    
    
    int32_t i;
    uint8_t highByte = 0, lowByte = 0;
    int32_t j = 0;

    UNUSED(j);
    UNUSED(highByte);
    UNUSED(lowByte);
    if (source == NULL || dest == NULL)
    {
    
    
        return -1;
    }
    /* 十六进制字符串转换为字节流  要求source字符数是偶数,单数不执行转换, */
    if (sourceLen % 2 != 0)
    {
    
    
       printf("HexStrToByte error,%d\r\n", sourceLen);
    }
    for (i = 0; i < sourceLen; i += 2)
    {
    
    
        highByte = _mkupper(source[i]);
        lowByte = _mkupper(source[i + 1]);
        if (highByte > 0x39)
            highByte -= 0x37;
        else
            highByte -= 0x30;
        if (lowByte > 0x39)
            lowByte -= 0x37;
        else
            lowByte -= 0x30;
        dest[i / 2] = (highByte << 4) | lowByte;
    }
    return 0;
}

eg:

char * b="123456ABDC";//必须为偶数个字符
char c[10];//目标存储空间
HexStrToByte(c,b,strlen(b);
输出结果:
c={
    
    0x120x340x560xAB0xDC}

to sum up

These conversions are all converted to the original stored ASCII, compare with the law of the real data after the conversion, and draw the algorithm between them, according to the gourd.

Guess you like

Origin blog.csdn.net/weixin_42271802/article/details/106886940