bitconverter

Converting raw byte data to float[]

限于喜欢 提交于 2019-12-05 18:50:17
I have this code for converting a byte[] to float[] . public float[] ConvertByteToFloat(byte[] array) { float[] floatArr = new float[array.Length / sizeof(float)]; int index = 0; for (int i = 0; i < floatArr.Length; i++) { floatArr[i] = BitConverter.ToSingle(array, index); index += sizeof(float); } return floatArr; } Problem is, I usually get a NaN result! Why should this be? I checked if there is data in the byte[] and the data seems to be fine. If it helps, an example of the values are: new byte[] { 231, 255, 235, 255, } But this returns NaN (Not a Number) after conversion to float. What

How to convert last 4 bytes in an array to an integer?

泄露秘密 提交于 2019-12-05 10:28:15
If I have an Uint8Array array in JavaScript, how would I get the last four bytes and then convert that to an int? Using C# I would do something like this: int count = BitConverter.ToInt32(array, array.Length - 4); Is there an inequivalent way to do this using JavaScript? Access the underlying ArrayBuffer and create a new TypedArray with a slice of its bytes: var u8 = new Uint8Array([1,2,3,4,5,6]); // original array var u32bytes = u8.buffer.slice(-4); // last four bytes as a new `ArrayBuffer` var uint = new Uint32Array(u32bytes)[0]; If the TypedArray does not cover the entire buffer, you need

Byte[] to ASCII

你。 提交于 2019-12-03 10:26:53
问题 I received the contents of a text file returned in binary values: Byte[] buf = new Byte[size]; stream = File.InputStream; stream.Read(buf, 0, size); How can I convert this to ASCII? 回答1: Use: System.Text.Encoding.ASCII.GetString(buf); 回答2: You can use: System.Text.Encoding.ASCII.GetString(buf); But sometimes you will get a weird number instead of the string you want. In that case, your original string may have some hexadecimal character when you see it. If it's the case, you may want to try

Byte[] to ASCII

假装没事ソ 提交于 2019-12-03 00:55:18
I received the contents of a text file returned in binary values: Byte[] buf = new Byte[size]; stream = File.InputStream; stream.Read(buf, 0, size); How can I convert this to ASCII? Jalal Said Use: System.Text.Encoding.ASCII.GetString(buf); Patrick Desjardins You can use: System.Text.Encoding.ASCII.GetString(buf); But sometimes you will get a weird number instead of the string you want. In that case, your original string may have some hexadecimal character when you see it. If it's the case, you may want to try this: System.Text.Encoding.UTF8.GetString(buf); Or as a last resort: System.Text

Convert pointer to loop option in C#

ⅰ亾dé卋堺 提交于 2019-12-02 06:54:42
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 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/questions/4882378/convert-pointer-to-loop-option-in-c-sharp

How to convert unsigned integer to signed integer without OverflowException

笑着哭i 提交于 2019-12-01 03:10:06
I would like to be able to convert a high-valued unsigned-integer (a value that uses the highest-order bit) to a signed-integer. In this case, I don't care that the value is higher than the maximum value of the signed integer type. I just want it to convert to whatever the bit-values represent as a signed-integer. In other words, I would expect it to result in a negative number. However, with VB.NET, the CType operation doesn't work that way (or any of the other conversion functions like CShort and CInteger ). When you try to convert an unsigned value that is higher than the desired signed

How to convert unsigned integer to signed integer without OverflowException

梦想与她 提交于 2019-11-30 23:13:20
问题 I would like to be able to convert a high-valued unsigned-integer (a value that uses the highest-order bit) to a signed-integer. In this case, I don't care that the value is higher than the maximum value of the signed integer type. I just want it to convert to whatever the bit-values represent as a signed-integer. In other words, I would expect it to result in a negative number. However, with VB.NET, the CType operation doesn't work that way (or any of the other conversion functions like

How to get sound data sample value in c#

心已入冬 提交于 2019-11-29 15:41:59
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++; } But I am getting negative values more like (-326260). so is this right or wrong? I mean can a sample

Converting hex to string in C?

戏子无情 提交于 2019-11-29 07:14:12
Hello I am using digi dynamic c. I am trying to convert this in to string char readingreg[4]; readingreg[0] = 4a; readingreg[1] = aa; readingreg[2] = aa; readingreg[3] = a0; Currently when I do printf statements it has to be like this: printf("This is element 0: %x\n", readingreg[0]); But I want this in string so I can use printf statement like this printf("This is element 0: %s\n", readingreg[0]); I am essentialy sending the readingreg array over TCP/IP Port, for which I need to have it as string. I cant seem to be able to convert it into string. Thanks for your help. Also if someone can tell

Convert binary string to binary or decimal value

孤街浪徒 提交于 2019-11-27 01:01:58
Is there any function to convert binary string into binary or decimal value? If I have a binary string 000101 , what should I do to convert it into 5 ? Here is what you can try: binStr <- "00000001001100110000010110110111" # 20121015 (binNum <- 00000001001100110000010110110111) # 20121015 [1] 1.0011e+24 binVec <- c(1,0,1,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1) # 2670721 shortBin <- 10011010010 # 1234 BinToDec <- function(x) sum(2^(which(rev(unlist(strsplit(as.character(x), "")) == 1))-1)) BinToDec(binStr) [1] 20121015 BinToDec(binNum) [1] 576528 BinToDec(binVec) [1] 2670721 BinToDec(shortBin)