Search code examples
apex-codevisualforce

OR operator can only be applied to Boolean expressions or to Integer or Long expressions?


I have the following code:

public class Testcode {



private static final Long[] P = new Long[18];             

public void setKey( string key )    

{    

integer i, j, k;    

long data;    

integer N = 16;    

string[] keytemp = new string[]{}; keytemp.add(key);



// Initialize P and S.    

for ( i = 0; i < N + 2; ++i ){        

P[i] = Pinit[i];        

}              

// XOR the key into P.    

j = 0;    

for ( i = 0; i < N + 2; ++i )        

{        

data = 0;        

for ( k = 0; k < 4; ++k )        

{       

data = ( data << 8 ) | keytemp[j];        

++j;        

}        

P[i] ^= data;        

}              

}   



private static final long[] Pinit = new Long[] {       

604135516L,   2242044355L,  320440478L ,  57401183L,        

2732047618L,  698298832L,   137296536L ,  3964563569L,        

1163258022L,  954160567L,   3193502383L,  887688400L,        

3234508543L,  3380367581L,  1065660069L,  3041631479L,        

2420952273L,  2306437331L       

};      

}

im getting the following error:

Error: Compile Error: OR operator can only be applied to Boolean expressions or to Integer or Long expressions at line 36 column 18

which is in this line:

   data = ( data << 8 ) | keytemp[j];

Is there another way to write this line of code?

Thanks


Solution

  • I'm assuming that the keytemp array contains strings of length 1 since Apex doesn't have a Character primitive. You'll have to convert the first character of each string to an integer and then perform the OR.

    Unfortunately Apex doesn't appear to have a built-in way of getting the ASCII value of a single-character String. You may have to write your own convertor function. Here are some people with the same issue with some proposed solutions:

    Discussion of how to convert strings to ASCII values