BitConvertor - Reading Values From A Byte Array
Jul 18, 2010I am reading some values from a Byte Array ,i am confused with the Offset
[Code]...
I am reading some values from a Byte Array ,i am confused with the Offset
[Code]...
A c# class's single dimension byte array contains socket level instrument sensor data as per:
public static byte[] frameData
I need to read signed and unsigned 8 bit, 16 bit and 32 bit values from a file stream which may be little-endian or big-endian (it happens to be a tiff file which carries the byte order indicator at the start).I initially started by writing my own functions to read the values and was able to do so for unsigned values. e.g.
Public Function ReadUInt32() As UInt32
Dim b(4) As Byte
input.Read(b, 0, 4)
[code]....
But then I started looking at signed values and my brain broke.As an alternative, I found the IO.BinaryReader which will let me read signed values directly but doesn't seem to have any way to indicate that the data is big-endian or little-endian.Is there a nice way of handling this? Failing that, can someone tell me how to convert multiple bytes into signed values (in both byte orders)?
Is there any way using existing .NET framework classes/methods that you can sequentially read a byte array? I dont mean just using a for loop to loop through from the beginning to the end, I mean having the kind of methods that you have when working with an IO.Stream subclass, such as ReadByte. I thought I would just be able to construct a new IO.Stream class and pass it in a byte array but it seems that Stream is just for inheriting and cannot actually be used itself. I could implement my own class that just has an internal position counter and a byte array and then each time you call ReadByte on that it advances the position and returns the byte at that index... but it seems odd to have to implement such a basic thing myself.
View 1 RepliesI'm trying to convert old code to VB in VS2005. The code I'm working on is used to store values in a string, which will then be handed to a proprietary scramble function. This function is a given in this case. I've already switched from a string to an array of bytes in my .NET code.
The old code (VBA) was using a string value to store the data, example function for Double value:
Private Function DoubleToStr(ByVal value As Double) As String
Dim Bytes(7) As Byte ' 0-7 Double is 8 bytes
Dim n As Long
CopyMemory(Bytes(0), value, 8)
DoubleToStr = ""
For n = 0 To UBound(Bytes)
DoubleToStr = DoubleToStr & Chr(Bytes(n))
Next n
End Function
The code basically copies the memory contents (using API call CopyMemory) of the memory (held by the double variable) to the memory held by a byte array. It then reads the byte array and converts it into a string. Workarounds could be to convert a bool or long to a string, though that would be expensive in terms of memory/storage eg. 1234564787 would be 10 bytes in a string where as a long is only 4 bytes (factor 2.5 increase).
How to retain the original precision? Converting it to a string would result in endless numbers. Another problem (at least I think it could be) is that in VBA the variables are rather simple, but in .NET they are all objects, so how to retrieve the actual memory location of the value? let alone garbage collection moving stuff around. How to get those variables stored in my byte array?
I need to convert hex values aa 01 00 17 f4 2f (as contained in byte array FrameData) to display in a label control as AA:01:00:17:F4:2F
HexDump(FrameData(2)) converts the aa (which has been converted to Decimal 170) into 31 37 30 !
similarly I need to convert hex data ('02 00' contained in byte array FrameData) as
0x0200."0x" & FrameData(1) + FrameData(2) displays the data as 0x20. How would I convert the '02 00' hex data to display as 0x0200?
How would I copy/convert a string containing an ascii representation of hex values in to a byte array containing the actual hex values? For example, I have a variable containing the hex values delimited by spaces (I can change the delimiter):
[Code]...
I have a device with upon serial communication , it send the data as HEX values , (eg, C020042ABD0F91A103E400F929EBC) . I use the following code to get data from the serial port.
Dim fStream As New FileStream(sFileName, FileMode.CreateNew) ' creates new file
Dim bw As New BinaryWriter(fStream)
System.Threading.Thread.Sleep(1500)
ComTd.Read(data, 1, bCount)
[code]....
I have a device with upon serial communication, it send the data as HEX values, (e.g., C020042ABD0F91A103E400F929EBC). I use the following code to get data from the serial port.
Dim fStream As New FileStream(sFileName, FileMode.CreateNew) ' creates new file
Dim bw As New BinaryWriter(fStream)
System.Threading.Thread.Sleep(1500)
ComTd.Read(data, 1, bCount)
Dim bCount As Integer = 4119 ' it is the size of the chunk not block size
Dim data(bCount) As Byte
[Code] .....
I'm creating an emulator for a device to simulate wave forms. My challenge is that when I try to convert the output of the following code, I can't convert the Math.Sin's double output into byte values that lend themselves to the byte array format required by an existing graphing control as listed at bottom below. I''m not sure how to handle this when the sin function creates negative values but the byte values will need to be positive and basically 'shifted up' so that anything over 127 is negative. (0 - 255) unsigned. BitConverter.GetBytes has been suggested but THIS is the result of that attempt.
[Code]....
I have a button which reads a comma separated value file and loads it into a richtextbox. Each line contains comma separated chars.
Once the file is loaded in the richtextbox, i want to be able to select specific values from each line and store them into an array.
For example:
Line1: a,b,c,d,e
I want to be able to place only the values "b" and "c" into an array.
I was thinking something like creating a menu for the richtextbox which will be triggered on right click, then highlight the values I want, press right click and select 'place these values in an array'.
it doesn't have to be the way i proposed it, but it definitely must use a richtextbox.
I am trying to Convert a data field stored as IMAGE ( SQL Server 2000) using Java to a byte array using VB.NET Java uses signed numbers for a Byte array where as VB dosent. Can somone point me to how I can covert java byte array to VB byte array?
View 2 RepliesObjective: Combine byte arrays, read specific sections, and then cut byte array at specified point.
Private Sub DataArrival(ByRef aBot As xyzSocket, ByRef theData As Byte())
Dim RDLength As Int32
If aBot.ReceivedData Is Nothing Then[code]....
I'm not sure if I have this right to begin with because I can't test it without having it completed.
how to upload file using fileupload control into sql database. I have class where i created insert function and getting info frm the .aspx.vb file
Class File
========
Imports System
Imports System.Data
Imports System.Data.SqlClient
[coe]....
I have 2 byte arrays. I want to merge these two byte array into 1 byte array.Usually, I just create a new byte array with length = byte array #1 + byte array #2. Then copy byte array #1 and #2 to the new byte array.do I have more efficient way to merge 2 byte array using VB.NET and .Net 4?
View 2 RepliesI would like to create a function so that I can pass a string and it will return me the binary value, I will use this later in other parts of the script but I am getting an error that I don't understand.
Private Function ConvertToMD5(ByVal OldPassword As String) As Byte
Dim NewPassword As String = ""
'The string we wish to encrypt
[code]....
On the "Return hashedDataBytes I get "Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'"
I have 9 1-dimensional arrays of bytes and 1 of them is empty, I want to make the empty one equal to the others put together like you would a string:
Dim bla As String = "bla" & "bla" & "bla"
'now bla = "blablabla"
but instead:
[Code]......
How do I read a raw byte array from any file, and write that byte array back into a new file?
View 3 RepliesHow could you convert a IntPrt to a Byte Array (Byte()) ?
View 19 RepliesI have a byte array that I convert into a string like so Dim byt As Byte() = New Byte(255) {} s = New String(Encoding.ASCII.GetChars(byte))My question is when I look at the string in a debuger its clearly a normal string but when I compare it to what I know its supposed to be it doesnt equal. So i did a quick check and for some reason its return a string thats the length of 256 characters. So i did a s.trim and it still is 256 characters long.
View 1 Repliesgiven my code below, I'm trying to figure out how to create an array of 1 byte containing 7 bits. So the byte in the array would contain 0111111 to correspond to mData_Out's boolean values. How would I change the following code? [Code]
View 8 RepliesI am trying to get a count of all the times a byte sequences occurs in another byte sequences. It cannot however re-use a bytes if it already counted them. For example given the string let's assume the byte sequence was k.k it would then find only 3 occurrences rather than 5 because they would be broke down like: [k.k].[k.k].[k.k]. and not like [k.[k].[k].[k].[k].k] where they over lap and essentially just shift 2 to the right.
Ideally the idea is to get an idea how a compression dictionary or run time encoding might look. so the goal would be to get down to just 2 parts, as (k.k.k.) is the biggest and best symbol you can have.
Here is source so far:
[Code]...
I have a DEM file(Digital Elevattion Model File) and I am trying to open the file,where we have Latitude and Longitude values inside the DEM file.And, Now I have opened the file using Filestream and Read the file using BinaryReader.But, I am having a trobule in getting the values of Latitude and Longitude.I am getting Byte values randomly as 1,202,31,271 etc.But, we dont have latitude and longitude values more than 180,and also I am not getting 16 bit UInt values.For example, the values should be like Latitude 20.00416666666667 and Longitude 39.99583333333333.So,read the correct Byte values of the DEM file.The code I have used till now is:
Dim fs As FileStream = New FileStream("C:UsersadminDesktopHeader and DEM fileE020N40.DEM", FileMode.Open, FileAccess.Read)
Dim rd As New BinaryReader(fs)
Dim convertDB As UInt16
For i = 0 To rd.BaseStream.Length
[code]....
I didn't think it fair to post a comment on Fredrik Mörk's answer in this 2 year old post, so I thought I'd just ask it as a new question instead..NB: This is not a critiscm of the answer in any way, I'm simply trying to understand this all before delving into memory management / the marshal class.
In that answer, the function GetByteArray allocates memory to each object within the given array, within a loop.Would the GetByteArray function on the aforementioned post have benefited at all from allocating memory for the total size of the provided array:
Dim arrayBufferPtr = Marshal.AllocHGlobal(Marshal.SizeOf(<arrayElement>) * <array>.Count)
I just wonder if allocating the memory, as shown in the answer, causes any kind of fragmentation? Assuming there may be fragmentation, would there be much of an impact to be concerned with? Would allocating the memory in the way I've shown force you to call IntPtr.ToInt## to obtain pointer offsets from the overall allocation pointer, and therefore force you to check the underlying architecture to ensure the correct method is used*1 or is there a better way? (ToInt32/ToInt64 depending on x86/64?)
*1 I read elsewhere that calling the wrong IntPtr.ToInt## will cause overflow exceptions. What I mean by that statement is would I use:
Dim anOffsetPtr As New IntPtr(arrayBufferPtr.ToInt## + (loopIndex * <arrayElementSize>))
I've read through a few articles on the VB.Net Marshal class and memory allocation; listed below, but if you know fo any other good articles I'm all ears![URL]..
is there is any direct method or function in array class that can check if any given byte array is a subarray of other array?or should i loop through the each byte of main array to see if smaller array exists in that or not?
View 2 RepliesI have a packet that I need to send to a client with an ID of 255. I've had no problems sending packets with IDs of 0, 1, and 2. The ID has to be 255. For some reason, after the translation has happened, both me with my server, and the client, get "63" for any Id greater than 127.
This is the code I am using:
Console.WriteLine(Asc(System.Text.ASCIIEncoding.ASCII.GetString(System.Text.ASCIIEncoding.ASCII.GetBytes(Chr("255")))))
Now, This is an overly complicated version of what the server does. You may consider this a bit unnecessary but the inverse functions performed are for your viewing reasons only.
Where it says "255" is the Packet Id I need sent in the format above. As I said, anything larger than 127 returns "63".
given the following Sub, how would I initialize byte array 'temp'to zeros and give it the length of the incoming byte array passed into the subroutine?
Sub ReceivePacket(ByVal buffer As Byte())
Dim temp() As Byte 'initialize to zeros and length of buffer
temp = buffer.Skip(17).ToArray()
End Sub
File.txt contains both chinese and english characters.. English characters are 1 byte and Chinese characters are 2 bytes.
View 9 RepliesI'm reading a binary file. Apparently its older, and some of its values are based not on position, but on null terminated bytes. Position 17 thru 35 represents a value, however, if their is a null byte at 30, then you get garbage if you try to read 17 thru 35 as a value...
I need to read, specifically, from 17 to 29 in this instance because pos 30 is a null byte.
Have to read it byte by byte, no problem. Not sure though the syntax of comparing a null byte specifically.
If you need a sample of the binary file, I can post it.
I have an array defined As string and the value stored in site is like "7E", "A1" and so on. But in order to send out this array through serial port. I need to change the the array to As Byte instead of using As String. How can I convert it?
View 8 Replies