天使と悪魔選手権

unsigned long の値が例えば'aufx'が入っていたとして
その値を元にC Stringsの"aufx" もしくは NSString の @"aufx"を作りたいのですがどのようにしたらよいでしょう。
目的はNSDictionary の key にしたいのですが

//includeされています
//typedef unsigned long FourCharCode;
//typedef FourCharCode OSType;


OSType osType;
osType = kAudioUnitType_Effect; //'aufx' 61756678
char no1;
char no2;
char no3;
char no4;
char osTypeStrings[5];

no1 = (osType>>24)|(osType<<8);
no2 = (osType>>16)|(osType<<16);
no3 = (osType>>8)|(osType<<24);
no4 = osType;

osTypeStrings[0] = no1;//'a'
osTypeStrings[1] = no2;//'u'
osTypeStrings[2] = no3;//'f'
osTypeStrings[3] = no4;//'x'
osTypeStrings[4] = '?0';


のようにしてもうまくいかず
いろいろ試したのですがうまくいかないのですが
どのようにすればベストですか?
もっと簡潔な方法もありますか?
解る方、すみませんがよろしくお願いします。

A 回答 (3件)

char str[ sizeof( OSType ) + 1 ];


*reinterpret_cast < OSType * > ( str ) = fcc;
str[ sizeof( str ) - 1 ] = '\0';

とかどうでしょうか。
    • good
    • 0
この回答へのお礼

ありがとうございます。
できました。

お礼日時:2005/04/03 15:43

全然ベストではないですが…


union X {
unsigned long x;
char c[4];
};

unsigned long x=0x61756678;
char s[5]=" ";
union X wk;
wk.x = x;
s[0]=wk.c[3];
s[1]=wk.c[2];
s[2]=wk.c[1];
s[3]=wk.c[0];
}
上記はインテル系でモトローラ系だと逆順(wk.cの添字が0~3)になります。
    • good
    • 0
この回答へのお礼

ありがとうございます。
できました。

お礼日時:2005/04/03 15:43

unsigned long x=0x61756678;


char s[5];
unsigned char mask=0xff;
s[0] = (x>>24)&mask;
s[1] = (x>>16)&mask;
s[2] = (x>> 8)&mask;
s[3] = x&mask;
s[4] = '\0';
とかでどうでしょうか。
    • good
    • 0
この回答へのお礼

ありがとうございます。
できました。

お礼日時:2005/04/03 15:43

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!