在Linux的終端中顯示BMPString的內容

來源:互聯網
上載者:User

標籤:c語言   linux   

在上一篇博文中,介紹了如何在 Windows 的控制台介面下輸出 BMPString 的內容,但是那裡的方法在 Linux 下不適用。如果將那裡的範例程式碼放到 Linux 下執行,輸出的結果為亂碼。產生亂碼的原因在於 wchar_t 類型的變數在 Windows 下和 Linux 下的位元組長度不同。

以下 C 程式在 Windows 和 Linux 下都能夠執行:

#include <stdio.h>#include <wchar.h>#if defined(_WIN32) || defined(_WIN64)  #include <stdlib.h>#endifint main(void){  printf("wide character (wchar_t type) length is %d bytes.\n", sizeof(wchar_t));#if defined(_WIN32) || defined(_WIN64)  system("pause");#endif  return 0;}

輸出結果是不同的。在 64 位元 Windows 下,用微軟的編譯器將其分別編譯成 32 位和 64 位元可執行程式,輸出結果證明 wchar_t 類型變數長度為 2 位元組。在 64 位元 Linux 下,用 64 位元的 GCC 編譯後執行,輸出結果證明 wchar_t 類型變數長度為 4 位元組。

在 Windows 下處理 BMPString 時,比如對於字元“中”,對應的 UTF-16 編碼是0x4E, 0x2D,我們處理的方法是將其由 big-endian 順序轉換為 little-endian 順序,再用 wprintf() 函數輸出。

在 Linux 下處理 BMPString 時,比如對於字元“中”,對應的 UTF-16 編碼是0x4E, 0x2D,我們處理的方法是將其由 2 位元組長擴充到 4 位元組長,擴充時新添加的兩個位元組的值為 0,即變為 0x0, 0x0, 0x4E, 0x2D,再由 big-endian 順序轉換為 little-endian 順序,即編碼變成 0x2D, 0x4E, 0x0, 0x0,再用 wprintf() 函數輸出。

所以在 Linux 下要顯示 BMPString,一般方法是先將 UTF-16 big-endian 方式的字元編碼擴充為 UTF-32 big-endian 方式的字元編碼,擴充方法是在前面加上兩個位元組的 0,再將UTF-32 big-endian 方式的字元編碼變為 UTF-32 little-endian 方式的字元編碼。(也可先將 UTF-16 big-endian 方式的字元編碼轉換為 little-endian 順序,再給後面加上兩個位元組的 0,效果是一樣的。)

下面給出一個樣本程式:

/*************************************************** Author: HAN Wei* Author's blog: http://blog.csdn.net/henter/* Date: Oct 31th, 2014* Description: demonstrate how to print BMPString  on Linux console**************************************************/ #include <stdio.h>#include <stdlib.h>#include <string.h>#include <locale.h>#include <wchar.h>/***************************************************函數名稱:PrintBMPStringOnLinux*功能: 在 Linux 終端下輸出 BMPString*參數:    BMPString     [in]          BMPString_len [in]   BMPString 的長度,以位元組為單位*傳回值:    0   成功    -1  失敗**************************************************/int PrintBMPStringOnLinux(unsigned char *BMPString, unsigned int BMPString_len){  unsigned char *buffer;  unsigned int buffer_len, i;  unsigned char *p, *q;  buffer_len = BMPString_len * 2 + 4; /* 緩衝區大小為 BMPString 的位元組長度的兩倍                                         再加上四個位元組,這四個位元組用來存放字串 結束符 \0(其類型為 wchar_t ),其對應 編碼是 0x0, 0x0, 0x0, 0x0 */  if ( !(buffer = (unsigned char *)malloc(buffer_len)) )  {#ifdef _DEBUG    printf("malloc() function failed!\n");#endif    return (-1);  }  memset(buffer, 0, buffer_len);  p = buffer;  q = BMPString;  for (i=0; i < (int)BMPString_len/2; i++)  {    *p = *(q+1);    *(p+1) = *q;    p += 4;    q += 2;  }  setlocale(LC_ALL, "zh_CN.utf8");  wprintf(L"BMPString: %ls\n", (wchar_t *)buffer);  free(buffer);  return 0;}int main(void){  int error_code;  unsigned char BMPString_data1[]={0x4e, 0x2d, 0x56, 0xfd};  /* 中文字串"中國"對應的 Unicode 編碼 */  unsigned char BMPString_data2[]={0x0, 0x55, 0x0, 0x73, 0x0, 0x65, 0x0, 0x72};  /* 英文字串"User"對應的 Unicode 編碼 */  char str[]={0x2d, 0x4e, 0x0, 0x0, 0xfd, 0x56, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};  if ( error_code = PrintBMPStringOnLinux(BMPString_data1, sizeof(BMPString_data1)) )  {   printf("Print BMPstring on Windows console failed!\n");   return (-1);  }  if ( error_code = PrintBMPStringOnLinux(BMPString_data2, sizeof(BMPString_data2)) )  {   printf("Print BMPstring on Windows console failed!\n");   return (-1);  }/* 下面給出了說明 unicode 編碼的字元在 Linux 中是如何存放的一個例子,   從顯示結果可以看出:對於每一個用 wchar_t 類型儲存的 UTF-16 字元,   長度為 4 位元組,以 little-endian 順序存放 */  printf("\n");  setlocale(LC_ALL, "zh_CN.utf8");  wprintf(L"%ls\n", (wchar_t *)str);  return 0;}

該程式在 64 位元 CentOS 下,用 64位 GCC 編譯器編譯後執行,輸出結果如:

在Linux的終端中顯示BMPString的內容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.