Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getting value from Binary field
Message
From
28/01/2014 04:44:26
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01592445
Message ID:
01592479
Views:
42
This message has been marked as a message which has helped to the initial question of the thread.
>>>>
>>>>Since the size of the hex presentation is twice the size of the byte array, you have to multiply by 2
>>>>
>>>>>> StringBuilder sb = new StringBuilder(length * 2);
>>>>
>>>>
>>>>
>>>>The bitshift: shifting one bit to the left (provided the argument is not negative) is faster. I prefer that
>>>>
>>>>
>>>>>>           StringBuilder sb = new StringBuilder(length << 1);
>>>>
>>>
>>>Cute :-)
>>
>>Another thing I have found
>>
>>(1) You have class with an array member ( I tested static, expect instance also)
>>(2) A method using that array frequently ( a loop or so)
>>
>>It is faster to assign the array to a method variable and use the method variable instead of the class member
>
>Interesting. But how about:
       static string ToHex(byte[] bytes)
>        {
>            return "0X"+ BitConverter.ToString(bytes).Replace("-",string.Empty);
>        }
That may even be faster ( but it instantiates a char[] )
Here's the code of BitConverter.ToString()

It creates a char arrary, populates it and instantiates a string

You can reuse the code, modify it so that it doesn't insert the hyphens

But I have not done any performance comparison ( BitConverter, BitConverter Modified NoHyphen, two classes I posted)
I may in the future -

( As to calling GetHexValue() : I would favour bit shifts and bit ands instead of division and modulo )
       // Converts an array of bytes into a String.
        public static String ToString (byte[] value, int startIndex, int length) { 
            if (value == null) {
                throw new ArgumentNullException("byteArray");
            }
 
            if (startIndex < 0 || startIndex >= value.Length && startIndex > 0) {  // Don't throw for a 0 length array.
                throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_StartIndex")); 
            } 

            if (length < 0) { 
                throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
            }

            if (startIndex > value.Length - length) { 
                throw new ArgumentException(Environment.GetResourceString("Arg_ArrayPlusOffTooSmall"));
            } 
            Contract.EndContractBlock(); 

            if (length == 0) { 
                return string.Empty;
            }

            if (length > (Int32.MaxValue / 3)) { 
                // (Int32.MaxValue / 3) == 715,827,882 Bytes == 699 MB
                throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_LengthTooLarge", (Int32.MaxValue / 3))); 
            } 

            int chArrayLength = length * 3; 

            char[] chArray = new char[chArrayLength];
            int i = 0;
            int index = startIndex; 
            for (i = 0; i < chArrayLength; i += 3) {
                byte b = value[index++]; 
                chArray[i]= GetHexValue(b/16); 
                chArray[i+1] = GetHexValue(b%16);
                chArray[i+2] = '-'; 
            }

            // We don't need the last '-' character
            return new String(chArray, 0, chArray.Length - 1); 
        }





 private static char GetHexValue(int i) {
            Contract.Assert( i >=0 && i <16, "i is out of range."); 
            if (i<10) {
                return (char)(i + '0');
            }
 
            return (char)(i - 10 + 'A');
        } 
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform