2

I'm still trying to add a jpeg image to a person in Open LDAP, using Novell's c# libraries:

Openldap: Add jpegPhoto to inetOrgPerson

There is a constructor for Novell.Directory.Ldap.LdapAttribute the with the following signature:

public LdapAttribute(string attrName, sbyte[] attrBytes)

So, I plan to use this one. But, how to convert a jpeg image from a MemoreyStream to sbyte[]?

MemoryStream.ToArray() 

method returns byte[] and I don't know how to do it.

Community
  • 1
  • 1
Oscar
  • 13,594
  • 8
  • 47
  • 75

2 Answers2

3

You can convert the array like this:

Array.ConvertAll(bytes, b => (sbyte)b)
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    Much better than my `MemoryStream.ToArray().Select(a=>(sbyte)a).ToArray()` idea I was going to post. – Scott Chamberlain May 30 '14 at 14:03
  • Thanks for your answer. But in the original array there are values greater than 127. What will happen? Will cause errors? The conversion won't make the image unreadable? – Oscar May 30 '14 at 14:04
  • 1
    @Oscar: They will be viewed as negative numbers; the underlying bytes will not change. This must be done in an `unchecked` context. – SLaks May 30 '14 at 14:06
  • 1
    Actually, the C# `byte` is unsigned - http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.90%29.aspx – David Crowell May 30 '14 at 14:17
  • @DavidCrowell: That's exactly why he's asking this. – SLaks May 30 '14 at 14:18
1

On the CLR you can constant-time convert a byte[] to an sbyte[]. See my previous answer on this little-known trick.

(sbyte[])(object)MemoryStream.ToArray(); //compiles and runs
Community
  • 1
  • 1
usr
  • 168,620
  • 35
  • 240
  • 369