将uint8的数组转换为uint16的数组(ASCII到Unicode)
我有一个大小为n>1的将uint8的数组转换为uint16的数组(ASCII到Unicode),c,unicode,ascii,C,Unicode,Ascii,我有一个大小为n>1的uint8\u t数组,希望将其转换为大小为n>1的uint16\u t数组。实际上,我使用uint8\t数组来表示ASCII字符,现在我想使用UNICODE 你知道如何进行这种转换吗 编辑: 我想在这里使用这个函数,它可以将const char*string用作参数,而不是const uint16\u t*string。所以我需要把它投出去 srv_err_t gui_write_text_16bit(const uint16_t *string, Layout_type
uint8\u t
数组,希望将其转换为大小为n>1的uint16\u t
数组。实际上,我使用uint8\t
数组来表示ASCII字符,现在我想使用UNICODE
你知道如何进行这种转换吗
编辑:
我想在这里使用这个函数,它可以将const char*string用作参数,而不是const uint16\u t*string
。所以我需要把它投出去
srv_err_t gui_write_text_16bit(const uint16_t *string, Layout_type_t layout,
Layout_field_t field, Text_inverted_t inv) {
srv_err_t err;
uint8_t charCount;
uint8_t byteCount;
uint16_t bitmapCol = 0;
uint16_t bitmapRow = 0;
uint8_t textLength = 0;
uint8_t textHeight = GUI_FONT_NAME.FontHeight;
uint16_t offset;
uint8_t mask;
lcd_rectangle_t position;
if (LAYOUT_A == layout) {
if (LAYOUT_FIELD6 == field) {
// Position 6 is not available in Layout A
err.bits.input_parameter = true;
return err;
}
}
GUI_CONST_STORAGE GUI_CHARINFO
*pcharInfo;
GUI_CONST_STORAGE
unsigned char* pchar;
GUI_CONST_STORAGE GUI_FONT_PROP
*pfontProp;
//uint8_t textBitmap [bitmapLength * textHeight];
uint8_t textBitmap[(LCD_COLUMN_NUMBER_DISPLAY / 8) * GUI_FONT_HEIGHT] = { 0 };
/* Calculate needed space in the array */
// for (charCount = 0; charCount < stringLength; charCount++)
for (charCount = 0; string[charCount] != '\0'; charCount++) {
pfontProp = GUI_FONT_NAME.FontProp;
while (0 != pfontProp) {
if (pfontProp->First <= string[charCount]
&& pfontProp->Last >= string[charCount]) {
offset = string[charCount] - pfontProp->First;
pcharInfo = (pfontProp->pCharInfoFirstChar) + offset; // Pointer to the right character
textLength += pcharInfo->XSize; // Text length in Pixels
break; // exit while loop and beginn with next character
}
pfontProp = pfontProp->pNext;
}
}
textLength = (textLength / 8) + 1; // Text length in Bytes
// for(charCount = 0; charCount < stringLength; charCount++)
for (charCount = 0; string[charCount] != '\0'; charCount++) {
pfontProp = GUI_FONT_NAME.FontProp;
while (0 != pfontProp) {
if (pfontProp->First <= string[charCount]
&& pfontProp->Last >= string[charCount]) {
// Character in Range found
offset = string[charCount] - pfontProp->First;
pcharInfo = (pfontProp->pCharInfoFirstChar) + offset; // Pointer to the right character
pchar = pcharInfo->pData;
for (bitmapRow = 0; bitmapRow < textHeight; bitmapRow++) {
uint16_t bitmapByte = 0;
uint16_t charByte = 0;
uint8_t pixelShift;
for (byteCount = 0; byteCount < pcharInfo->BytesPerLine;
byteCount++) {
//bitmapByte = bitmapRow * bitmapLength + (bitmapCol / 8) + byteCount;
bitmapByte = bitmapRow * textLength + (bitmapCol / 8) + byteCount;
charByte = pcharInfo->BytesPerLine * bitmapRow + byteCount;
pixelShift = bitmapCol % 8;
if (byteCount == (pcharInfo->BytesPerLine - 1)) {
// Last Byte in row
switch (pcharInfo->XSize % 8) {
case 1:
mask = 0x80;
break;
case 2:
mask = 0xC0;
break;
case 3:
mask = 0xE0;
break;
case 4:
mask = 0xF0;
break;
case 5:
mask = 0xF8;
break;
case 6:
mask = 0xFC;
break;
case 7:
mask = 0xFE;
break;
case 0:
mask = 0xFF;
break;
default:
break;
}
textBitmap[bitmapByte] |= (pchar[charByte] & mask) >> pixelShift;
textBitmap[bitmapByte + 1] |= (pchar[charByte] & mask)
<< (8 - pixelShift);
//bitmapCol += pcharInfo->XSize % 8;
} else {
/* charByte is not aligned with the bitmapByte. A direct copy is not possible */
textBitmap[bitmapByte] |= pchar[charByte] >> pixelShift;
textBitmap[bitmapByte + 1] |= pchar[charByte]
<< (8 - pixelShift);
}
}
}
bitmapCol += pcharInfo->XSize;
break; // exit while loop and beginn with next character
}
pfontProp = pfontProp->pNext;
}
}
if (layout == LAYOUT_A) {
position = Layout_A_Text_Field[field];
/* place Bitmap on the right display Position */
if (LAYOUT_TITLE == field) {
gui_place_text(&position, textLength, textHeight, ALIGN_CENTER);
} else {
gui_place_text(&position, textLength, textHeight, ALIGN_LEFT);
}
if (LAYOUT_FIELD2 == field) {
lcd_draw_text(position, textBitmap, sizeof(textBitmap),
Layout_A_Field[field], DRAW_INVERSE);
} else {
lcd_draw_text(position, textBitmap, sizeof(textBitmap),
Layout_A_Field[field], DRAW_NORMAL);
}
}
return err;
}
srv\u err\u t gui\u write\u text\u 16位(const uint16\u t*字符串、布局类型\u t布局、,
布局\字段\字段,文本\反向\库存){
srv_err_t err;
uint8_t charCount;
uint8字节数;
uint16_t bitmapCol=0;
uint16\u t位图行=0;
uint8_t textLength=0;
uint8_t textHeight=GUI_FONT_NAME.FontHeight;
uint16_t偏移量;
uint8_t面罩;
液晶显示矩形位置;
if(布局_A==布局){
if(布局_字段6==字段){
//位置6在布局A中不可用
err.bits.input_参数=true;
返回错误;
}
}
GUI\u CONST\u存储GUI\u CHARINFO
*pcharInfo;
GUI_CONST_存储
无符号字符*pchar;
GUI\u CONST\u存储GUI\u字体\u道具
*pfontProp;
//uint8_t textBitmap[BitmaLength*textHeight];
uint8文本位图[(LCD列号显示/8)*GUI字体高度]={0};
/*计算阵列中所需的空间*/
//对于(charCount=0;charCountFirst-Last>=字符串[charCount]){
偏移量=字符串[charCount]-pfontProp->First;
pcharInfo=(pfontProp->pcharInfo firstchar)+offset;//指向右字符的指针
textLength+=pcharInfo->XSize;//以像素为单位的文本长度
break;//在循环时退出,并使用下一个字符开始
}
pfontProp=pfontProp->pNext;
}
}
textLength=(textLength/8)+1;//以字节为单位的文本长度
//对于(charCount=0;charCountFirst-Last>=字符串[charCount]){
//找到范围内的字符
偏移量=字符串[charCount]-pfontProp->First;
pcharInfo=(pfontProp->pcharInfo firstchar)+offset;//指向右字符的指针
pchar=pcharInfo->pData;
对于(bitmapRow=0;bitmapRowBytesPerLine;
字节计数++){
//bitmapByte=bitmapRow*bitmapLength+(bitmapCol/8)+字节数;
bitmapByte=bitmapRow*textLength+(bitmapCol/8)+字节数;
charByte=pcharInfo->BytesPerLine*bitmapRow+byteCount;
pixelShift=位图颜色%8;
if(字节计数==(pcharInfo->BytesPerLine-1)){
//第行的最后一个字节
开关(pcharInfo->XSize%8){
案例1:
掩码=0x80;
打破
案例2:
掩码=0xC0;
打破
案例3:
掩码=0xE0;
打破
案例4:
掩码=0xF0;
打破
案例5:
掩码=0xF8;
打破
案例6:
掩码=0xFC;
打破
案例7:
掩码=0xFE;
打破
案例0:
掩码=0xFF;
打破
违约:
打破
}
textBitmap[bitmapByte]|=(pchar[charByte]&掩码)>>pixelShift;
textBitmap[bitmapByte+1]|=(pchar[charByte]&掩码)
XSize%8;
}否则{
/*charByte未与bitmapByte对齐。无法直接复制*/
textBitmap[bitmapByte]|=pchar[charByte]>>pixelShift;
textBitmap[bitmapByte+1]|=pchar[charByte]
XSize;
break;//在循环时退出,并使用下一个字符开始
}
pfontProp=pfontProp->pNext;
}
}
如果(布局==布局A){
位置=布局\文本\字段[字段];
/*将位图放置在右侧显示位置*/
如果(布局标题==字段){
图形用户界面放置文本(&位置、文本长度、文本高度、对齐中心);
}否则{
图形用户界面放置文本(位置、文本长度、文本高度、左对齐(&P));
}
if(布局_字段2==字段){
lcd_绘图_文本(位置、文本位图、大小(文本位图),
布局\u A\u字段[字段],绘制\u反转);
}否则{
lcd_绘图_文本(位置、文本位图、大小(文本位图),
布局\u A\u字段[字段],绘制\u法线);
}
}
返回错误;
}
强制转换是没有意义的,因为字符数组中的每个元素使用1个单字节,而UNICODE uint16\t数组需要2个单字节。但是,如果初始字符串仅由ASCII或拉丁1字符组成,则可以使用ASCII或拉丁1字符的UNICODE代码点只是字符值这一事实
因此,如果结果数组是在调用者中分配的,您可以使用如下简单代码:
// Convert an ASCII or Latin1 string to a 16bits unicode string:
void tounicode(uint16_t *dest, const char *src, size_t nchars) {
while(nchars-- > 0) { // process nchars characters
*dest++ = (unsigned char) *src++; // one at a time
}
}
强制转换是没有意义的,因为字符数组中的每个元素使用1个单字节,而UNICODE uint16_t数组需要2个单字节。但如果初始字符串仅由ASCII或拉丁字符组成,则可以使用ASCII或拉丁字符的UNICODE代码点仅为字符值这一事实 因此,如果结果数组是在调用者中分配的,您可以使用如下简单代码:
// Convert an ASCII or Latin1 string to a 16bits unicode string:
void tounicode(uint16_t *dest, const char *src, size_t nchars) {
while(nchars-- > 0) { // process nchars characters
*dest++ = (unsigned char) *src++; // one at a time
}
}
如果您使用的是x86硬件,请检查