1. 程式人生 > >:c語言漢字轉化成二進位制程式碼

:c語言漢字轉化成二進位制程式碼

**********************************************************

***********************************************************

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
/*
漢字轉化成二進位制碼 
*/
void conv(char *c)
{
    char ch[9] = {'\0'};
    int i;
    int j = 0;
    int count = 0;
    char t[3] = {'\0'};
    while('\0' != c[j])
    {
       for(i = 7;i >= 0;i--)
       {
            ch[i] = (c[j]&1)+'0';
            //右移 
            c[j]>>=1;
       }
       printf("%s\t",ch);
       j++;
    }
}
int main()
{
    char use;
    char c[100];
    printf("本程式實現漢字的二進位制編碼翻譯功能\n確認開始程式?(y/n)");
    use = getch();
    while(use =='y' || use =='Y')
    {
       printf("\n請輸入要轉化的字 :");
       scanf("%s",c);
       conv(c);
       printf("是否繼續轉換(y/n)");
       use = getch();
    }
    system(" PAUSE");
    return 0;

}

********************************************************

***********************************************************

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//漢字轉化成16進位制 
int main()
{
    char c[10] = "哈爾濱";
    for(int i=0;i<6;i++)
    {
        printf("%x\n",c[i]&0xff);
   
    }
    system("pause");
    return 0; 
}

國標GB2312-80規定,全部國標漢字及符號組成94×94矩陣,每行稱區,每列稱位,這種編碼方式就叫做區位碼. 因區碼和位碼組合是在01至94範圍內,會與ASCII碼產生衝突,於是制定了漢字內碼:規定為高位內碼=區碼+20H+80H; 低位內碼=位碼+20H+80H.所以各種輸入法都是按這個標準去編寫的.因此,把一個漢字表示為兩個位元組的二進位制碼,這種編碼稱為 區位 碼,或稱為內碼.