Reputation: 1364
I need to write the system to check either this user is valid or not by numbers issued for customer (NIC).
The data was given in forms of bytes with the total is 255 kilobytes, and I need to convert from bytes to bit. If 255 kb convert to bit, it will become 2,088,960.
Let say we take F9 as first byte, when convert to binary it will become 11111001.
NIC | Binary
1 = 1
2 = 1
3 = 1
4 = 1
5 = 1
6 = 0
7 = 0
8 = 1
0 = False
1 = True
For example,
NIC for this customer is number 3, so the value of bit is 1. So for another customer, let say his NIC is 6 then the value of bit is 0.
If the value of bit is 0, so this customer is valid. But if the value is 1, so this customer not valid.
So far what has done
var reader = com.ExecuteScalar() as byte[];
if (reader != null)
{
//From database to bytes array
list_bytes = reader;
//From bytes array to bit array
BitArray bits = new BitArray(list_bytes);
for (int a = 1; a <= NIC; a++)
{
//Debug purpose
if(a == NIC)
lblStatus.Text = Convert.ToBoolean(bits[a - 1]).ToString();
}
}
The problem is, let say I enter the NIC is 1 then it return = True. When I enter NIC is 2 then it return False but the answer should be True.
NIC 1 = True = 1
NIC 2 = False = 0
NIC 3 = False = 0
NIC 4 = True = 1
NIC 5 = True = 1
NIC 6 = True = 1
NIC 7 = True = 1
NIC 8 = True = 1
The binary is 10011111 and convert to byte is 0x9F, but the the data should be 0xF9.
I was Google for few hour ago and no one answer fit with my problems. Kindly let me know if this question not clear.
Upvotes: 0
Views: 723
Reputation: 10863
This actually appears to be a rather simple problem covered up by too much code. One suggestion: forget about converting anything to bits. There is only pain that way.
As I understand it you have an array of bytes and each byte is a customer. The customer has a NIC and you simply want to check whether a particular bit is set in the customer's byte according to the NIC. The code looks something like this.
Byte customer_byte = list_bytes[customer_id];
Boolean isvalid = test_bit(customer_byte, customer_nic);
The NIC bits are numbered from 1 to 8, where 1 means 0x80 and 8 means 0x01. The test_bit function to do that could be written:
Boolean test_bit(Byte value, Int bitno) {
Byte mask = POW(2, 8 - bitno); // POW is the power function
return (value AND bitno) NEQ 0; // AND is the bitwise operator
}
I leave writing the actual code (and fixing my misunderstandings) as an exercise to the reader.
Upvotes: 0
Reputation: 3133
The binary is 10011111 and convert to byte is 0x9F, but the the data should be 0xF9.
The problem is that the first value you push on the array will be first value you are getting out of the array again (logic). But in your case, you don't want this, you want to start at the last value and work your way up to the first value, then it will work and ultimately return 0xF9
.
for (int a = NIC; a >= 0; a--)
{
// Loop will run from NIC until it reaches 0. (can be 1 to, depending if your collection is zero-based)
}
Upvotes: 1