test
splitData.cpp@0:44e295c34728, 2019-02-15 (annotated)
- Committer:
- yuto17320508
- Date:
- Fri Feb 15 09:57:11 2019 +0000
- Revision:
- 0:44e295c34728
bit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yuto17320508 | 0:44e295c34728 | 1 | /*intの数をchar型の欲しいbyte数に変換*/ |
yuto17320508 | 0:44e295c34728 | 2 | #include "splitData.h" |
yuto17320508 | 0:44e295c34728 | 3 | #include <cstdlib> |
yuto17320508 | 0:44e295c34728 | 4 | #include "math.h" |
yuto17320508 | 0:44e295c34728 | 5 | |
yuto17320508 | 0:44e295c34728 | 6 | |
yuto17320508 | 0:44e295c34728 | 7 | //////////////Intobyte内の関数 |
yuto17320508 | 0:44e295c34728 | 8 | void limitDataForByte(int data,int byte);//指定のbyteで送れる量に制限. |
yuto17320508 | 0:44e295c34728 | 9 | void SplitToAbsData(int wannaChange,int byte,char *candata,int firstElement);//絶対値をcharに分解 |
yuto17320508 | 0:44e295c34728 | 10 | void PutSignInTheLeft(int wannaChange,int byte,char *candata,int firstElement); |
yuto17320508 | 0:44e295c34728 | 11 | int Intsign(int num);//int型の数の符号を返す関数。正なら1,負なら-1 |
yuto17320508 | 0:44e295c34728 | 12 | |
yuto17320508 | 0:44e295c34728 | 13 | |
yuto17320508 | 0:44e295c34728 | 14 | void Intobyte(int wannaChange,int byte,char *candata,int firstElement)//指定されたbyte数のchar型に分解.一番左に符号 |
yuto17320508 | 0:44e295c34728 | 15 | { |
yuto17320508 | 0:44e295c34728 | 16 | limitDataForByte(wannaChange, byte); |
yuto17320508 | 0:44e295c34728 | 17 | SplitToAbsData(wannaChange,byte,candata,firstElement); |
yuto17320508 | 0:44e295c34728 | 18 | PutSignInTheLeft(wannaChange,byte,candata,firstElement); |
yuto17320508 | 0:44e295c34728 | 19 | } |
yuto17320508 | 0:44e295c34728 | 20 | |
yuto17320508 | 0:44e295c34728 | 21 | |
yuto17320508 | 0:44e295c34728 | 22 | void limitDataForByte(int data,int byte)//指定のbyteで送れる量に制限. |
yuto17320508 | 0:44e295c34728 | 23 | { |
yuto17320508 | 0:44e295c34728 | 24 | int limit=1<<(8*byte-1); //一番左に1 |
yuto17320508 | 0:44e295c34728 | 25 | if( abs(data) >= limit) { |
yuto17320508 | 0:44e295c34728 | 26 | data=Intsign(data)*(limit-1); |
yuto17320508 | 0:44e295c34728 | 27 | } |
yuto17320508 | 0:44e295c34728 | 28 | |
yuto17320508 | 0:44e295c34728 | 29 | } |
yuto17320508 | 0:44e295c34728 | 30 | |
yuto17320508 | 0:44e295c34728 | 31 | //絶対値をcharに分解 |
yuto17320508 | 0:44e295c34728 | 32 | void SplitToAbsData(int wannaChange,int byte,char *candata,int firstElement) |
yuto17320508 | 0:44e295c34728 | 33 | { |
yuto17320508 | 0:44e295c34728 | 34 | int absdata = abs(wannaChange); |
yuto17320508 | 0:44e295c34728 | 35 | for(int i=0; i<byte; i++) |
yuto17320508 | 0:44e295c34728 | 36 | candata[i+firstElement]=(absdata>>8*i)&255; |
yuto17320508 | 0:44e295c34728 | 37 | } |
yuto17320508 | 0:44e295c34728 | 38 | void PutSignInTheLeft(int wannaChange,int byte,char *candata,int firstElement) |
yuto17320508 | 0:44e295c34728 | 39 | { |
yuto17320508 | 0:44e295c34728 | 40 | if(wannaChange<0) { |
yuto17320508 | 0:44e295c34728 | 41 | int lastElement=firstElement+byte-1; |
yuto17320508 | 0:44e295c34728 | 42 | candata[lastElement]+=128; |
yuto17320508 | 0:44e295c34728 | 43 | } |
yuto17320508 | 0:44e295c34728 | 44 | } |
yuto17320508 | 0:44e295c34728 | 45 | int Intsign(int num)//int型の数の符号を返す関数。正なら1,負なら-1 |
yuto17320508 | 0:44e295c34728 | 46 | { |
yuto17320508 | 0:44e295c34728 | 47 | return (num>0)-(num<0); //num>0の時(num>0)=1,num<=0で(num>0)=0であることを利用。 |
yuto17320508 | 0:44e295c34728 | 48 | } |