bitconverter

Equivalent of BitConverter.ToUInt16 of C# for Java 7 [duplicate]

左心房为你撑大大i 提交于 2020-07-10 10:27:44
问题 This question already has an answer here : Equivalent of BitConverter.ToUInt16 of C# for Java (1 answer) Closed 9 days ago . My question is about Java. I need a method that returns an unsigned 16-bit integer converted from two bytes at the specified position in a byte array. In other words I need the equivalent of the method BitConverter.ToUInt16 of C# for Java that works with Java 7. In C# using System; using System.IO; using System.Linq; using System.Collections.Generic; namespace CSharp

Equivalent of BitConverter.ToUInt16 of C# for Java

☆樱花仙子☆ 提交于 2020-07-03 13:26:41
问题 My question is about Java What is the equivalent of BitConverter.ToUInt16 for Java? I am trying to translate this method of C#: public static implicit operator ushort(HashlinkReader p) { ushort tmp = BitConverter.ToUInt16(p.Data.ToArray(), p.Position); p.Position += 2; return tmp; } to Java: public static int convertToInt(HashlinkReader p) { byte[] byteArray = new byte[p.Data.size()]; for (int index = 0; index < p.Data.size(); index++) { byteArray[index] = p.Data.get(index); } int tmp =

Equivalent of BitConverter.ToUInt16 of C# for Java

一世执手 提交于 2020-07-03 13:25:48
问题 My question is about Java What is the equivalent of BitConverter.ToUInt16 for Java? I am trying to translate this method of C#: public static implicit operator ushort(HashlinkReader p) { ushort tmp = BitConverter.ToUInt16(p.Data.ToArray(), p.Position); p.Position += 2; return tmp; } to Java: public static int convertToInt(HashlinkReader p) { byte[] byteArray = new byte[p.Data.size()]; for (int index = 0; index < p.Data.size(); index++) { byteArray[index] = p.Data.get(index); } int tmp =

Why is BitConverter slower than doing the bitwise operations directly?

冷暖自知 提交于 2020-01-16 19:18:12
问题 I recently did some profiling on some code and found that the largest CPU usage was being consumed by calls to BitConverter such as: return BitConverter.ToInt16(new byte[] { byte1, byte2 }); when switching to something like: return (short)(byte1 << 8 | byte2); I noticed a huge improvement in performance. My question is why is using BitConverter so much slower? I would have assumed that BitConverter was essentially doing the same kind of bit shifting internally. 回答1: The call to BitConverter

C# generics: cast generic type to value type

泄露秘密 提交于 2020-01-12 13:49:06
问题 I have a generic class which saves value for the specified type T. The value can be an int, uint, double or float. Now I want to get the bytes of the value to encode it into an specific protocol. Therefore I want to use the method BitConverter.GetBytes() but unfortunately Bitconverter does not support generic types or undefined objects. That is why I want to cast the value and call the specific overload of GetBytes(). My Question: How can I cast a generic value to int, double or float? This

BitConverter VS ToString for Hex

≯℡__Kan透↙ 提交于 2020-01-03 17:16:13
问题 Just wondering if someone could explain why the two following lines of code return "different" results? What causes the reversed values? Is this something to do with endianness? int.MaxValue.ToString("X") //Result: 7FFFFFFF BitConverter.ToString(BitConverter.GetBytes(int.MaxValue)) //Result: FF-FF-FF-7F 回答1: int.MaxValue.ToString("X") outputs 7FFFFFFF , that is, the number 2147483647 as a whole . On the other hand, BitConverter.GetBytes returns an array of bytes representing 2147483647 in

Convert pointer to loop option in C#

て烟熏妆下的殇ゞ 提交于 2019-12-31 03:49:06
问题 How would I convert this into a loop and not to use the pointer. byte[] InputBuffer = new byte[8]; unsafe { fixed (byte* pInputBuffer = InputBuffer) { ((long*)pInputBuffer)[0] = value; } } I am trying to use the code from this page: query string parameter obfuscation 回答1: There's no looping here. You could use BitConverter.GetBytes instead of the unsafe type-punning cast. byte[] InputBuffer = BitConverter.GetBytes(value); replaces all six original lines of code. 来源: https://stackoverflow.com

How to get sound data sample value in c#

邮差的信 提交于 2019-12-29 09:14:09
问题 I need to get the sample values of sound data of a WAV file so that by using those sample values i need to get the amplitude values of that sound data in every second. Important: Is there any way to get audio data sample values using Naudio library or wmp library? I am getting the sample values in this way: byte[] data = File.ReadAllBytes(File_textBox.Text); var samples=new int[data.Length]; int x = 0; for (int i = 44; i <data.Length; i += 2) { samples[x] = BitConverter.ToInt16(data, i); x++;

Is there a better way to detect endianness in .NET than BitConverter.IsLittleEndian?

北慕城南 提交于 2019-12-25 11:14:06
问题 It would be nice if the .NET framework just gave functions/methods from the BitConverter class that just explicitly returned an array of bytes in the proper requested endianness. I've done some functions like this in other code, but is there a shorter more direct way? (efficiency is key since this concept is used a TON in various crypto and password derivation contexts, including PBKDF2, Skein, HMAC, BLAKE2, AES and others) // convert an unsigned int into an array of bytes BIG ENDIEN // per

Is there a less painful way to GetBytes for a buffer not starting at 0?

走远了吗. 提交于 2019-12-23 12:24:23
问题 I am having to deal with raw bytes in a project and I need to basically do something like this byte[] ToBytes(){ byte[] buffer=new byte[somelength]; byte[] tmp; tmp=BitConverter.GetBytes(SomeShort); buffer[0]=tmp[0]; buffer[1]=tmp[1]; tmp=BitConverter.GetBytes(SomeOtherShort); buffer[2]=tmp[0]; buffer[3]=tmp[1]; } I feel like this is so wrong yet I can't find any better way of doing it. Is there an easier way? 回答1: BinaryWriter is very efficient: byte[] ToBytes() { var ms = new MemoryStream