我想往非接触IC卡中输入汉字,但是只能输入32位十六进制数。怎么将汉字转换为32位十六进制数进行保存,并将32位十六进制数读出转换成汉字。

解决方案 »

  1.   

    貌似汉字在内存中就是16位十六进制的。要转换吗? 要转换也是把16bit扩充到32bit吧
      

  2.   

    //将字符(汉字、字母、符号...)转换为unicode码(16进制)
    CharToUnicode()
    {
    CString strIn, strOut, str;
    m_data.GetWindowText(strIn);
    if(strIn=="") return; const char *p = LPCSTR(strIn);
    wchar_t wszstr[256];
    //转化后的unicode码存放在wszstr数组中
    MultiByteToWideChar(CP_ACP, 0, p, -1, wszstr, 256);
    int j=0;
    while (wszstr[j]!='\0')
    {
    str.Format("%d", wszstr[j]);
    char s[5];
    itoa(atoi(str), s, 16);
    str = s;
    int k = str.GetLength();

    for(int i=0; i<4-k; i++)
    {
    str.Insert(0, "0"); //若少于4个字符,用'0'字符在其左则补齐。
    }
    strOut += str; if(m_septsign.GetCheck()==BST_CHECKED) 
    strOut +=" "; j++;
    }
    strOut.MakeUpper(); //大写处理
    m_out.SetWindowText(strOut);

    }
    //将unicode码转换为字符
    UnicodeToChar()
    {
    CString strIn, str;
    m_data.GetWindowText(strIn);
    strIn.Remove(' ');  //若有空格分隔符,将其移除
    if(strIn.GetLength() % 4 || !IsUnicodeChar(strIn)) 
    {
    MessageBox("输入的Unicode有错误!","警告");
    return;
    }
        wchar_t wch[256];
    for(int i=0; i<strIn.GetLength()/4; i++)
    {
    int n_vaule = strtol(strIn.Mid(i*4, 4),NULL,16);
    wch[i] = n_vaule;
    }
    wch[i] = 0;
    CString strValue(wch);
    m_out.SetWindowText(strValue);
    }//判断字符串中是否为unicode字符
    bool IsUnicodeChar(const char *str)
    {
    for(int i=0; str[i]!=0; i++)
    {
    if(str[i]<'0') return false;
    if(str[i]>'9' && str[i]<'A') return false;
    if(str[i]>'F' && str[i]<'a') return false;
    if(str[i]>'f') return false;
    } return true;
    }
    具体可以卸载我共享的一个资源
    http://download.csdn.net/source/2215007
      

  3.   

    一个汉字占用两个字节,你按这种方法进行转换就可以了。
    char cn[]="我是中国人";
    int cnl = lstrlen(cn)/2;//汉字个数
    DWORD *dwcn=new DWORD[cnl];//用于存放汉字,一个汉字对应一个DWORD,32位
    memset(dcn,0,cnl*sizeof(DWORD));
    WORD *wcn = (WORD *)cn;
    for(int i=0;i<cnl;i++)
    {
      dwcn[i]=wcn[i];
    }//DWROD转汉字
    for(int i=0;i<cnl;i++)
      wcn[i]=dwcn[i] & 0x0000ffff;
      

  4.   

    http://blog.csdn.net/zhixingzhe/archive/2009/09/28/4610063.aspx
      

  5.   

    3楼按你的代码出现如下错误,应该怎么定义一下?
    D:\MyProjects\lll\lllDlg.cpp(66) : error C2065: 'm_Data' : undeclared identifier
    D:\MyProjects\lll\lllDlg.cpp(66) : error C2440: '=' : cannot convert from 'char [1]' to 'int'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    D:\MyProjects\lll\lllDlg.cpp(175) : error C2065: 'm_data' : undeclared identifier
    D:\MyProjects\lll\lllDlg.cpp(175) : error C2228: left of '.GetWindowTextA' must have class/struct/union type
    D:\MyProjects\lll\lllDlg.cpp(197) : error C2065: 'm_septsign' : undeclared identifier
    D:\MyProjects\lll\lllDlg.cpp(197) : error C2228: left of '.GetCheck' must have class/struct/union type
    D:\MyProjects\lll\lllDlg.cpp(203) : error C2065: 'm_out' : undeclared identifier
    D:\MyProjects\lll\lllDlg.cpp(203) : error C2228: left of '.SetWindowTextA' must have class/struct/union type
    Error executing cl.exe.
      

  6.   

    直接用 unsigned long 来读取汉字的数值,出来的就是他的 32 位数了.
    如果把这个 unsigned long 又当作 char* 来用字符显示函数去显示,又变成汉字了.
    楼主写一写你数据保存的方式,写个代码给你,很简单的.
      

  7.   

    // ReadWriteTestDlg.cpp : implementation file
    //#include "stdafx.h"
    #include "ReadWriteTest.h"
    #include "ReadWriteTestDlg.h"
    #include "mwrf32.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CReadWriteTestDlg dialogCReadWriteTestDlg::CReadWriteTestDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CReadWriteTestDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CReadWriteTestDlg)
    m_Port = -1;
    m_Sector = 0;
    m_key = _T("");
    m_OpMode = -1;
    m_Data = _T("");
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    icdev = NULL;
    }void CReadWriteTestDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CReadWriteTestDlg)
    DDX_Control(pDX, IDC_KEY_MODE, m_KeyModeCtrl);
    DDX_Control(pDX, IDC_STATUS, m_Status);
    DDX_Control(pDX, IDC_COMBO_BAUND, m_BaundCtl);
    DDX_Radio(pDX, IDC_RADIO1, m_Port);
    DDX_Text(pDX, IDC_SECTOR, m_Sector);
    DDX_Text(pDX, IDC_EDIT8, m_key);
    DDV_MaxChars(pDX, m_key, 12);
    DDX_Radio(pDX, IDC_READBLOCK, m_OpMode);
    DDX_Text(pDX, IDC_DATA, m_Data);
    DDV_MaxChars(pDX, m_Data, 32);
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CReadWriteTestDlg, CDialog)
    //{{AFX_MSG_MAP(CReadWriteTestDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
    ON_BN_CLICKED(IDC_BTN_DISCONNECT, OnBtnDisconnect)
    ON_BN_CLICKED(IDC_TEST, OnTest)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CReadWriteTestDlg message handlersBOOL CReadWriteTestDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here

    m_Port = 0;
        m_BaundCtl.SetCurSel(6);
    m_OpMode = 0;
    m_KeyModeCtrl.SetCurSel(0);
    m_key = "FFFFFFFFFFFF";
    UpdateData(FALSE);
    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CReadWriteTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CReadWriteTestDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CReadWriteTestDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }void CReadWriteTestDlg::OnBtnConnect() 
    {
    UpdateData(TRUE); __int16 st;
    unsigned char _Status[30];
    memset(_Status,0,30); m_baund = GetBaund();
    rf_exit(icdev);
    icdev = rf_init(m_Port,m_baund);
    st = rf_get_status(icdev,_Status);
    if(icdev<0 || st)
    m_Status.SetWindowText("连接失败!");
    else
    {
    rf_beep(icdev,10);
    m_Status.SetWindowText("连接成功!");
    }

    UpdateData(FALSE);
    }void CReadWriteTestDlg::OnBtnDisconnect() 
    {
        __int16 st;

    if(icdev == NULL || icdev <0)
            m_Status.SetWindowText("端口未初始化!"); st = rf_exit(icdev);
    if(st)
    m_Status.SetWindowText("断开连接失败!");
    else
    m_Status.SetWindowText("断开连接成功!"); UpdateData(FALSE);
    }void CReadWriteTestDlg::OnTest() 
    {
    UpdateData(TRUE);

    unsigned char Sec = m_Sector;
        __int16 st;
        unsigned __int16 TagType;
    unsigned long Snr;
    unsigned char Size;
        unsigned char data[33];
    memset(data,0,33);

    st = rf_reset(icdev,5);

    st = rf_request(icdev,1,&TagType);
    if(st)
    {
    m_Status.SetWindowText("寻卡失败!");
    return;
    }
    st = rf_anticoll(icdev,0, &Snr);
    if(st)
    {
    m_Status.SetWindowText("防冲突失败!");
    return;
    }
    st = rf_select(icdev,Snr,&Size);
    if(st)
    {
    m_Status.SetWindowText("选卡失败!");
    return;
    }
    unsigned char key[7];
    memset(key,0,7);
    a_hex(m_key.GetBuffer(12),key,12);
    m_key.ReleaseBuffer();
    unsigned char m_keymode;
    if(m_KeyModeCtrl.GetCurSel()==0)
    m_keymode = 0;
    else
    m_keymode = 4;
    st = rf_load_key(icdev,m_keymode,Sec,key);
    if(st)
    {
    m_Status.SetWindowText("装载密码失败!");
    return;
    }
    st = rf_authentication(icdev,m_keymode,Sec);
        if(st)
    {
    m_Status.SetWindowText("认证失败!");
    return;
    }
    if(m_OpMode == 0 )
    {
    unsigned char temp[33];
    memset(temp,0,33);


    if(m_OpMode == 0)
    st = rf_read(icdev,Sec*4,data);

    if(st)
    {
    m_Status.SetWindowText("读数据失败!");
    return;
    }
    hex_a(data,(char*)temp,16);
    m_Data.Format("%s",temp);


    m_Status.SetWindowText("读数据成功!");
    }
    else if(m_OpMode == 1)
    {
    if(m_Data == "" )
    {
    MessageBox("请确认是否有输入三个块的数据!","读写测试",MB_OK|MB_ICONWARNING);
    return;
    }
    unsigned char temp[33];
    memset(temp,0,33);
    memset(data,0,33);
    a_hex(m_Data.GetBuffer(32),temp,32);
    m_Data.ReleaseBuffer();
    memcpy(data,temp,16);

    if(m_OpMode == 1)
    st = rf_write(icdev,Sec*4,data);

    if(st)
    {
    m_Status.SetWindowText("写数据失败!");
    return;
    }
            
    m_Status.SetWindowText("写数据成功!");
    }
    rf_beep(icdev,10);

    UpdateData(FALSE);
    st = rf_halt(icdev);
    }long CReadWriteTestDlg::GetBaund()
    {
    switch(m_BaundCtl.GetCurSel())
    {
    case 0:
    return 9600;
    case 1:
    return 14400;
    case 2:
    return 19200;
    case 3:
    return 28800;
    case 4:
    return 38400;
    case 5:
    return 57600;
    case 6:
    return 115200;
    }    return 9600;
    }
    这个是代码,帮忙加一下转换汉字,因为第一次用,需要在配置定义什么也请大家告诉我,谢谢
      

  8.   

    楼主,加个QQ,发个工程来看看,你这样飞代码出来,不够直接喔.QQ 644832501