473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Big-endian long <--> byte[8] conversion

Short version: is it possible to control the endian-ness of BitConverter? -
or are there any /framework/ methods like
BigEndianBitConverter.GetBytes(long) and .ToInt64()?
(I am not after equivalent code; I already have that)

===

Background:
I'm dealing with Sql-Server timestamp fields [which are equivalent to the
binary(8) sql data-type]; in some circumstances (e.g. OpenXml) they need to
be representated using the bigint sql-datatype (equiv. to the c# long); the
sql cast between bigint and binary(8) is (as far as I can tell) big-endian.

Now; when I get the timestamp (as a byte[]) from the database in C#, it also
appears to be big-endian; I want to convert this to a long, so that I can
use it in an xml structure for OpenXml later on (for a complex block
update); for efficiency (and simplicity) I had hoped to use BitConverter,
but this (on my environment, at least) is little-endian.

Obviously it is fairly simple to code this translation (have done it; it
works fine - this is not the problem), but is there a framework way of doing
this conversion? I realise that simple byte-shifts and binary-ORs work very
efficiently, but the code would be used a fair bit, and often the framework
wrappers can cheat and do things even more frugally... (and yes: this is
probably "premature optimisation", but the question stands...)

Or am I being too hopeful?

Marc
Aug 1 '06 #1
5 3977
Marc Gravell wrote:
Short version: is it possible to control the endian-ness of BitConverter? -
or are there any /framework/ methods like
BigEndianBitConverter.GetBytes(long) and .ToInt64()?
(I am not after equivalent code; I already have that)
No, I don't believe there's anything in the framework.

I have a library to provide equivalent code in a nicely wrapped up way,
however :)

http://www.pobox.com/~skeet/csharp/miscutil

Jon

Aug 1 '06 #2
Kinda confirms what I suspected, cheers Jon.

Handy wrapper, btw; if I need any more conversions I may well use those
classes. For now, my "long only" version is sufficient and less complex -
the only real coding difference is that I am using a "--" foreach loop (in
what you have called FromBytes) to make the buffer lookup nominally quicker,
plus you reminded me to use "unchecked", which will help... I'm also
currently checking BitConverter.IsLittleEndian to decide whether to use the
CPU's native translation, which is possibly overkill (and I'd need an IA64
to properly test it...).

Cheers,

Marc
Aug 1 '06 #3
Marc Gravell wrote:
Kinda confirms what I suspected, cheers Jon.

Handy wrapper, btw; if I need any more conversions I may well use those
classes. For now, my "long only" version is sufficient and less complex -
the only real coding difference is that I am using a "--" foreach loop (in
what you have called FromBytes) to make the buffer lookup nominally quicker,
plus you reminded me to use "unchecked", which will help... I'm also
currently checking BitConverter.IsLittleEndian to decide whether to use the
CPU's native translation, which is possibly overkill (and I'd need an IA64
to properly test it...).
Well, you don't need an IA64 to compare the speed of your own routine
to the speed of BitConverter if you just ignore the results. I strongly
suspect you'll find that it's actually not significantly faster to use
BitConverter - in which case make the code as simple as possible and
ignore BitConverter entirely :)

Jon

Aug 1 '06 #4
By my tests, 46.9ms/million (BitConverter) vs 93.8ms/million (my code minus
BitConverter check/usage) on the same h/w (release build, no IDE); it isn't
exactly order-of-magnitude bigger, so you are right: keep it simple and
ditch BitConverter for this particular case.

Interesting little exercise (or maybe I should get out more ;-p)

Marc
Aug 1 '06 #5
Marc Gravell wrote:
By my tests, 46.9ms/million (BitConverter) vs 93.8ms/million (my code minus
BitConverter check/usage) on the same h/w (release build, no IDE); it isn't
exactly order-of-magnitude bigger, so you are right: keep it simple and
ditch BitConverter for this particular case.

Interesting little exercise (or maybe I should get out more ;-p)
One thing it may be interesting to try: turn your array into a fixed
pointer, and index it that way. You may find that accounts for the
difference - not that it's that huge anyway, as you've said.

Jon

Aug 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
55
by: Sharon | last post by:
hi all, I generate binary text file (in Windows using Dev-C++) Is the generated binary file using BigEndian? Are there any method to generate binary file using LittleEndian? Because I want the...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem...
10
by: Old Wolf | last post by:
Consider the following program: #include <stdio.h> int main(void) { /* using malloc to eliminate alignment worries */ unsigned long *p = malloc( sizeof *p ); if ( p && sizeof(long) ==...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
1
by: DR | last post by:
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...
105
by: Keith Thompson | last post by:
pereges <Broli00@gmail.comwrites: These types already have perfectly good names already. Why give them new ones? If you must rename them for some reason, use typedefs, not macros. --
21
by: REH | last post by:
It it permissible to use the constructor style cast with primitives such as "unsigned long"? One of my compilers accepts this syntax, the other does not. The failing one chokes on the fact that the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.