Equivalent C code in C#. (uint8_t *) cast? - c#

I am trying to convert these C piece of code into C#. But I've stumble in some parts that I don't understand exactly what is happening, so I am unable to translate it.
void foo(uint64_t *output, uint64_t *input, uint32_t Length){
uint64_t st[25];
memcpy(st, input, Length);
((uint8_t *)st)[Length] = 0x01;
memset(((uint8_t *)st) + Length + 1, 0x00, 128 - Length - 1);
for(int i = 16; i < 25; ++i) st[i] = 0x00UL;
// Last bit of padding
st[16] = 0x8000000000000000UL;
bar(st);
memcpy(output, st, 200);
}
More specifically in the ((uint8_t *)st)[Length] = 0x01; part. I cannot understand the cast/pointer in this line. Why is the * there for? If somebody could explain what is happening, I would be grateful.
What I got so far in C#:
private void foo(ref ulong[] output, ref ulong[] input, uint Length)
{
ulong[] st = new ulong[25];
//memcpy(st, input, Length);
Buffer.BlockCopy(input, 0, st, 0, (int)Length);
// Help in these line please:
//((uint8_t *)st)[Length] = 0x01;
// Still don't know what to do here too:
//memset(((byte)st) + Length + 1, 0x00, 128 - Length - 1);
for (int i = 16; i < 25; ++i)
{
st[i] = 0x00U;
}
// Last bit of padding
st[16] = 0x8000000000000000U;
bar(st);
//memcpy(output, st, 200);
Buffer.BlockCopy(st, 0, output, 0, 200);
}
Thank you.

What your function does is rather primitive memory manipulation:
void foo(uint64_t *output, uint64_t *input, uint32_t Length)
{
uint64_t st[25];
Your function takes a pointer to 16 elements of uint64_t and prepares a local buffer (st) to store a copy of them.
memcpy(st, input, Length);
((uint8_t *)st)[Length] = 0x01;
memset(((uint8_t *)st) + Length + 1, 0x00, 128 - Length - 1);
Make a copy of the input array into st but insert a byte with value 1 at an offset of Length bytes from the start.
for(int i = 16; i < 25; ++i) st[i] = 0x00UL;
The remaining 8 elements of the buffer are cleared to 0. Writing st[16] is pointless as it will be changed again by the next line...
// Last bit of padding
st[16] = 0x8000000000000000UL;
Now we have 16*8 bytes from the input buffer, 1 extra byte with value 1 inserted somewhere and the remaining elements are filled with 1<<63 and lots of 0 bytes.
bar(st);
memcpy(output, st, 200);
}
Do something we have no clue about and copy the resulting 25 elements into the output buffer

Related

Appending Array on top of Array to specified length

I've made a function to append an array on top of itself to a specified length. The only issue I'm having is when the array is an odd number, say 9, and the length I'm trying to get it to, for example 20, it would output with 2 zeros at the end. I'm sure there's a way to have the array fill in every byte.
Current Function:
public static byte[] AppendToLen(byte[] input, int length)
{
byte[] output = new byte[length];
if (length <= input.Length) return null;
for (int i = 0; i < length / input.Length; i++)
Array.Copy(input, 0, output, input.Length * i, input.Length);
return output;
}
Block of bytes
byte[] Block = new byte[0x10] { 0x02, 0x03, 0xFF, 0x04, 0x61, 0x37, 0x5f, 0xe8, 0x19, 0x70, 0xa2, 0x77, 0x8c, 0x94, 0x89, 0xb4 };
An example is:
foreach(byte bit in AppendToLen(Block, 56)) {
Console.WriteLine(bit.ToString("X2"));
}
Ouput: 0203FF0461375FE81970A2778C9489B40203FF0461375FE81970A2778C9489B40203FF0461375FE81970A2778C9489B40000000000000000
In the example above you are only iterating twice because the length / input.Length term rounds down to 2. Remember that this is integer division. Also, remember not to walk off the end of your array. The snippet below does what you want.
public static byte[] AppendToLen(byte[] input, int length)
{
byte[] output = new byte[length];
if (length <= input.Length) return null;
// Just use offset here since that's what you care about
for (int offset = 0; offset < length; offset += input.Length)
// Copy as much of the input array as possible to the output,
// starting at this iteration's offset
Array.Copy(input, 0, output, offset, Math.Min(length - offset, input.Length));
return output;
}
The problem is that you are not assigning any value to the remainder bytes. In your example 56 % 16 = 8, so you have 8 bytes left at the end after your loop without initialize. Now, the way you want to fill those I don't know, maybe with the first 8 bytes in your input array. If so, you would need to add an extra check after the loop to see if the remainder is > 0, and copy those 8 (remainder) bytes to the last positions of your array.

