Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

this is pretty simple really... what i need is to compare some byte array obtain from the header of a file, to decimal value (to verify the signature) and unfortunatly, the 4 first byte of a jpg are not chars... they are plain hex value, wich Is why i need to do this, here's what I go so far:

if(entete.SOI!=0xFFD8)
		{
			return 0;
		}
		if(entete.JFIFMarker!=0xFFE0)
		{
			return 0;
		}

 

but the problem is that 0xFFD8 is seen as an integer and entete.SOI is a byte[2] so he wont compare them, so is there a way to:

1-change a byte array into integers

2-change an int into a byte array...

3 something else

Posted

Sure there's a number of ways to do it. You can shift and or them together or better yet use a pointer to copy the value.

 

But the real question is why is SOI a byte array in the first place? It seems better suited to be an int or short. Generally you shouldn't expose arrays publically anyway.

"Who is John Galt?"
Posted

well it's a structure so everything is just public in it...

puting it as a short is a good idea... that's what i'll do.

but just for my personal (and others) knowledge, how would you do it?

You can shift and or them together or better yet use a pointer to copy the value.
Posted

Are you reading this from a stream? Just use BinaryReader.ReadInt16() in that case.

But anyway, here's 2 ways to convert a byte[2] array into a short:

short s = (short)((b[1] << 8) | b[0]);

or in an unsafe block:

fixed (byte* pb = b) s = *(short*)pb;

"Who is John Galt?"
Posted (edited)
thanks, but just so someone reading this thread dont do mistake, using a short here wont work, because the value of 0xFFD8 =65496 and a shot stop at 32767, so a ushort (unsigned short) need to be used Edited by Shurikn

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...