蒙蒙plus
蒙蒙plus
Published on 2024-02-06 / 82 Visits
0
0

MDK对多种字符编码的支持

MDK对多种字符编码的支持

概要

常规来说我们只使用GB2312编码进行项目开发,但是有时需要控制设备发送中文字符,这时就需要考虑中文编码问题了,常用的编码有GB2312(ANSI),UTF8,UNICODE

演示环境

当前开发软件版本:MDK535
模拟硬件:STM32F103C8
已知编码:

"中文GB2312"
d6 d0 ce c4 47 42 32 33 31 32
"中文Unicode"
\u4e2d\u6587\u0055\u006e\u0069\u0063\u006f\u0064\u0065
"中文UTF8"
e4 b8 ad e6 96 87 55 54 46 38

​MDK中采用大端模式
"中文Unicode"
2d 4e 87 65 55 00 6e 00 69 00 63 00 6f 00 64 00 65 00

演示代码

#include "stm32f10x.h"                  // Device header
#include "RTE_Components.h"             // Component selection
#include "RTE_Device.h"                 // Keil::Device:Startup
#include <wchar.h>
#include <stdio.h>

char str_buff[64],str_GB2312[64]="中文GB2312";//GB2312  ANSI
char str_UTF8_buff[64],str_UTF8[64]="中文UTF8";//UTF8
wchar_t str_16_buff[64], str_Unicode[64]=L"中文Unicode";//Unicode

int main()
{
     //GB2312  ANSI
    sprintf(str_buff,"%d,%s",123,str_GB2312);  
    //UTF8
    sprintf(str_UTF8_buff,"%d,%s",123,str_UTF8);
    //Unicode
    swprintf(str_16_buff,sizeof(str_16_buff)/sizeof(wchar_t),L"%d,%ls",345,str_Unicode);
    while(1)
    {
    }
}

补充内容

/* wchar.h: C99 header */
/* Copyright 2002 ARM Limited. All rights reserved.            */
/*
 * RCS $Revision$
 * Checkin $Date$
 * Revising $Author: agrant $
 */


typedef unsigned short wchar_t; /* see <stddef.h> */

#pragma __printf_args
    int swprintf(wchar_t * __restrict /*s*/, size_t /*n*/,
                 const wchar_t * __restrict /*format*/, ...) __attribute__((__nonnull__(3)));

总结

v5编译器

文本编码是GB2312下的支持中文GB2312和UNICODE
文本编码是UTF-8下的中文的UTF-8和UNICODE

V6编译器下

文本编码是GB2312下的中文字符无法通过编译和UNICODE也只能用英文字符
文本编码是UTF8下可以支持UTF8和UNICODE
不可以同时支持三种中文编码三者同时支持ANSI
不建议在一个项目中同时使用两种文件编码
如何支持UNICODE
1.#inlcude <wchar.h>
2. Options for target->target ->Use MicroLIB不可以选中

注意事项

  1. 头文件中如果有 中文字符串 也等效与在.c文件里的字符串
  2. 如果使用UNICODE则会增加code区大小
  3. 过于旧版本的设备包不支持V6编译器
  4. V6编译器对中文路径支持不好

MDK如何支持软件仿真

官方解释链接
https://developer.arm.com/documentation/ka002225/latest
下面是支持的列表
Peripheral Simulation for STMicroelectronics STM32F1 Series

Dialog DLL

Parameter

Microcontroller Device

DARMSTM.DLL

-pSTM32F101C6

STM32F101C6

DARMSTM.DLL

-pSTM32F101C8

STM32F101C8

DARMSTM.DLL

-pSTM32F101CBT6

STM32F101CBT6

DARMSTM.DLL

-pSTM32F101R6

STM32F101R6

DARMSTM.DLL

-pSTM32F101R8

STM32F101R8

DARMSTM.DLL

-pSTM32F101RB

STM32F101RB

DARMSTM.DLL

-pSTM32F101T6

STM32F101T6

DARMSTM.DLL

-pSTM32F101T8

STM32F101T8

DARMSTM.DLL

-pSTM32F101V8

STM32F101V8

DARMSTM.DLL

-pSTM32F101VB

STM32F101VB

DARMSTM.DLL

-pSTM32F103C6

STM32F103C6

DARMSTM.DLL

-pSTM32F103C8

STM32F103C8

DARMSTM.DLL

-pSTM32F103CB

STM32F103CB

DARMSTM.DLL

-pSTM32F103R6

STM32F103R6

DARMSTM.DLL

-pSTM32F103R8

STM32F103R8

DARMSTM.DLL

-pSTM32F103RB

STM32F103RB

DARMSTM.DLL

-pSTM32F103T6

STM32F103T6

DARMSTM.DLL

-pSTM32F103T8

STM32F103T8

DARMSTM.DLL

-pSTM32F103V8

STM32F103V8

DARMSTM.DLL

-pSTM32F103VB

STM32F103VB

DARMSTM.DLL

-pSTM32F103ZE

STM32F103ZE


Comment