Reputation: 249
I have two hex strings of 8 digits. I need to apply and operation for these two hex string, then apply right shift to 7 bits and get the decimal value. I have tried converting Hex strings to byte array of length 4 (8 *2 = 32 bits = 4 bytes) and did & operation to individual bytes in same order, saved the result to another byre array of length 4. How to do bit shifting to this byte array?
Ex : data1 in hex: 0x40003019,
data1 in bits: 0100-0000 0000-0000 0011-0000 0001-1001,
data1 in bytes: 64 0 48 25,
data2 in hex: 0x00FFFF80,
data2 in bits : 0000-0000 1111-1111 1111-1111 1000-0000,
data2 in bytes : 0 255 255 128
AND operation between data1Bytes , data2Bytes which gives output : bytearray1[0,0,48,0] (bits for these 0000-0000 0000-0000 0011-0000 0000-0000 and decimal value is 12,288).
Till this step all my conversions and calculations are working as expected. now I need to right shift 7 bits of this end result which should give 0000-0000 0000-0000 0000-0000 0110-0000( decimal value of 96).
1)I have tried converting byte array to int and apply right shift
var res = BitConverter.ToInt32(bytearray1, 0);
var shift = res >> 7;
but res = 3145728(which should be 12,228) and shift = 24,576(which should be 96)
2)I have tired converting bytearray1[0,0,48,0] into BitArray but bits in resultant BitArray are in reverse order
var bitArray = new BitArray(bytearray1);
bitArray[0]...bitArray[19] = false, bitArray[20] = bitArray[21] = true , bitArray[22]...bitArray[31] = false.
bitArray[0] -----------[31] : 0000 0000 0000 0000 0000 1100 0000 0000,
bit shifting this result wrong value. Please help me with this, what I am missing?
Upvotes: 1
Views: 1846
Reputation: 109567
I'm not sure why this isn't working for you, but the obvious approach works when I try it.
Firstly assume you have the two hex numbers in uint
values:
uint data1 = 0x40003019;
uint data2 = 0x00FFFF80;
Now just AND them together and then right shift the result:
uint anded = data1 & data2;
uint result = anded >> 7; // Gives 96 as requested.
This gives a result of 96
.
If your input is a string of the form string str = "0x40003019";
you can convert it to a uint
like so:
uint data1 = uint.Parse(str.Substring(2), NumberStyles.HexNumber);
The str.SubString(2)
is just to strip off the "0x"
prefix. This is unnecessary if the input string does not have a "0x"
prefix.
Upvotes: 0
Reputation: 34421
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication33
{
class Program
{
static void Main(string[] args)
{
List<string> digits = new List<string>() {
"0000","0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110","1111"
};
string input = "0100-0000 0000-0000 0011-0000 0001-1001";
byte[] bytes = input.Split(new char[] { '-', ' ' }).Select(x => (byte)digits.IndexOf(x)).ToArray();
ulong number = BitConverter.ToUInt64(bytes,0);
Console.WriteLine(number);
Console.ReadLine();
}
}
}
Upvotes: -2