Creating Bit Array in Powershell/C# from integers - c#

I'm trying to reverse engineer a game database and have come to a roadblock.
I can load all the tables/fields/records , however I'm stuck when it comes to converting the record values to hex or bits
the values (in game) are as follows: (15 bits) 192 - (10 bits) 20 - (5 bits) 19 - (5 bits) 2
In the db file , it shows 00 C0 - 00 0A - A6 - 00
This is strange , because only the first value (00 C0) is the same in Hex (192)
The other values are different , I'm guessing this is because they are not full bytes (10 and 5 bits respectively) so it must be using a bit array.
This guess is further proven when I change the final value from 2 , to 31. The last 2 values in the db are changed, and the hex string becomes 00 C0 - 00 0A - E6 - 07
So what's the best way to get these 4 integers in to a bit array in PowerShell so I can try to determine what's going on here ? If it is not obvious to any more experienced programmers what is at play here. If required I could also use C# however I'm less experienced.
Thanks

I am not sure what you want to achieve. 5bits words are literally odd.
It could be that there is no clear conversion here but something like a hash. Anyways, you could technically count from 0 to 2^35 - 1 and poke that in your game and lookup the result in your database.
Let me give you a few conversion methods:
To bit array:
$Bits =
[convert]::ToString(192, 2).PadLeft(15, '0') +
[convert]::ToString( 20, 2).PadLeft(10, '0') +
[convert]::ToString( 19, 2).PadLeft( 5, '0') +
[convert]::ToString( 2, 2).PadLeft( 5, '0')
$Bits
00000001100000000000101001001100010
And back:
if ($Bits -Match '(.{15})(.{10})(.{5})(.{5})') {
$Matches[1..4].Foreach{ [convert]::ToByte($_, 2) }
}
192
20
19
2
To Int64:
$Int64 = [convert]::ToInt64($Bits, 2)
$Int64
201347682
To bytes:
$Bytes = [BitConverter]::GetBytes($Int64)
[System.BitConverter]::ToString($Bytes)
62-52-00-0C-00-00-00-00
Note that the bytes list is reverse order:
[convert]::ToString(0x62, 2)
1100010

Related

ASN.1 object identifier values greater than 128

So I understand the way the values are encoded when their values are less than 127. However, after reading https://learn.microsoft.com/en-us/windows/desktop/seccertenroll/about-object-identifier, i still don't understand how values greater than 128 are encoded. For example:
1.3.6.1.4.1.311.21.20
gets encoded into:
2b 06 01 04 01 82 37 15 14
How is 311 encoded into 82 37? When you convert 8237 to decimal, you get 33335. I don't really understand this part exactly.
This article should help you understand the encoding.
7-bit encoding is used and 8th bit (MSB) used to indicate end of encoding.
82 37 is in binary 10000010 00110111. You can see that it is composed of 2 parts. The first part has MSB set to 1 but the second (also the last in this case) has the MSB set to 0 indicating end of encoding. If you decoded that (ignore MSB from first part) it would be 0000 0010 = 256 (2*128) + 0011 0111 = 55 (2^0 + 2^1 + 2^2 + 2^4 + 2^5) = 311

How to create a byte array that will have a BCC (Block Check Character) of a given value (0)?

