Help with java converting byte to int .... int to byte

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
dreampeppers99
Posts: 77
Joined: Mon Aug 21, 2006 4:19 am

Help with java converting byte to int .... int to byte

Post by dreampeppers99 » Tue Aug 29, 2006 6:14 am

I made a method to assembler 8 bit into a unique byte.
this follow.
This servers to assembles the 7 flags of cpu.

public static int juntar()
{
int tmp = 0x0 ; //inicializo com 00000000 senão inicializar não aceita de cara o |=

tmp = (
((flagN) ? 128 :0) |
((flagV) ? 64 : 0) |
32 |
((flagB) ? 16 : 0) |
((flagD) ? 8 : 0) |
((flagI) ? 4 : 0) |
((flagZ) ? 2 : 0) |
((flagC) ? 1 : 0)
) ;
return tmp; //I dont know what but if i put byte like a return type this return -78
}

//supossing that i put this value on the variables
byte tmp;
flagN = true ; //10000000
flagV = false; //00000000
//00100000
flagB = true ; //00010000
flagD = false; //00000000
flagI = false; //00000000
flagZ = true ; //00000010
flagC = false; //00000000
//10110010 = 178 decimal or B2 hexa.

Then, this method , if i put the byte type to return it return -78 but if i put int it return 178 , 178 is that correct.
Why this occors?

I need this number, the correct, in byte. And i cannot covert the int to byte?
I try Byte.valueOf(tmp) , but it not works too.
If you can help me I will thanks you !!

User avatar
dXtr
Posts: 375
Joined: Tue Sep 21, 2004 12:11 am
Location: Karlshamn (Sweden)

Post by dXtr » Tue Aug 29, 2006 6:30 am

well.. it is correct.. but java doesn't support unsigned data types.. that's why you get -78 instead of 178.

oh and btw. here is a little read if you're interested:
http://www.darksleep.com/player/JavaAnd ... Types.html
Sorry for misspellings, I'm from Sweden ^^

User avatar
dreampeppers99
Posts: 77
Joined: Mon Aug 21, 2006 4:19 am

Post by dreampeppers99 » Tue Aug 29, 2006 7:06 am

dXtr wrote:well.. it is correct.. but java doesn't support unsigned data types.. that's why you get -78 instead of 178.

oh and btw. here is a little read if you're interested:
http://www.darksleep.com/player/JavaAnd ... Types.html

but if i want the initialize one variable byte with 0;

//isvalid?
byte m = 0;

//how i atribute? this i can but ... this byte m = 129 ; I cannot ?
//rigth?

User avatar
dreampeppers99
Posts: 77
Joined: Mon Aug 21, 2006 4:19 am

Post by dreampeppers99 » Tue Aug 29, 2006 7:16 am

So, if i am writing my emulator on java, i must create all variables like short and not byte.

I need to change array of memory to short too?

mozz
Posts: 94
Joined: Mon Mar 06, 2006 3:42 pm
Location: Montreal, canada

Post by mozz » Tue Aug 29, 2006 8:01 am

I think this thread belongs in the newbie forum =)
dreampeppers99 wrote:So, if i am writing my emulator on java, i must create all variables like short and not byte.

I need to change array of memory to short too?
If you're going to do that, you might as well do it properly and change it to an array of doubles!

...or, you could stick with an array of bytes and just be careful how you handle them, for example, when you read from memory assign the byte to an int and mask it with 0xFF.

Post Reply