Convert ushort[] into byte[] and back

I have a ushort array that needs converting into a byte array to be transferred over a network.
Once it gets to its destination, I need then reconvert it back into the same ushort array it was to being with.
Ushort Array
Is an array that is of Length 217,088 (1D array of broken down image 512 by 424). It's stored as 16-bit unsigned integers. Each element is 2 bytes.
Byte Array
It needs to be converted into a byte array for network purposes. As each ushort element is worth 2 bytes, I assume the byte array Length needs to be 217,088 * 2?
In terms of converting, and then 'unconverting' correctly, I am unsure on how to do that.
This is for a Unity3D project that is in C#. Could someone point me in the right direction?
Thanks.
You're looking for BlockCopy:
https://msdn.microsoft.com/en-us/library/system.buffer.blockcopy(v=vs.110).aspx
and yes, short as well as ushort is 2 bytes long; and that's why corresponding byte array should be two times longer than initial short one.
Direct (byte to short):
byte[] source = new byte[] { 5, 6 };
short[] target = new short[source.Length / 2];
Buffer.BlockCopy(source, 0, target, 0, source.Length);
Reverse:
short[] source = new short[] {7, 8};
byte[] target = new byte[source.Length * 2];
Buffer.BlockCopy(source, 0, target, 0, source.Length * 2);
using offsets (the second and the fourth parameters of Buffer.BlockCopy) you can have 1D array being broken down (as you've put it):
// it's unclear for me what is the "broken down 1d array", so
// let it be an array of array (say 512 lines, each of 424 items)
ushort[][] image = ...;
// data - sum up all the lengths (512 * 424) and * 2 (bytes)
byte[] data = new byte[image.Sum(line => line.Length) * 2];
int offset = 0;
for (int i = 0; i < image.Length; ++i) {
int count = image[i].Length * 2;
Buffer.BlockCopy(image[i], offset, data, offset, count);
offset += count;
}

Convert and Append string into existing byte array

I am working on converting and existing C# project over to Java/Android. I am looking for the Java equivalent to UTF8Encoding.GetBytes(String, Int32, Int32, Byte[], Int32). Take a look at the C# code below, how do I add the string packet into the data byte array? I have looked at the String.getBytes() method but it is not the same.
int length = packet.Length;
byte[] data = new byte[6 + length + 1];
data[0] = (byte)VAR1;
data[1] = 1;
**Encoding.UTF8.GetBytes(packet, 0, length, data, 6);**
data[6 + length] = (byte)VAR2;
data[5] = (byte)(length % 256);
length /= 256;
data[4] = (byte)(length % 256);
length /= 256;
data[3] = (byte)(length % 256);
length /= 256;
data[2] = (byte)(length % 256);
Okay, given that you mean ASCII rather than UTF-8, there are two immediate options:
Intermediate byte array
byte[] encodedText = text.getBytes(StandardCharsets.US_ASCII);
System.arraycopy(encodedText, 0, data, 6, encodedText.length);
This is inefficient, but simple.
Charset directly
CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
CharBuffer charBuffer = CharBuffer.wrap(text);
ByteBuffer byteBuffer = ByteBuffer.wrap(data, 6, data.length - 6);
encoder.encode(charBuffer, byteBuffer, true);
This is possibly more efficient, but more complicated to understand.

Converting 3 bytes into signed integer in C#

I'm trying to convert 3 bytes to signed integer (Big-endian) in C#.
I've tried to use BitConverter.ToInt32 method, but my problem is what value should have the lats byte.
Can anybody suggest me how can I do it in different way?
I also need to convert 5 (or 6 or 7) bytes to signed long, is there any general rule how to do it?
Thanks in advance for any help.
As a last resort you could always shift+add yourself:
byte b1, b2, b3;
int r = b1 << 16 | b2 << 8 | b3;
Just swap b1/b2/b3 until you have the desired result.
On second thought, this will never produce negative values.
What result do you want when the msb >= 0x80 ?
Part 2, brute force sign extension:
private static int Bytes2Int(byte b1, byte b2, byte b3)
{
int r = 0;
byte b0 = 0xff;
if ((b1 & 0x80) != 0) r |= b0 << 24;
r |= b1 << 16;
r |= b2 << 8;
r |= b3;
return r;
}
I've tested this with:
byte[] bytes = BitConverter.GetBytes(p);
int r = Bytes2Int(bytes[2], bytes[1], bytes[0]);
Console.WriteLine("{0} == {1}", p, r);
for several p.
The last value should be 0 if it isn't set for a positive number, 256 for a negative.
To know what you should pass in, you can try converting it the other way:
var bytes = BitConverter.GetBytes(i);
int x = BitConverter.ToInt32(bytes, 0);
To add to the existing answers here, there's a bit of a gotcha in that Bitconverter.ToInt32() will throw an ArgumentException if the array is less than sizseof(int) (4) bytes in size;
Destination array is not long enough to copy all the items in the collection. Check array index and length.
Given an array less than sizeof(int) (4) bytes in size, you can compensate for left/right padding like so;
Right-pad
Results in positive Int32 numbers
int intByteSize = sizeof(int);
byte[] padded = new byte[intByteSize];
Array.Copy(sourceBytes, 0, padded, 0, sourceBytes.Length);
sourceBytes = padded;
Left-pad
Results in negative Int32 numbers, assuming non-zero value at byte index sourceBytes.Length - 1.
int intByteSize = sizeof(int);
byte[] padded = new byte[intByteSize];
Array.Copy(sourceBytes, 0, padded, intByteSize - sourceBytes.Length, sourceBytes.Length);
sourceBytes = padded;
Once padded, you can safely call int myValue = BitConverter.ToInt32(sourceBytes, 0);.

number formation for byte conversion

How to format the number which can be converted to Byte[] (array);
I know how to convert the result value to byte[], but the problem is how to format the intermediate number.
This is by data,
int packet = 10;
int value = 20;
long data = 02; // This will take 3 bytes [Last 3 Bytes]
i need the long value, through that i can shift and I'll fill the byte array like this
Byte[0] = 10;
Byte[1] = 20;
Byte[2] = 00;
Byte[3] = 00;
Byte[4] = 02;
Byte 2,3,4 are data
but formatting the intermediate value is the problem. How to form this
Here is the sample
long data= 683990319104; ///I referred this as intermediate value.
This is the number i receiving from built in application.
Byte[] by = new byte[5];
for (int i = 0; i < 5; i++)
{
by[i] = (byte)(data & 0xFF);
data>>= 8;
}
Here
Byte[0] = 00;
Byte[1] = 00; //Byte 0,1,2 are Data (ie data = 0)
Byte[2] = 00;
Byte[3] = 65; //Byte 3 is value (ie Value = 65)
Byte[4] = 159; // Byte 4 is packet (is Packet = 159);
This is one sample.
Currently BitConverter.GetBytes(..) is ues to receive.
Byte while sending, the parameter is long.
So i want the format to generate the number 683990319104 from
packet = 159
value = 65
data = 0
I think now its in understandable format :)
Not entirely sure what you are asking exactly, but I think you are looking for BitConverter.GetBytes(data).
The use of 3 bytes to define the long seems unusual; if it is just the 3 bytes... why a long? why not an int?
For example (note I've had to make assumptions about your byte-trimming based on your example - you won't have the full int/long range...):
static void Main() {
int packet = 10;
int value = 20;
long data = 02;
byte[] buffer = new byte[5];
WritePacket(buffer, 0, packet, value, data);
for (int i = 0; i < buffer.Length; i++)
{
Console.Write(buffer[i].ToString("x2"));
}
Console.WriteLine();
ReadPacket(buffer, 0, out packet, out value, out data);
Console.WriteLine(packet);
Console.WriteLine(value);
Console.WriteLine(data);
}
static void WritePacket(byte[] buffer, int offset, int packet,
int value, long data)
{
// note I'm trimming as per the original question
buffer[offset++] = (byte)packet;
buffer[offset++] = (byte)value;
int tmp = (int)(data); // more efficient to work with int, and
// we're going to lose the MSB anyway...
buffer[offset++] = (byte)(tmp>>2);
buffer[offset++] = (byte)(tmp>>1);
buffer[offset] = (byte)(tmp);
}
static void ReadPacket(byte[] buffer, int offset, out int packet,
out int value, out long data)
{
// note I'm trimming as per the original question
packet = buffer[offset++];
value = buffer[offset++];
data = ((int)buffer[offset++] << 2)
| ((int)buffer[offset++] << 1)
| (int)buffer[offset];
}
oooh,
Its simple
int packet = 159
int value = 65
int data = 0
long address = packet;
address = address<<8;
address = address|value;
address = address<<24;
address = address|data;
now the value of address is 683990319104 and i termed this as intermediate value. Let me know the exact term.

Categories

Resources