473,473 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQLBinary to compare to Int

I have an SQLBinary (8) field(mts) with data like 0x01F4000000000000,
0x0400000000000000, etc. Now I need to compare just the first 4 bytes
to an Int32 field (cts) and update the SQLBinary field if it's
different.

What would the be the best way to do this. Like for this data, which is
stored like this.

cts=4
mts=0x0400000000000000

I tried to get mts as byte[] and then BitConverter.ToInt32(byte[],0)
compare to cts and if different set mts=BitConverter.GetBytes(cts) but
that did not work.

Any help is appreciated.

thanks
Sunit

Nov 16 '05 #1
5 3098
<sj****@ingr.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I tried to get mts as byte[] and then BitConverter.ToInt32(byte[],0)
compare to cts and if different set mts=BitConverter.GetBytes(cts) but
that did not work.
It would be helpful if you said how that didn't work.

My suggestion would be to GetBytes mts into an array *and* GetBytes cts
into an array, and then just compare them element by element. Actually, you
might as well, just skip the comparision step, and just immediately copy the
cts[] array into the mts[] array.

int cts = 4;
byte[] ctsarray = BitConverter.GetBytes(cts);

while (....)
{
byte[] mtsarray = GetMTSfromDB();
for (int i = 0; i < 4; ++i)
{
// if (mtsarray[i] != ctsarray[i]) // this if() is completely
unnecessary. It's easier/faster to just always to the assigment.
mtsarray[i] = ctsarray[i];
}
PutMTSBackIntoDB(mtsarray);
}
I have an SQLBinary (8) field(mts) with data like 0x01F4000000000000,
0x0400000000000000, etc. Now I need to compare just the first 4 bytes
to an Int32 field (cts) and update the SQLBinary field if it's
different.

What would the be the best way to do this. Like for this data, which is
stored like this.

cts=4
mts=0x0400000000000000
Any help is appreciated.

thanks
Sunit

Nov 16 '05 #2
Thanks but won't you need to compare them using the if ?. Also I tried
this:

long mts = 0x4000000000000000;
int cts=4;
string ctsHex = string.Format(cts.ToString("x2"));

try
{
byte[] mtsBytes = BitConverter.GetBytes(mts);
byte[] ctsBytes = BitConverter.GetBytes(cts);
int ctsInt = int.Parse(ctsHex,
System.Globalization.NumberStyles.HexNumber);
int mtsInt = BitConverter.ToInt32(mtsBytes, 0);
Console.WriteLine("ctsHex: {0}, ctsInt: {1}, mtsInt: {2}", ctsHex,
ctsInt, mtsInt);
}
catch(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
And this is what I got:
ctsHex: 04, ctsInt: 4, mtsInt: 0

thanks
Sunit

Nov 16 '05 #3
<sj****@ingr.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Thanks but won't you need to compare them using the if ?. Also I tried
Why? Your goal is that the bytes in mts be the same as the bytes in
cts. Your were only copying them if they were different. Why not just save
a step, and always copy them?
string ctsHex = string.Format(cts.ToString("x2")); The string.Format is unnecessary: string ctsHex = cts.ToString("x2");
is sufficent.
int mtsInt = BitConverter.ToInt32(mtsBytes, 0);

mtsBytes is 8 bytes long. You are asking to convert it into a 4-byte
long number. It's going to take them from the right side.

I think you might be a bit confused by Big-Endian vs Little-Endian
binary number representation. Note that when a number has a representation
in memory of
12 34 56 78 9A BC DE F0
it is equal to the long value:
long mts = 0XF0DEBC9A78563412

Nov 16 '05 #4
Thanks I had realized that the "if" step was unnecessary. Regarding the
endian, how can I tell if the hex no is in Big/Little endian formats ?

If say cts=500, mts=0x01F4000000000000 is in BigEndian format, then
would I have to use a BigEndianClass.GetBytes(mts) and then use the
Int64 of BitConverter to get the value ? I'm also still interested in
how to compare cts and mts.

thanks a lot
Sunit
int mtsInt = BitConverter.ToInt32(mtsBytes, 0);

mtsBytes is 8 bytes long. You are asking to convert it into a 4-byte
long number. It's going to take them from the right side.

I think you might be a bit confused by Big-Endian vs Little-Endian
binary number representation. Note that when a number has a
representation
in memory of
12 34 56 78 9A BC DE F0
it is equal to the long value:
long mts = 0XF0DEBC9A78563412
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
"Sunit Joshi" <sj****@ingr.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
Thanks I had realized that the "if" step was unnecessary. Regarding the
endian, how can I tell if the hex no is in Big/Little endian formats ?

If say cts=500, mts=0x01F4000000000000 is in BigEndian format, then
would I have to use a BigEndianClass.GetBytes(mts) and then use the
Int64 of BitConverter to get the value ? I'm also still interested in
how to compare cts and mts.


Ok, 500 (dec) == 0x0000000001F4.
On a little endian machine, that will be stored as:
F4 01 00 00 00 00 00 00

On a big endian machine, that will be stored as:
00 00 00 00 00 00 01 F4

On a 16-bit, big endian machine, which isn't quite sure what it want to do
with numbers bigger than 16 bit, may store it as:
01 F4 00 00 00 00 00 00

On the other hand, 0x01F4000000000000 == 140,737,488,355,328,000 (dec).

So, when you say that "mts=0x01F4000000000000" it's not quite clear if you
mean that it's "01 F4 00 00 00 00 00 00" in memory (as seen in a hex dump
of the DB record), or that it's actually equal to 140 quadrillion (as seen
by printing out the final value of mts).

There is no BigEndianClass. BitConverter just copies the bytes into an
array. Once they are there you can rearrange them as you see fit.

Nov 16 '05 #6

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

Similar topics

13
by: MrCoder | last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!" Ok heres my question for you lot. Is there a faster way to compare 1 byte array to another? This is my current code //...
8
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences...
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
3
by: megan | last post by:
I can easily get my byte array into the SqlBinary Sql servr field. getting it out again is more difficult. SqlBinary myBin = myBinaryObject; // object from SQL server field. byte b= myBin ; ...
1
by: Linda | last post by:
Hi, Is there a way to do a "text" (rather than "binary") compareison with the "like" operator, without changing the global "Option Compare" setting? I don't want to risk breaking many, many...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
5
by: antani | last post by:
I need to implement a function with a argument that is a compare function. This compare function must be several for every necessity. For example , I would like a compare function to analyze...
26
by: neha_chhatre | last post by:
can anybody tell me how to compare two float values say for example t and check are two variables declared float how to compare t and check please help me as soon as possible
1
by: Lambda | last post by:
I defined a class: class inverted_index { private: std::map<std::string, std::vector<size_t index; public: std::vector<size_tintersect(const std::vector<std::string>&); };
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.