SQLBinary to compare to Int

  • Thread starter Thread starter sjoshi
  • Start date Start date
S

sjoshi

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
 
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 != ctsarray) // this if() is completely
unnecessary. It's easier/faster to just always to the assigment.
mtsarray = ctsarray;
}
PutMTSBackIntoDB(mtsarray);
}
 
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
 
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
 
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
 
Sunit Joshi said:
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.
 
Back
Top