Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary to Hex to Decimal.. Weird Hex Values FF FD should be 2 decimal

Status
Not open for further replies.

altendew

Programmer
Mar 29, 2005
154
US
This is kind of weird im trying to pull the number of layers from a .psd file.

I get to the section where it contains the information. Its a 2 byte section (short integer)

But it looks like this in hex

FF FD and some how its suppose to equal 2?

FF FC = 3
FF FB = 4

& so forth it seems backwards.

Now how can I convert that in php the hex to the decimal.
 
You can use the hexdec() function to convert hexadecimal values to their decimal equivalents.

However the values you posted do not correspond to each other. FFFD is 65533 not 2. So I'm not really sure what those values would be.

Can you maybe describe where those letter values are coming from?>

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Well take a look at how its doing it backwards

In hex FFFF is the highest you can go correct?

Now the second highest would be FFFE

then FFFD

FFFF = 0
FFFE = 1
FFFD = 2
FFFC = 3

I am not sure why photoshop is doing it backwards like this?
 
Also to follow up i noticed the 65533 I have tried it and didnt understand then I started adding more layers and looking at the hex values and noticed it was doing it backwards.
 
Well actually it could be FFFFFF.

But any way , what is Photoshop doing with those numbers?

Here's the deal with hexadecimal:

from 0 to 9 the numbers are just regular integers. 1 2 3 4 5 6 7 8 9 . When it reaches 10 then it uses letters A is 10 B is 11 C is 12 D is 13 E is 14 and F is 15. That means it has a base of 16 numbers from 0 to 15. "Hexadecimal"

Hexadecimal numbers are also used to define colors. FFFFFF would be white. while 000000 would be black.

Photoshop may just be producing a different number such as for Saturation. where White has 0 saturation. and colors from there would have a different Saturation value.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I think it can be a two's complement. A Two's complement is a binary representation of a negative number.

You can make a two's complement by supplying an extra 1 in front of the zeros that make up zero (so for a two byte integer, 0000 is zero and 10000 is your new base) and subtract the number you want to negate, so minus 1 is
10000
0001 -
------
FFFF is the two's complement.

You can do this even faster by negating all the bits and then adding 1 (so, 0001 negated becomes FFFE, and with 1 added it becomes FFFF) This is how integer arithmetic works in your processor.

In that case, FFFD would be -0003.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
According the Adobe Photoshop file specification they use the big endian format.

So a value of 2 should be displaying as 0002 just suprised its not.

Could you show me a PHP example of how to negate and subtract?
 
This is what I have to-do to get the result doesnt seem correct though..

$short = @reset(unpack('n',$data));
$normal = hexdec(bin2hex(unpack('n',-$short)));

What I do is unpack the binary code as "unsigned short (always 16 bit, big endian byte order)"

Then I negate it then convert it to hex then convert it to decimal

Is there an easier way?
 
Correct code..

$short = @reset(unpack('n',$data));
$normal = hexdec(bin2hex(pack('n',-$short)))-1;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top