#include <stdio.h> int main() { int area_code,location_code; unsigned char c[3]={0}; FILE * fp = fopen("gb2312_table.c","wb"); char buf[100]; if(fp) { sprintf(buf,"#include \"gb2312.h\"\n\nunsigned short gb2312_table[]=\n{\n"); fwrite(buf,1,strlen(buf),fp); for(area_code=1;area_code<95;area_code++)//区码为1-94 { fputc('\t',fp); c[0]=area_code + 0xA0;//区码 + A0H 转换为机内码 高字节 for(location_code=1;location_code<95;location_code++)//位码为1-94 { c[1]=location_code+0xA0;//位码 + A0H 转换为机内码 低字节 if(area_code == 1 && location_code == 1) sprintf(buf," 0x%02X%02X",c[0],c[1]); else sprintf(buf,",0x%02X%02X",c[0],c[1]); fwrite(buf,1,strlen(buf),fp); } fputc('\n',fp);//每区一行 } sprintf(buf,"\n};\n\nint GetGB2312TableLen()\n{\n\treturn sizeof(gb2312_table)/sizeof(unsigned short);\n}\n"); fwrite(buf,1,strlen(buf),fp); fclose(fp); } return 0; }
这里多生成了一个函数GetGB2312TableLen,用于获取gb2312_table大小.
下面是一个GB2312区位码查询、转换、区位码全表网址:static int Utf8ToGb2312(char *sOut, int iMaxOutLen/*BYTE*/, const char *sIn, int iInLen/*BYTE*/) { char *pIn = (char *)sIn; char *pOut = sOut; size_t ret; size_t iLeftLen=iMaxOutLen; iconv_t cd = iconv_open("gb2312", "utf-8"); if (cd == (iconv_t) - 1) { perror("iconv_open()"); return -1; } size_t iSrcLen=iInLen; ret = iconv(cd, &pIn,&iSrcLen, &pOut,&iLeftLen); if (ret == (size_t) - 1) { perror("iconv()"); iconv_close(cd); return -1; } iconv_close(cd); return (iMaxOutLen - iLeftLen); }
len = Utf8ToGb2312((char*)gb2312,sizeof(gb2312),utf8,strlen(utf8)); printf("UTF8 TEXT LEN:%d converted len=%d\n",strlen(utf8),len); for(i=0;i<len;i++) { if(gb2312[i]<128)//为ANSC字符,每个字符用1个Byte存储 { printf("ASCII Encode \t-- code:%c \n",gb2312[i]); } else//为GB2312(简体中文),每个字用两个Byte存储 { printf("GB2312 Encode \t-- Area code:%02d%02d Machine code:0x%04x\n",gb2312[i]- 0xA0,gb2312[i+1]-0xA0,(gb2312[i]<<8)|gb2312[i+1]); i++;//别忘了GB2312字符需要2个Byte } }
static int Gb2312ToUtf16be(char *sOut, int iMaxOutLen/*BYTE*/, const char *sIn, int iInLen/*BYTE*/) { char *pIn = (char *)sIn; char *pOut = sOut; size_t ret; size_t iLeftLen=iMaxOutLen; iconv_t cd = iconv_open("UTF-16BE", "gb2312"); if (cd == (iconv_t) - 1) { perror("iconv_open()"); return -1; } size_t iSrcLen=iInLen; ret = iconv(cd, &pIn,&iSrcLen, &pOut,&iLeftLen); if (ret == (size_t) - 1) { perror("iconv()"); iconv_close(cd); return -1; } iconv_close(cd); return (iMaxOutLen - iLeftLen); }
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
#include <stdio.h> #include "bmp_header.h" //包含swscale.h头文件 #ifdef __cplusplus extern "C"{ #endif #include "libavformat/avformat.h" #include "libswscale/swscale.h" #ifdef __cplusplus } #endif int save_bgr24_to_yuv420p(const char * src_bmp24_file,const char * dst_yuv_data_file) { FILE *fp = NULL; struct SwsContext *pSwsCtx=NULL; uint8_t * bmp_data = NULL; int data_size = 0; int w=0,h=0; fp = fopen(src_bmp24_file, "rb");//打开图片 if(fp) { // 位图文件头 #ifdef _WIN32 BITMAPFILEHEADER bmpheader={0}; BITMAPINFO bmpinfo={0}; fread(&bmpheader,sizeof(BITMAPFILEHEADER),1,fp); fread(&bmpinfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp); w = bmpinfo.bmiHeader.biWidth; h = bmpinfo.bmiHeader.biHeight; data_size = bmpheader.bfSize - bmpheader.bfOffBits; #else FileHead bmp_head; Infohead bmp_info; fread(&bmp_head,sizeof(FileHead),1,fp); fread(&bmp_info,sizeof(Infohead),1,fp); w = bmp_info.biWidth; h = bmp_info.biHeight; data_size = bmp_head.bfSize - bmp_head.bfOffBits; #endif if(h<0)h=-h; if(data_size != w * h * 3) { printf("not 24 bit bmp,file size = %d,w=%d,h=%d\n",data_size,w,h); fclose(fp); return -1; } bmp_data = (uint8_t *)malloc(data_size); memset(bmp_data,0,data_size); if(bmp_data) { fread(bmp_data,data_size,1,fp); } fclose(fp); fp = NULL; } if(bmp_data) { pSwsCtx = sws_getContext( w, h, PIX_FMT_BGR24, w, h, PIX_FMT_YUV420P, SWS_POINT/*SWS_BILINEAR*/, NULL, NULL, NULL); if(pSwsCtx) { uint8_t *data[4]={bmp_data,NULL,NULL,NULL}; int linesize[4] ={w*3,0,0,0}; int height = 0; uint8_t * buffer = NULL; AVFrame * yuv_frame = avcodec_alloc_frame(); buffer = (unsigned char *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P,w,h)); memset(buffer,0,avpicture_get_size(PIX_FMT_YUV420P,w,h)); avpicture_fill((AVPicture*)yuv_frame,(uint8_t *)buffer,PIX_FMT_YUV420P,w,h); height = sws_scale( pSwsCtx, data, linesize, 0, h, yuv_frame ->data, yuv_frame ->linesize); fp = fopen(dst_yuv_data_file,"w"); if(fp) { char buf[1024]={0}; int i=0; sprintf(buf,"/*********************Y***************************/\nunsigned char data_Y[]={"); fwrite(buf,1,strlen(buf),fp); for(i=0;i<yuv_frame ->linesize[0]*height;i++) { if(!(i%16)) { sprintf(buf,"\n\t"); fwrite(buf,strlen(buf),1,fp); } if(i) { sprintf(buf,",0x%02X",*(yuv_frame ->data[0]+i)); } else { sprintf(buf," 0x%02X",*(yuv_frame ->data[0]+i)); } fwrite(buf,strlen(buf),1,fp); } sprintf(buf,"\n};\n//%d bytes\n/**************end of Y***************************/\n\n",yuv_frame ->linesize[0]*h); fwrite(buf,strlen(buf),1,fp); sprintf(buf,"/********************UV***************************/\nunsigned char data_UV[]={"); fwrite(buf,1,strlen(buf),fp); for(i=0;i<yuv_frame ->linesize[1]*height/2;i++) { if(!(i%8)) { sprintf(buf,"\n\t"); fwrite(buf,strlen(buf),1,fp); } if(i) { sprintf(buf,",0x%02X,0x%02X",*(yuv_frame ->data[1]+i),*(yuv_frame ->data[2]+i)); } else { sprintf(buf," 0x%02X,0x%02X",*(yuv_frame ->data[1]+i),*(yuv_frame ->data[2]+i)); } fwrite(buf,strlen(buf),1,fp); } sprintf(buf,"\n};\n//%d bytes\n/*************end of UV***************************/\n\n",yuv_frame ->linesize[1]*h); fwrite(buf,strlen(buf),1,fp); fclose(fp); fp = NULL; } av_free(yuv_frame); av_free(buffer); sws_freeContext(pSwsCtx); pSwsCtx = NULL; } free(bmp_data); bmp_data = NULL; } return 0; }
#ifndef __BMP_HEADER_H__ #define __BMP_HEADER_H__ #ifndef _WIN32 typedef long BOOL; typedef long LONG; typedef unsigned char BYTE; typedef unsigned long DWORD; typedef unsigned short WORD; typedef struct { WORD bfType;//2 DWORD bfSize;//4 WORD bfReserved1;//2 WORD bfReserved2;//2 DWORD bfOffBits;//4 }__attribute__((packed))FileHead; typedef struct{ DWORD biSize;//4 LONG biWidth;//4 LONG biHeight;//4 WORD biPlanes;//2 WORD biBitCount;//2 DWORD biCompress;//4 DWORD biSizeImage;//4 LONG biXPelsPerMeter;//4 LONG biYPelsPerMeter;//4 DWORD biClrUsed;//4 DWORD biClrImportant;//4 }__attribute__((packed))Infohead; #endif//_WIN32 #endif //__BMP_HEADER_H__