2

char 配列を unsigned short (UInt16) に変換する際に問題があります。私の変換テクニックは間違っているようです...コードは次のとおりです。

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int _tmain(int argc, _TCHAR* argv[])
{
    // Symbols 0x1210 :
    char test[2];
    test[0] = 0x12;
    test[1] = 0x10;

    unsigned short n;
    memcpy(&n, test, sizeof(unsigned short));

    int i=0, arrToInt=0;
    for(i=1;i>=0;i--)
        arrToInt =(arrToInt<<8) | test[i];

    /*
    Now are:

    n = 4114
    arrToInt = 4114

    But! -> 0x1210 == 4624
    */

    return 0;
}

char 配列を逆にする (せずに) 方法はありますか?

ご協力いただきありがとうございます!

4

1 に答える 1

3
for(i=0;i<=1;i++)
    arrToInt =(arrToInt<<8) | test[i];
于 2012-05-04T21:49:19.780 に答える