Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

mySqlBytes.buffer is getting converted to BigEndian even though both SQL server 2005 and the CLR function are on the same machine which shows BitConverter.IsLittleEndian == true

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DR - 04 Jan 2008 23:24 GMT
mySqlBytes.buffer is getting converted to BigEndian even though both SQL
server 2005 and the CLR function are on the same machine which shows
BitConverter.IsLittleEndian == true

in tsql: select * from dbo.MY_CLR_FUNCTION(cast(1024 as binary(4)))

public static int MY_CLR_FUNCTION(SqlBytes mySqlBytes)

in debugger binaryData.buffer now shows bigendian!!
 [0] 0 byte
 [1] 0 byte
 [2] 4 byte
 [3] 0 byte

the data stays littleendian if the integer is hard coded in C#:

           int myint;
           myint = 1024;
           byte[] b = new byte[4];
           b = BitConverter.GetBytes(myint);

in debugger binaryData.buffer shows littleendian
 [0] 0 byte
 [1] 4 byte
 [2] 0 byte
 [3] 0 byte

in all cases BitConverter.IsLittleEndian shows true, so why is the integer
hard coded in tsql getting converted to big endian when it arrives in a clr
function?
Stephany Young - 05 Jan 2008 02:36 GMT
The point is that it is not being converted to any-endian.

binary(4) is nothing more that a buffer of bytes and cast(... as binary(4))
is certainly NOT analogous to BitConverter.GetBytes(int).

If you want it correctly then pass the int (intact) to the function and use
BitConverter inside the function.

> mySqlBytes.buffer is getting converted to BigEndian even though both SQL
> server 2005 and the CLR function are on the same machine which shows
[quoted text clipped - 26 lines]
> hard coded in tsql getting converted to big endian when it arrives in a
> clr function?
DR - 05 Jan 2008 03:27 GMT
do you have some documentation to back this up? so far all i can do is test
this to verify that tsql's cast is giving me data that comes into clr as big
endian. any documentation on this behavior in msdn?

> The point is that it is not being converted to any-endian.
>
[quoted text clipped - 34 lines]
>> integer hard coded in tsql getting converted to big endian when it
>> arrives in a clr function?
Stephany Young - 05 Jan 2008 07:41 GMT
I'll try and put it another way. It is NOT big-endian! It is no-endian!

The cast makes no assumption about the source data and 'gives' you the bytes
from left to right without otherwise massaging them in any way.

You do not need documentation to support this. This is what you are
observing.

It is the BitConverter class that 'knows' to return the bytes for an int
reversed (big-endian).

> do you have some documentation to back this up? so far all i can do is
> test this to verify that tsql's cast is giving me data that comes into clr
[quoted text clipped - 38 lines]
>>> integer hard coded in tsql getting converted to big endian when it
>>> arrives in a clr function?
Ben Voigt [C++ MVP] - 07 Jan 2008 23:45 GMT
> I'll try and put it another way. It is NOT big-endian! It is no-endian!
>
> The cast makes no assumption about the source data and 'gives' you the
> bytes
> from left to right without otherwise massaging them in any way.

The behavior the OP claims to observe is that the cast, not BitConverter, is
reversing the bytes.

> You do not need documentation to support this. This is what you are
> observing.

Here's what the documentation
http://msdn2.microsoft.com/en-us/library/ms187928.aspx says:

SQL Server guarantees that only roundtrip conversions, conversions that
convert a data type from its original data type and back again, will yield
the same values from version to version.
     Note:
     Do not try to construct binary values and then convert them to a data
type of the numeric data type category. SQL Server does not guarantee that
the result of a decimal or numeric data type conversion to binary will be
the same between versions of SQL Server.

> It is the BitConverter class that 'knows' to return the bytes for an int
> reversed (big-endian).
[quoted text clipped - 43 lines]
>>>> integer hard coded in tsql getting converted to big endian when it
>>>> arrives in a clr function?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.