在嵌入式软件开发时,数据的显示必不可少,那么必定会涉及到数据类型转换。将不同类型的数据在编程中进行转换,以便满足不同的需求。
插入一个知识点: 在C语言中,字符串是由字符组成的字符数组,以null终止符('\0')标识字符串的结束。每个字符都是一个字节,按顺序存储在内存中。
例如,字符串 "Hello" 在内存中的存储方式如下:
0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
'H' | 'e' | 'l' | 'l' | 'o' | '\0' |
-
这里的 '\0' 是null终止符,它表示字符串的结束。C语言中的字符串函数会根据这个null终止符来判断字符串的结束位置。
-
要在C语言中声明和操作字符串,可以使用字符数组来存储字符序列,并在数组的末尾添加一个null终止符,从而将其视为一个字符串。
c
/*
******************************************************************************
,* Function Name:
,* Author: By yangbocsu
,* Created: 2023.08.20
,* Description:
,* Parameters:Non
,* Returns: 0.
******************************************************************************
*/
#include <stdio.h>
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;u
int main() {
u8 u8Str[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // 字符数组表示字符串
printf("String: %s\n", str); // 打印字符串
return 0;
}
- 通常情况下,我们使用字符串常量的简写方式来声明字符串:
c
/*
******************************************************************************
,* Function Name:
,* Author: By yangbocsu
,* Created: 2023.08.20
,* Description:
,* Parameters:Non
,* Returns: 0.
******************************************************************************
*/
#include "stdio.h"
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
int main() {
u8 u8Str[] = "Hello"; // 字符串常量会自动添加null终止符
printf("String: %s\n", u8Str);
return 0;
}
一、单个字符 <----> 字符串
1.1 单个字符 转换为 字符串
c
/*
******************************************************************************
,* Function Name:
,* Author: By yangbocsu
,* Created: 2023.08.20
,* Description:
,* Parameters:Non
,* Returns: 0.
******************************************************************************
*/
#include "stdio.h"
#include "string.h"
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
int main() {
// 单个字符转为字符串
u8 u8Ch = 'A';
u8 u8Ch2CharArr[2];
u8Ch2CharArr[0] = u8Ch;
u8Ch2CharArr[1] = '\0'; // 字符串末尾需要添加 null 终止符
printf("Char2String: %s\n", u8Ch2CharArr);
return 0;
}
1.2 字符串 转换为 单个字符
c
/*
******************************************************************************
,* Function Name:
,* Author: By yangbocsu
,* Created: 2023.08.20
,* Description:
,* Parameters:Non
,* Returns: 0.
******************************************************************************
*/
#include "stdio.h"
#include "string.h"
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
int main() {
// 字符串转为单个字符
u8 u8Str[] = "Hello";
u8 u8Ch = u8Str[0];
printf("Char from string: %c\n", u8Ch);
return 0;
}
二、整数 <----> 字符串
2.1 整数 转为 字符串
- 版本一 :sprintf 函数
c
/*
******************************************************************************
,* Function Name:
,* Author: By yangbocsu
,* Created: 2023.08.20
,* Description:
,* Parameters:Non
,* Returns: 0.
******************************************************************************
*/
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
int main() {
// 整数转为字符串
u8 u8Num = 42;
u8 u8StrFromInt[20]; // 适当大小的字符数组
sprintf(u8StrFromInt, "%d", num);
printf("String from int: %s\n", str_from_int);
return 0;
}
- 版本二
c
/*
******************************************************************************
,* Function Name:
,* Author: By yangbocsu
,* Created: 2023.08.20
,* Description:
,* Parameters:Non
,* Returns: 0.
******************************************************************************
*/
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
int main() {
u8 u8Num = 42;
u8 u8StrFromInt[20]; // 适当大小的字符数组
snprintf(u8StrFromInt, sizeof(u8StrFromInt), "%d", u8Num); // 使用sizeof来确保不会溢出
printf("String from int: %s\n", u8StrFromInt);
return 0;
}
用到的函数:snprintf函数和sprintf函数的区别
sprintf函数无法检查目的缓冲区是否溢出,相反,snprintf函数要求第二个参数指定目的缓冲区的大小,因此可以确保该缓冲区不溢出。
2.2 字符串 转为 整数
c
/*
******************************************************************************
,* Function Name:
,* Author: By yangbocsu
,* Created: 2023.08.20
,* Description:
,* Parameters:Non
,* Returns: 0.
******************************************************************************
*/
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
int main() {
// 字符串 转为 整数
u8 u8NumStr[] = "123";
u32 u32IntFromStr = atoi(u8NumStr);
printf("u32IntFromStr = %d\n", u32IntFromStr);
return 0;
}
- atoi函数原型:int atoi(const char *str)
c
#include<iostream>
using namespace std;
int atio1(char *s)
{
int sign=1,num=0;
if(*s=='-')
sign=-1;
s++;
while((*s)!='\0')
{
num=num*10+(*s-'0');
s++;
}
return num*sign;
}
三、浮点数 <----> 字符串
3.1 浮点数 转换为 字符串
c
/*
******************************************************************************
,* Function Name:
,* Author: By yangbocsu
,* Created: 2023.08.20
,* Description:
,* Parameters:Non
,* Returns: 0.
******************************************************************************
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
int main() {
// 浮点数转为字符串
float f = 3.14159;
u8 u8StrFromFloat[20]; // 适当大小的字符数组
snprintf(u8StrFromFloat, sizeof(u8StrFromFloat), "%.2f", f); // 控制小数点位数
printf("u8StrFromFloat = %s\n", u8StrFromFloat);
return 0;
}
3.2 字符串 转换为 浮点数
c
/*
******************************************************************************
,* Function Name:
,* Author: By yangbocsu
,* Created: 2023.08.20
,* Description:
,* Parameters:Non
,* Returns: 0.
******************************************************************************
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
int main() {
// 字符串转为浮点数
u8 u8FloatStr[] = "2.71828";
float FloatFromStr = strtof(u8FloatStr, NULL);
printf("FloatFromStr = %.5f\n", FloatFromStr);
return 0;
}