I need to process a binary resource (contents of a binary file). The file contains shorts. I can't figure how to cast the result of accessing that resource to a short array though:
short[] indexTable = Properties.Resources.IndexTable;
doesn't work; I can only use
byte[] indexTable = Properties.Resources.IndexTable;
Using Array.Copy wouldn't work since it would convert each byte from the array returned by accessing the resource to a short.
How do I solve this problem please (other than manually converting the byte array)? Is there a way to tell C# that the resource is of type short[] rather than byte[]?
I had even tried to edit the project's resx file and change the resources' data types to System.UInt16, but then I got the error message that now constructor could be found for them.
Using VS 2010 Express and .NET 4.0.
You should use BinaryReader:
using (var reader = new BinaryReader(new MemoryStream(Properties.Resources.IndexTable)))
{
var firstValue = reader.ReadInt16();
...
}
Buffer.BlockCopy()
byte[] srcTable = Properties.Resources.IndexTable;
short[] destTable = new short[srcTable.Length / 2];
Buffer.BlockCopy(srcTable, 0, destTable, 0, srcTable.Length);
Here's how if you prefer a 'Do it yourself' style (however, I personally prefer #Joel Rondeau's method)
byte[] bytes = Properties.Resources.IndexTable;
short[] shorts = new short[bytes.Length/2];
for( int i = 0; i < bytes.Length; i += 2 )
{
// the order of these depends on your endianess
// big
shorts[i/2] = (short)(( bytes[i] << 8 ) | (bytes[i+1] ));
// little
// shorts[i/2] = (short)(( bytes[i+1] << 8 ) | (bytes[i] ));
}
Related
I'm passing int[] array that hold image, later I want to convert it to bytes[] and save the image to local path. However, I notice that the bytePic[] length is equal to int[] arrPic just the values are missing. There is a screenshot below:
Below is the entire function:
public string ChangeMaterialPicture(int[] arrPic, int materialId,string defaultPath)
{
var material = _warehouseRepository.GetMaterialById(materialId);
if(material is not null)
{
// Convert the Array to Bytes
byte[] bytePic = new byte[arrPic.Length];
for(var i = 0; i < arrPic.Length; i++)
{
AddByteToArray(bytePic, Convert.ToByte(arrPic[i]));
}
// Convert the Bytes to IMG
string filename = Guid.NewGuid().ToString() + "_.png";
System.IO.File.WriteAllBytes(#$"{defaultPath}\materials\{material.VendorId}\{filename}", bytePic);
// Update the Image
material.Picture = filename;
_warehouseRepository.UpdateMaterial(material);
return material.Picture;
}
else
{
return String.Empty;
}
}
public byte[] AddByteToArray(byte[] bArray, byte newByte)
{
byte[] newArray = new byte[bArray.Length + 1];
bArray.CopyTo(newArray, 1);
newArray[0] = newByte;
return newArray;
}
You are creating the new array newArray in AddByteToArray and return it. But at the call site you are never using this returned value and the bytePic array remains unchanged.
The code in AddByteToArray makes no sense. Why create a new array when the intention was to insert one byte into an existing array? What you need to do is to cast the int into byte. Simply write:
byte[] bytePic = new byte[arrPic.Length];
for (int i = 0; i < arrPic.Length; i++)
{
bytePic[i] = (byte)arrPic[i];
}
And delete the method AddByteToArray.
This assumes that every value in the int array is in the range 0 to 255 and therefore fits into one byte.
There are different ways to do this. With LINQ you could also write:
byte[] bytePic = arrPic.Select(i => (byte)i).ToArray();
I would assume your original array uses a int to represent a full RGBA-pixel, since 32bit per pixel mono images are very rare in my experience. And if you do have such an image, you probably want to be more intelligent in how you do this conversion. The only time just casting int to bytes would be a good idea is if you are sure only the lower 8 bits are used, but if that is the case, why are you using an int-array in the first place.
If you actually have RGBA-pixles you do not want to convert individual int-values to bytes, but rather convert a single int value to 4 bytes. And this is not that difficult to do, you just need to use the right methods. The old school options is to use Buffer.BlockCopy.
Example:
byte[] bytePic = new byte[arrPic.Length * 4];
Buffer.BlockCopy(arrPic, 0, bytePic, 0, bytePic.Length);
But if your write-method accepts a span you might want to just convert your array to a span and cast this to the right type, avoiding the copy.
Assuming my WAV file contains 16 bit PCM, How can I read wav file as double array:
using (WaveFileReader reader = new WaveFileReader("myfile.wav"))
{
Assert.AreEqual(16, reader.WaveFormat.BitsPerSample, "Only works with 16 bit audio");
byte[] bytesBuffer = new byte[reader.Length];
int read = reader.Read(bytesBuffer, 0, buffer.Length);
// HOW TO GET AS double ARRAY
}
Just use the ToSampleProvider extension method on your WaveFileReader, and the Read method will take a float[] with the samples converted to floating point. Alternatively use AudioFileReader instead of WaveFileReader and again you can access a version of the Read method that fills a float[]
16-bit PCM is an signed-integer encoding. Presuming you want doubles between 0 and 1, you simply read each sample as an 16-bit signed integer, and then divide by (double)32768.0;
var floatSamples = new double[read/2];
for(int sampleIndex = 0; sampleIndex < read/2; sampleIndex++) {
var intSampleValue = BitConverter.ToInt16(bytesBuffer, sampleIndex*2);
floatSamples[sampleIndex] = intSampleValue/32768.0;
}
Note that stereo channels are interleaved (left sample, right sample, left sample, right sample).
There's some good info on the format here: http://blog.bjornroche.com/2013/05/the-abcs-of-pcm-uncompressed-digital.html
This question already has answers here:
How do you remove and add bytes from a byte array in C#
(2 answers)
Closed 7 years ago.
So I have a byte array, and I need to remove the first 5 elements from it. Anyway, I looked online and I couldn't find anything that suited what I was looking for. So I made this, and it is horribly slow, in essence, unusable.
private byte[] fR(byte[] tb)
{
string b = "";
byte[] m = new byte[tb.Length - 5];
for (int a = 5; a < tb.Length; a++)
{
b = b + " " + tb.GetValue(a);
}
b = b.Remove(0, 1);
string[] rd = Regex.Split(b, " ");
for (int c = 0; c < rd.Length; c++)
{
byte curr = Convert.ToByte(rd[c]);
m.SetValue(curr, c);
}
return m;
}
What I am asking is, is if there is a way to make this faster/improve. Or another method in which I can remove the first 5 elements from a byte array.
Much easier and quicker:
byte[] src = ...;
byte[] dst = new byte[src.Length - 5];
Array.Copy(src, 5, dst, 0, dst.Length);
This is as fast as you'll be able to get.
If you're using C# 8, you can use ranges to copy a slice of the array very concisely:
byte[] src = ...;
byte[] dst = src[5..];
LINQ used in other answers, being a bit easier to understand, is what I'd do 90% of the time. But, LINQ has its own overheads especially for simple problems like this, and I'd not use it if performance is critical.
Your code is slow because you're packing the byte array into a string and then unpacking it. Get rid of the string manipulation and it will be fast.
You can use Linq:
tb.Skip(5).ToArray();
What about
tb.Skip(5).ToArray()
?
I am attempting to make a file comparison program, one of the features I would like to implement is to calculate the Similarity and Difference of the two files chosen. I would like this comparison to be fast (if possible) on large files. I am not sure what method should be used, but in the end I want a percentage.
Refer to this gif to get a visual idea.
You probably want to something like similarity as seen by a binary diff utility -- not a dumb byte-by-byte comparison. But hey, just for fun...
unsafe static long DumbDifference(string file1Path, string file2Path)
{
// completely untested! also, add some using()s here.
// also, map views in chunks if you plan to use it on large files.
MemoryMappedFile file1 = MemoryMappedFile.CreateFromFile(
file1Path, System.IO.FileMode.Open,
null, 0, MemoryMappedFileAccess.Read);
MemoryMappedFile file2 = MemoryMappedFile.CreateFromFile(
file2Path, System.IO.FileMode.Open,
null, 0, MemoryMappedFileAccess.Read);
MemoryMappedViewAccessor view1 = file1.CreateViewAccessor();
MemoryMappedViewAccessor view2 = file2.CreateViewAccessor();
long length1 = checked((long)view1.SafeMemoryMappedViewHandle.ByteLength);
long length2 = checked((long)view2.SafeMemoryMappedViewHandle.ByteLength);
long minLength = Math.Min(length1, length2);
byte* ptr1 = null, ptr2 = null;
view1.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr1);
view2.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr2);
ulong differences = (ulong)Math.Abs(length1 - length2);
for (long i = 0; i < minLength; ++i)
{
// if you expect your files to be pretty similar,
// you could optimize this by comparing long-sized chunks.
differences += ptr1[i] != ptr2[i] ? 1u : 0u;
}
return checked((long)differences);
}
Too bad .NET has no SIMD support built in.
If you can use linq this should be no problem.
var results = your1stEnumerable.Intersect(your2ndEnumerable);
I have a very painful library which, at the moment, is accepting a C# string as a way to get arrays of data; apparently, this makes marshalling for pinvokes easier.
So how do I make a ushort array into a string by bytes? I've tried:
int i;
String theOutData = "";
ushort[] theImageData = inImageData.DataArray;
//this is as slow like molasses in January
for (i = 0; i < theImageData.Length; i++) {
byte[] theBytes = System.BitConverter.GetBytes(theImageData[i]);
theOutData += String.Format("{0:d}{1:d}", theBytes[0], theBytes[1]);
}
I can do it this way, but it doesn't finish in anything remotely close to a sane amount of time.
What should I do here? Go unsafe? Go through some kind of IntPtr intermediate?
If it were a char* in C++, this would be significantly easier...
edit: the function call is
DataElement.SetByteValue(string inArray, VL Length);
where VL is a 'Value Length', a DICOM type, and the function itself is generated as a wrapper to a C++ library by SWIG. It seems that the representation chosen is string, because that can cross managed/unmanaged boundaries relatively easily, but throughout the C++ code in the project (this is GDCM), the char* is simply used as a byte buffer. So, when you want to set your image buffer pointer, in C++ it's fairly simple, but in C#, I'm stuck with this weird problem.
This is hackeration, and I know that probably the best thing is to make the SWIG library work right. I really don't know how to do that, and would rather a quick workaround on the C# side, if such exists.
P/Invoke can actually handle what you're after most of the time using StringBuilder to create writable buffers, for example see pinvoke.net on GetWindowText and related functions.
However, that aside, with data as ushort, I assume that it is encoded in UTF-16LE. If that is the case you can use Encoding.Unicode.GetString(), but that will exepect a byte array rather than a ushort array. To turn your ushorts into bytes, you can allocate a separate byte array and use Buffer.BlockCopy, something like this:
ushort[] data = new ushort[10];
for (int i = 0; i < data.Length; ++i)
data[i] = (char) ('A' + i);
string asString;
byte[] asBytes = new byte[data.Length * sizeof(ushort)];
Buffer.BlockCopy(data, 0, asBytes, 0, asBytes.Length);
asString = Encoding.Unicode.GetString(asBytes);
However, if unsafe code is OK, you have another option. Get the start of the array as a ushort*, and hard-cast it to char*, and then pass it to the string constructor, like so:
string asString;
unsafe
{
fixed (ushort *dataPtr = &data[0])
asString = new string((char *) dataPtr, 0, data.Length);
}
One thing you can do is switch from using a string to a stringBuilder it will help performance tremendously.
If you are willing to use unsafe code you can use pointers and implement the your c# code just like your c++. Or you could write a small c++\cli dll that implements this functionality.
Look into the Buffer class:
ushort[] theImageData = inImageData.DataArray;
byte[] buf = new byte[Buffer.ByteLength(theImageData)]; // 2 bytes per short
Buffer.BlockCopy(theImageData, 0, buf, 0, Buffer.ByteLength(theImageData));
string theOutData = System.Text.Encoding.ASCII.GetString(buf);
Just FYI, this has been fixed in later revision (gdcm 2.0.10). Look here:
http://gdcm.sourceforge.net/
-> http://apps.sourceforge.net/mediawiki/gdcm/index.php?title=GDCM_Release_2.0
I don't like this much, but it seems to work given the following assumptions:
1. Each ushort is an ASCII char between 0 and 127
2. (Ok, I guess there is just one assumption)
ushort[] data = inData; // The ushort array source
Byte[] bytes = new Byte[data.Length]; // Assumption - only need one byte per ushort
int i = 0;
foreach(ushort x in data) {
byte[] tmp = System.BitConverter.GetBytes(x);
bytes[i++] = tmp[0];
// Note: not using tmp[1] as all characters in 0 < x < 127 use one byte.
}
String str = Encoding.ASCII.GetString(bytes);
I'm sure there are better ways to do this, but it's all I could come up with quickly.
You can avoid unnecessary copying this way :
public static class Helpers
{
public static string ConvertToString(this ushort[] uSpan)
{
byte[] bytes = new byte[sizeof(ushort) * uSpan.Length];
for (int i = 0; i < uSpan.Length; i++)
{
Unsafe.As<byte, ushort>(ref bytes[i * 2]) = uSpan[i];
}
return Encoding.Unicode.GetString(bytes);
}
}