数値のバイナリ配列への変換方法を教えてください
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
次の処理を考えていますが、バイナリ配列への変換方法(②)がわかりません。どのように処理すればいいでしょうか?
①数値データのバイト順を変換する《swapbytes》。 ②4バイトのバイナリデータとして、変数に格納する。
例えば、数値「1403110」を「3865580800」に変換し、「header」という変数に4バイトのバイナリデータとして格納(追記)したいです。.NETのGetBytes関数のような、一度に変換する方法などあればご教示ください。
01行>header = double('RIFF'); %headerに「RIFF」を格納する
02行>fileSize=1403110; %数値「1403110」を定義する
03行>xFileSize = swapbytes(fileSize); %「1403110」のバイト順を変換し、「3865580800」にする
04行>header = horzcat(wavHeader, xFileSize); %headerにバイト順を変換した数値を4byteで格納する
期待する結果: header = [82 73 70 70 230 104 21 0](ASCIIのdecimal表示)
上記を実行した結果:header = [82 73 70 70 3865580800]
0 commentaires
Réponse acceptée
michio
le 21 Juin 2018
Modifié(e) : michio
le 21 Juin 2018
header = double('RIFF'); %headerに「RIFF」を格納
fileSize = uint32(1403110); %数値「1403110」を uint32 で定義
xFileSize = swapbytes(fileSize); %「1403110」のバイト順を変換し、「3865580800」に
xFileSizeBin = dec2bin(xFileSize)
xFileSizeBin8 = reshape(xFileSizeBin,8,4)'
xFileSizeDec4 = bin2dec(xFileSizeBin8)'
header = horzcat(header, xFileSizeDec4)
で header = [82 73 70 70 230 104 21 0] という結果になります。変換の過程は出力をそれぞれ確認いただければと。まったくもって美しくないコードですが、お役に立ちましたら。。
2 commentaires
michio
le 22 Juin 2018
参考にしていただきありがとうございます。 おっしゃる通り、4行8列にする必要があるので、上の例では一旦 8行4列に reshape した上で、転置しております。
reshape(xFileSizeBin,8,4)'
MATLABで行列は Column-major orderメジャーで定義されるが故、バイナリ配列の順番を想定通りにするための措置でございます。
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur ビット単位の演算 dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!