0
난 임의의 숫자를 생성하는 C 코드에 대한 Java 상응하는 코드를 쓰는에 맞았다. C 코드는 다음과 같습니다. static void lng_cosem_CreateRandom (u8 * u8p_Array_p, u8 u8_Len_p) { u8_Indx; u32 u32_Temp;양수 난수 생성 및 오른쪽 시프트 8 비트 및 바이트 저장
public static void CreateRandom(byte[] COSEM_CTOS, byte
COSEM_CHALLENGE_LEN)
{
long temp;
int index;
pos = 0;
while(COSEM_CHALLENGE_LEN!=0)
{
Random rand = new Random();
temp = Math.abs(rand.nextInt());
System.out.println(temp + "absolute val");
for(index=0;index<4;index++)
{
COSEM_CTOS[pos++] = (byte) temp;
temp >>= 8;
System.out.println(temp + "right shift value");
COSEM_CHALLENGE_LEN--;
if(COSEM_CHALLENGE_LEN == 0)
{
break;
}
}
}
}
을하지만 오른쪽 시프트 벌려서는 양수를 원하는 후에 내가 음수를 얻을 다음과 같이
srand(GetTickCount());
#endif /* ABC_COMPILER_USED */
while(u8_Len_p)
{
u32_Temp = SYS_GetRandom32();
for(u8_Indx = 0; u8_Indx < 4; u8_Indx++)
{
*u8p_Array_p++ = (u8)u32_Temp;
u32_Temp >>= 8;
u8_Len_p--;
if(!u8_Len_p)
{
break;
}
}
}
}
나는 위의 자바 상응하는 코드를 썼다. 내가 어떻게 해 ?
난 정말 이해가 안
public static void CreateRandom(byte[] COSEM_CTOS, byte COSEM_CHALLENGE_LEN)
{
ByteBuffer b = ByteBuffer.allocate(4);
byte[] temp = b.array();
int index;
pos = 0;
while(COSEM_CHALLENGE_LEN!=0)
{
Random rand = new Random();
rand.nextBytes(temp);
for(index=0;index<4;index++)
{
COSEM_CTOS[pos++] = temp[index];
COSEM_CHALLENGE_LEN--;
if(COSEM_CHALLENGE_LEN == 0)
{
break;
}
}
/* or we can use Math.abs(rand.nextByte(COSEM_CTOS)); */
}
}
enter code here
(BTW, 모든 동안-반복의 새로운 임의의 객체를 할당하는 이럴 아주 좋은 생각입니다) 여기에 서명 변수를 사용하는 것과 Java 코드 – mgokgoz
예, 알아요.하지만 Java에서 동일한 C 코드를 변환 할 수 있습니까? – Abinaya
@mgokgoz 그래서 어떻게 할 수 있습니까? 자바에서 서명되지 않은 번호? – Abinaya