I am trying to create a byte array that has a certain BCC (Block Check Character) result (=0).
The array will have the preamble of:
32 02 31 1F 31 1E 32 1F T E S T :
32 02 31 1F 31 1E 32 1F 54 45 53 54 3A 20
With a variable text message (msg) in the middle:
T e s t 2
54 65 73 74 32
Followed by a postamble of:
1E 37 1F 33 03
The BCC I get for this string is: 0x11
Here's the algorithm that returns this value (C++):
unsigned char bcc=0;
int index = block.Find(0x03); //ETX
for (int i=0; i<= index;i++)
bcc ^= block[i];
return bcc;
I'm trying to come up with a method of finding a middle message section that will result in the BCC of 0.
I am currently using trial and error, but I'm pretty sure there's a better way to do this - I just haven't come up with one that works. I've taken a swing at a tool that replicates the BCC method in use above (in C#) and that disagrees with the results I get (sigh).
You can set the checksum to zero by replacing any single character with the character xor ed with the current checksum.
For example, change test2 to test#
0x32(#) = 0x23(2)^0x11
You may need to take care to avoid certain special characters (it looks like 0x03 is significant in some way, and often 0x00 should also be avoided in case strings are using null termination). For example, if you wanted to make a character into 0x03 you might instead prefer to add two characters whose xor is 0x03, such as 'a' and 'b'

How do I detect frequency of mp3's in .NET?

I want to create a very simple piece of software in C# .NET that I can pass a folder's path to and detect all files with a frequency of below a given threshold. Any pointers on how I would do this?
You have to read mp3 files. To do that you have to find specifications for them.
Generally mp3 file is wrapped into ID3 tag, so that you have to read it, find its length and skip it. Let's take ID3v2.3 for example:
ID3v2/file identifier "ID3"
ID3v2 version $03 00
ID3v2 flags %abc00000
ID3v2 size 4 * %0xxxxxxx
so bytes 6,7,8,9 store header length in big-endian form. Here is sample of some file:
0 1 2 3 4 5 6 7 8 9 A B C D E F
49 44 33 03 00 00 00 00 07 76 54 43 4f 4e 00 00
07 76 - is the size. You need to shift left first byte so that actual size is 3F6. Then add 10 (A) to get the offset = 400. This is address of start of mp3 header.
Then you take description of mp3 header:
bits are: AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM, we need FF , sampling frequency and convert t to actual frequency:
bits MPEG1 MPEG2 MPEG2.5
00 44100 22050 11025
01 48000 24000 12000
10 32000 16000 8000
11 reserv. reserv. reserv.
You can use UltraID3Lib to get mp3 metadata (bitrate, frequency)
Check value of frequency bits in a file. There is some info about mp3 format.

Binary to Ascii and back again

I'm trying to interface with a hardware device via the serial port. When I use software like Portmon to see the messages they look like this:
42 21 21 21 21 41 45 21 26 21 29 21 26 59 5F 41 30 21 2B 21 27
42 21 21 21 21 41 47 21 27 21 28 21 27 59 5D 41 32 21 2A 21 28
When I run them thru a hex to ascii converter the commands don't make sense. Are these messages in fact something different than hex? My hope was to see the messages the device is passing and emulate them using c#. What can I do to find out exactly what the messages are?
Does the hardware device specify a protocol? Just because it's a serial port connection it doesn't mean that it has to be ASCII/Readable english Text. It could as well be just a sequence of bytes where for example 42 is a command and 21212121 is data to that command. Could be an initialization sequence or whatever.
At the end of the day, all you work with is a series of bytes. The meaning of them can be found in a protocol specification or if you don't have one, you need to manually look at each command. Issue a command to the device, capture the input, issue another command.
Look for patterns. Common Initialization? What could be the commands? What data gets passed?
Yes, it's tedious, but reverse engineering is rarely easy.
The ASCII for the Hex is this:
B!!!!AE!&!)!&Y_A0!+!'
B!!!!AG!'!(!'Y]A2!*!(
That does look like some sort of protocol to me, with some Initialization Sequence (B!!!!) and commands (AE and AG), but that's just guessing.
The decive is sending data to the computer. All digital data has the form of ones and zeroes, such as 10101001010110010... . Most often one combines groups of eight such bits (binary digits) into bytes, so all data consists of bytes. One byte can thus represent any of the 2^8 values 0 to 2^8 - 1 = 255, or, in hexadecimal notation, any of the numbers 0x00 to 0xFF.
Sometimes the bytes represent a string of alphanumerical (and other) characters, often ASCII encoded. This data format assigns a character to each value from 0 to 127. But all data is not ASCII-encoded characters.
For instance, if the device is a light-intensity sensor, then each byte could give the light intensity as a number between 0 (pitch-black) and 255 (as bright as it gets). Or, the data could be a bitmap image. Then the data would start with a couple of well-defined structures (namely this and this) specifying the colour depth (number of bits per pixel, i.e. more or less the number of colours), the width, the height, and the compression of the bitmap. Then the pixel data would begin. Typically the bytes would go BBGGRRBBGGRRBBGGRR where the first BB is the blue intensity of the first pixel, the first GG is the green intensity of the first pixel, the first RR is the red intensity of the first pixel, the second BB is the blue intensity of the second pixel, and so on.
In fact the data could mean anything. Whay kind of device is it? Does it have an open specification?

Write all bits in C#

How can i write all bits of a file using c#?
For example writing 0 to all bits
Please provide me with a sample
I'm not sure why you'd want to do this, but this will overwrite a file with data that is the same length but contains byte values of zero:
File.WriteAllBytes(filePath, new byte[new FileInfo(filePath).Length]);
Definitely has the foul stench of homework to it.
Hint - Think why someone might want to do this. Just deleting the file and replacing with a file of 0s of the correct length might not be what you're after.
Have a look at System.IO.FileInfo; you'll need to open a writable stream for the file you're interested in and then write however many bytes (with value 0 in your example) to it as there are in the file already (which you can ascertain via FileInfo.Length). Be sure to dispose of the stream once you're done with it – using constructs are useful for this purpose.
Consider using the BinaryWriter available in the .NET framework
using(BinaryWriter binWriter =
new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
binWriter.Write("Hello world");
}
When you say write all bits to a file I'll assume you mean bits as in nyble, bit, byte. That's just writing an integer to a file. You can't have a 4 bit file as far as I know so the smallest denomination will be a byte.
You probably don't want to be responsible for serializing yourself, so your easiest option would be to use the BinaryReader and BinaryWriter classes, and then manipulate the bits inside your C#.
The BinaryWriter class uses a 4 byte integer as minimum however. For example
writer.Write( 1 ); // 01
writer.Write( 10 ); // 0a
writer.Write( 100 ); // 64
writer.Write( 1000 ); // 3e8
writer.Write( 10000 ); // 2710
//writer.Write( 123456789 ); // 75BCD15
is written to file as
01 00 00 00 0a 00 00 00 64 00 00 00 e8 03 00 00 10 27 00 00 15 cd 5b 07
read into a byte and then test against >= powers of 2 to get each of the bits in that byte

Categories

Resources