Return type is byte[] instead of INT in webservices

时间:2015-06-25 19:06:31

标签: c# web-services integer type-conversion byte

I have an integer attribute in Active Directory Lightweight Directory services. In my webservices I access that attribute and read it's value. ... SearchResult results = searcher.FindOne(); if (results !=null) { if (results.Properties["userStatus"] != null) { user.Status = (UserStatusEnum) results.Properties["userStatus"][0]; } } ... This line returns correct data: results.Properties["userStatus"][0] Value = 1 (returns integer) However, I'm running into an issue when I point my external webservices to internal services. When I do that and I execute this line: results.Properties["userStatus"][0] for the same user, the same value it's now returning {byte1} [0]: 49 If I set the status to 2 it will return {byte1} [0]: 50 and so on... Why is this happening? How can I resolve this issue, or at least debug? I have tried to remove my external webservices out of the equation and pointed my web application directly to internal webservices. I get the same results. SOLVED Thanks to ps2goat I wrote some code to accomplish what I need. I'm still interested on finding out "Why it's working fine using internal webservices, but not when we call it using external webservice?! object statusObj = results.Properties["userStatus"][0]; if (statusObj is byte[]) { string statusString System.Text.Encoding.Default.GetString((byte[])statusObj); user.Status = (UserStatusEnum)Convert.ToInt32(statusString); } else { user.Status = (UserStatusEnum)Convert.ToInt32(statusObj); }

2 个答案:

答案 0 :(得分:0)

49是整数1的ASCII值。看起来您需要将字节值转换回char s,然后转换为int s。

这里有一个要引用的ASCII表:klcconsulting.net/ascii.htm查看十进制值49,看它是ASCII字符1的十进制值。如果用户值为10,则应返回两个字节{49, 48},第一个用于1,第二个用于0

答案 1 :(得分:-1)

So, I've had to deal with a similar problem to this before. As an int, those are the correct results, but the literal version of it is +48 (if you stay within 0-9) because that is where the numbers in the character code begins. If you subtract 48 you will get your answer within the first 10 digits, once you get over that it becomes a little more crazy to get the information.