Tiago Cunha

Greenhorn
+ Follow
since Feb 25, 2011
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tiago Cunha

Guys, maybe some of you can help me. I have a SCJP 6 certification that i'm trying to put in my acclaim, there is some way to generate a badge of it? Thank you all!
Friends,

Please, i have one byte like "byte b = 120", and i want to get the MSB (if it is 0 or 1) of this byte "b", i search in internet but i didn't found a way to make this...

8 years ago
Right Stephan, let me explain the purpose: i got a protocol of communication that specifies some bytes/values.. like this:

bytes[10] (bytes[0..2] is one value that represent one thing, bytes[3..9] represent another thing)... like this, and these 2 methods help me to put a value into a sequence of bytes, like, i want the value "-45" in a byte[] of size 2, so i use the method "longToByteArray(long, 2)" and i receive back this long in a byte[2] so i can put it between bytes[0..2] and bytes[5..9] for example, because when the message be received in the other side the machine will take this bytes[3..4] and convert to a Long using the method byteArrayToLong(bytes, 2, false) for example and must receive the "-45" value...

i tried to use a low value with the modification that you give to me, still not working, like using -45 for example when i run give me this output:


maybe i can change all bits value like all "1" turn "0" and "0" in "1" to change de sign? it can work? please help me.
8 years ago

Stephan van Hulst wrote:The problem is that you are only copying the least significant bytes, which will drop the sign bit most of the time. The solution is to set the sign bit explicitly after you're done copying.
Another tip: Don't use lowercase L as an identifier, or to terminate long literals. It looks dangerously similar to the numeric value 1.
You should call your parameter something like value, and you should use long literals like 65000L.



Stephan, thanks for the help, i tried what you said and i put this into the end of the method longToByteArray:



but now the return is:


still not working, maybe i made something wrong?
8 years ago
Friends,

I got two methods to make a conversion from byte array to long and the other to make from long to byte array, the problem is that when i use a number with a negative signal the code broken and the result is not the expected, so, if someone can give me a help i'll be thankful so much cause i tried everything but nothing worked

byteArrayToLong:


longToByteArray:



A running that gives a good result:


Now with a negative value that give the erro:

8 years ago