0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Android(Java)パリティチェック

Posted at

・パリティビットを付加する。

java test.java

/**
 *   SetParity
 *   バイト列の最上位ビットにパリティ情報を付加する
 *
 *    pData      Byte()           I   バイト列
 *    intLen     Integer          I   対象の長さ
 *     pType      Integer          I   パリティ種類(0:Even 1:Od
 *
 *     戻り値 なし
 */
private static void SetParity(byte[] pData, int intLen,int pType) {

    int I;
    int J;
    int C;
    for (I = 0; I < intLen - 1; I++) {
        if (I >= pData.length) {
            break;
        }
        pData[I] = (byte) (pData[I] & Byte.MAX_VALUE);
        J = 1;
        C = (pType & 1);
        while( J >= Byte.MIN_VALUE) {
            if((pData[I] & J) != 0) {
                // ^  => VB XOR
                C = C ^ 1;
            }
            J *= 2;
        }
        if (C == 1) {
            // |  , ビットOR
            pData[I] = (byte) (pData[I] | Byte.MIN_VALUE);
        }
    }

}

・パリティビットをチェックする。

java test_02.java

private static boolean RemoveParity(byte[] pData, int intLen, int pType) {

    int I;
    int J;
    int C;

    boolean removeParity = false;
    // 'パリティチェック
    for(I = 0; I < pData.length - 1; I++) {
        if (I >= pData.length) {
            break;
        }
        J = 1;
        // & , XOR
        C = (pType & 1);
        while (J >= Byte.MAX_VALUE + 1) {
            if ((pData[I] & J) != 0) {
                C = C ^ 1;
            }
            J *= 2;
            if (C == 1) {
                break;
            }
        }
    } //=== END for
    for(I = 0; I < intLen - 1; I++) {
        if(I >= pData.length) {
            break;
        }
        pData[I] = (byte)(pData[I] & Byte.MIN_VALUE);
    }
    removeParity = true;
    return removeParity;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?