It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Here is my code:
long max = pcmU16.Length;
long index = 0;
fixed (ushort* srcFix = pcmU16)
{
ushort* src = srcFix;
next:
*src = 32768;
src++;
index++;
if (index != max)
{
goto next;
}
}
Like you see, it is writing 2 bytes at once. How to use ulong type and write 8 bytes at once? pcmU16 is ushort[] array.
You just coerce it:
ulong* src = (ulong*)srcFix;
Things to watch, though:
your max needs to be divided by 4, else you're going out of range
you need to handle any stray values - for example, say you have 10 ushort values (max was 10 initially); that is 2 sets of ulong (4 each), and a final 2 ushort; the usual divisor/remainder stuff
As a final note, you might find the index syntax more convenient, i.e.
for(int i = 0 ; i < max ; i++) {
src[i] = ...
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I got stuck with my code, tried anything on this side and many other things Google showed.
To the problem:
I try to convert some code-snips from C# to phyton, but on this special point i got stuck.
public static long decode(string data, int size, int offset = 0)
{
long value = 0;
for (int i = 0; i < size; ++i) {
value <<= 6;
value |= (long)data[offset + i] - 0x30;
}
return value;
}
The String Data could be something like 1Dh. Based on this I convert each char to the hex-equivalent: 0x31, 0x44, 0x68 and subtract 0x30; so I get 0x1, 0x14, 0x38;
In the next step I have to convert to the binary equivalent 000001, 010100, 111000 and merge this to
000001010100111000. From this I want to get the integer meaning, in this case 5432.
Is there a possibility to do this in a smart and easy way in python?
It's actually pretty easy, and the translation is pretty straight forward. You can continue to use your bit shifting. The only change is the syntax of for-loop and using ord() to get the integer value from a character.
def decode(data, size, offset=0):
value = 0
for ch in data[offset:size]:
value <<= 6
value |= ord(ch) - 0x30
return value
Running this in the interpreter, I get 5432:
>>> decode("1Dh", 3)
5432
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
There are many hashing methods but I want to compose bit hash with 8096 bits long. Is it possible to achieve this?
For example when I enter "House" I should get a string like:
"0101010001010101..." (8096 bits)
How can i achieve this (using C# 4.0 is ok)?
If you wonder why I need such thing, I need it for comparing Signature Files & Vector Space Model.
For fast non-cryptographic hashes you can examine the FNV family. By careful and suitable variation you should be able to construct an 8096-bit hash that is reasonably fast.
If speed is not a primary concern but simplicity and quality are then you can simply use a variant of MD5 to make a non-cryptographic hash.
Hash(x) = MD5(0 || x) || MD5(1 || x) ... MD5(62 || x) || MD5(63 || x)<32>, where "||" is the concatenation operation and only the low order 32 bits of the final hash are used, will give you an 8096 bit hash.
EDIT
Here is small code sample showing the MD5 concept:
using System;
using System.Security.Cryptography;
using System.Text;
namespace Hash8096
{
class MainClass
{
public static byte [] H8096(byte [] x) {
byte [] Result = new byte[8096 / 8];
byte [] Xplus1 = new byte[x.Length + 1];
x.CopyTo(Xplus1, 1);
int ResultOffset = 0;
int AmountLeft = Result.Length;
for (int i=0; i<64; i++) {
// do MD5(i || x)
var md5 = MD5.Create();
Xplus1[0] = (byte) i;
var hash = md5.ComputeHash(Xplus1);
int NumToCopy = Math.Min(hash.Length, AmountLeft);
Array.Copy(hash, 0, Result, ResultOffset,NumToCopy);
ResultOffset += NumToCopy;
AmountLeft -= NumToCopy;
}
return Result;
}
public static void Main (string[] args)
{
byte [] x = Encoding.UTF8.GetBytes("Hello World!");
byte [] MonsterHash = H8096(x);
Console.WriteLine ("Monster hash in hex follows:");
Console.WriteLine(BitConverter.ToString(MonsterHash));
}
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
So I'm reading the memory of another program for the purpose of reverse engineering. I'm using a function from kernel32.dll.
The function looks like this:
ReadProcessMemory(Handle, new IntPtr(Address), buffer, BytesToRead, out ptrBytesRead);
I'm using it to read memory bit by bit, like so:
public static int ReadInt32(IntPtr Handle, long Address)
{
return BitConverter.ToInt32(ReadBytes(Handle, Address, 4), 0);
}
Then calling ReadInt32 using a programs handle and the address I'm looking to read, adding the base address of the program as it is not static.
What I'd like to do is read a whole chunk of memory (up to 1300 records, each record has a memory step of 0xB0).
I'm not sure exactly what the best way is to go about this. I've looked into it and it looks like it is possible to do something where I take the whole thing and basically dump it into a structure of my own. The problem I'm having is that I don't quite know what I need to actually do. I can see it in my mind, but can't put pen to paper on it.
I'll expand to show that it is in fact a 2d array, looking like this in structure:
int OFFSET_CREATURE_ID = 0;
int OFFSET_CREATURE_TYPE = 3;
int OFFSET_CREATURE_NAME = 4;
int OFFSET_CREATURE_Z = 36;
int OFFSET_CREATURE_Y = 40;
int OFFSET_CREATURE_X = 44;
int OFFSET_CREATURE_IS_WALKING = 80;
int OFFSET_CREATURE_DIRECTION = 84;
int OFFSET_CREATURE_OUTFIT = 100;
int OFFSET_CREATURE_OUTFIT_HEAD = 104;
int OFFSET_CREATURE_OUTFIT_BODY = 108;
int OFFSET_CREATURE_OUTFIT_LEGS = 112;
int OFFSET_CREATURE_OUTFIT_FEET = 116;
int OFFSET_CREATURE_OUTFIT_ADDON = 120;
int OFFSET_CREATURE_LIGHT = 124;
int OFFSET_CREATURE_LIGHT_COLOR = 128;
int OFFSET_CREATURE_HP_BAR = 140;
int OFFSET_CREATURE_WALK_SPEED = 144;
int OFFSET_CREATURE_IS_VISIBLE = 148;
int OFFSET_CREATURE_SKULL = 152;
int OFFSET_CREATURE_PARTY = 156;
int OFFSET_CREATURE_WARICON = 164;
int OFFSET_CREATURE_ISBLOCKING = 168;
Each of those offsets needs to be assigned to a different element in my structure. Some of them are bool, some int, and some string. I've got the struct. I can't guarentee that the length will consume the entire "step" for each offset, though when I read these values I think I need to declare a new instance of the struct each time (I will be reading them each 500ms or so, maybe more! The total read time for 250 records is approx 50ms, which is what I expect to read! I do need to be able to do up to 1300 though).
Please, no confusing code, just an explanation of what I should do would suffice. I struggle a lot when I'm working with this amount of code, so unless someone is going to put together a class which will read all of this, with a structure to store it (so I can convert it to work with mine), I'd appreciate literally minimal code.
For the primitive types if you know the offset you can simply take the bits from offset n (beginning of data) to n+1 and use a BitConverter to convert to the type. Because you can't specify an end with methods such as ToInt32(byte[] value, int startIndex) you'll have to copy the bytes into a new array.
Lets just assume every thing is an int for this example. I'm also going to assume all of the offsets are in an array. Bytes is the memblock you got from ReadInt32
int[] values = new int[Bytes.Length/4];
byte[] current = new byte[4];
BitConverter bc = new BitConvert();
for (i = 0; i < Bytes.Length/4 -1; i++)
{
Buffer(Bytes, i(4), current, 0, 4);
values[i] = bc.ToInt32(current, 0);
}
This is a huge simplification but it should be enough to get you moving in the right direction. You may not even want to use a loop (maybe you just hard code all of the BitConverter instructions, that is how you do serialization when reading from a db in C/C++) If you have many types that will also make things simpler. Either way the basic concept will be to use a bit converter to convert sections of the bytes you read from the other program into their respective properties in your data structure.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to write a chip8 emulator in C#. It will be necessary to simulate in software the operations that take place in hardware on the real chip.
There is an opcode that requires detecting a whether a borrow occurred during the subtraction of two binary numbers.
Byte1 - Byte2
Does anyone have any ideas as to how I can use C# to tell if a borrow occurred or not?
After searching in Wikipedia for the mysterious opcode you didn't mention, and looking for some implementations. I can conclude that the opcodes 8XY5 and 8XY7 (where X and Y are register identifiers) will do substractions.
For 8XY5 the register X will be set to the value of the register X minus the value of the register Y. And the register F will be set to 1 if the value of the register Y is greater or equal to the value of the register X (0 otherwise).
For 8XY7 the register X will be set to the value of the register Y minus the value of the register X. And the register F will be set to 1 if the value of the register X is greater or equal to the value of the register Y (0 otherwise).
This is the "pseudo"-code for 8XY5:
x = opcode[1]
y = opcode[2]
if (register[x] >= register[y])
register[0xF] = 1
else
register[0xF] = 0
result = register[x] - register[y]
if result < 0
result += 256
register[x] = result
and this is the "pseudo"-code for 8XY7:
x = opcode[1]
y = opcode[2]
if (register[y] >= register[x])
register[0xF] = 1
else
register[0xF] = 0
result = register[y] - register[x]
if result < 0
result += 256
register[x] = result
And here is a C# implementation:
const byte B_0 = 0x0;
const byte B_1 = 0x1;
const byte B_F = 0xF;
static void Main()
{
byte[] registers = new byte[16];
registers[0x1] = 255;
registers[0x2] = 127;
Opcode8XY5(registers, 1, 2);
Console.WriteLine(registers[0x1]);
Console.WriteLine(registers[0x2]);
Console.WriteLine(registers[0xF]);
Opcode8XY7(registers, 1, 2);
Console.WriteLine(registers[0x1]);
Console.WriteLine(registers[0x2]);
Console.WriteLine(registers[0xF]);
Console.ReadLine();
}
static void Opcode8XY5(byte[] registers, byte x, byte y)
{
registers[B_F] = registers[x] >= registers[y] ? B_1 : B_0;
registers[x] = (byte)(registers[x] - registers[y]);
}
static void Opcode8XY7(byte[] registers, byte x, byte y)
{
registers[B_F] = registers[y] >= registers[x] ? B_1 : B_0;
registers[x] = (byte)(registers[y] - registers[x]);
}
Check this implementation for reference.
You will need to compare each bit of the bytes... If the second byte bit is set but not the first, you have a borrow condition:
static void Main(string[] args) {
byte b1 = byte.Parse(args[0]);
byte b2 = byte.Parse(args[1]);
bool borrow = false;
for (int mask = 0x01; mask <= 0x80; mask <<= 1) {
if ((b2 & mask) > (b1 & mask)) {
borrow = true;
}
}
Console.WriteLine(Convert.ToString(b1, 2).PadLeft(8, '0'));
Console.WriteLine(Convert.ToString(b2, 2).PadLeft(8, '0'));
Console.WriteLine("borrowed: {0}", borrow);
}
There may be a very clever boolean logic that gives you the answer, but I cannot come up with it now.
You need to do the math with wider type provided that the typecast was not sign-propagating. Then check if the result has the 8th bit set to 1. If it is set then borrowing had place. Then save a result as byte.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
In one of the interview, i got asked the following question but i am not sure what is that, Please help me to understand about the question
Use C# to turn an 4th and 7th bits of a said byte myFlag in which write most bit is first bit.
Each byte has 8 bits, which are either on (1) or off (0). So you'd want to turn on the specified bits, using the bitwise operators.
how about ORing with 72 (01001000) ?
myFlag = myFlag | 72;
Assuming my interpretation is correct you are looking to use bit-wise operators to solve the problem. In order to ensure a particular bit is on use | with the bits set that you want set.
myFlag = myFlag | 0b00010010
Or equivalently
myFlag |= 18
If it helps to see the string of bytes, then you can use the Convert class to convert integers to bit strings and reverse to help visualise the effects of the bitwise OR. Below is a sample that creates a toggledOnFlag that has the bits toggled on. You could OR with the other bit string to switch them off.
var toggleBitsOn = "01001000";
var toggleBitsOff = "10110111";
var toggle = Convert.ToInt32(toggleBitsOn, 2);
var toggledOnFlag = myFlag | toggle;
Console.WriteLine(Convert.ToString(toggledOnFlag, 2));
You didn't specify how to declare "myFlag", but this should be what you're looking for.
[Flags]
enum BitFlags : byte
{
One = ( 1 << 0 ),
Two = ( 1 << 1 ),
Three = ( 1 << 2 ),
Four = ( 1 << 3 ),
Five = ( 1 << 4 ),
Six = ( 1 << 5 ),
Seven = ( 1 << 6 ),
Eight = ( 1 << 7 )
}
static void Main(string[] args)
{
BitFlags myFlag = BitFlags.Four | BitFlags.Seven;
Console.WriteLine( Convert.ToString( ( byte ) myFlag, 2 ) );
}
Edit: Updated for C# clarity and used "Flags" attribute, which is probably more along the lines of what the interviewer was looking for.