I want to convert a double array to byte array.
For a single double it is working fine, but I'm having trouble converting an entire array. Following is the code:
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("127.0.0.1", 50009)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
Dim value As Double = 45
Dim sendBytes As [Byte]() = BitConverter.GetBytes(value)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Now I want to convert an array of doubles to a byte array and send via tcp.
Dim values() As Double = {-12.3323, 45, 67}
Thanks
Arun
Related
How to get string from this code instead of array of char ?
Client Code :
stm = tcpClient.GetStream()
Dim ascenc As New ASCIIEncoding
Dim byteData() As Byte = ascenc.GetBytes(strMessage(counter))
Thread.Sleep(2000)
Console.WriteLine("Transmitted ")
stm.Write(byteData, 0, byteData.Length())
Server code :
Dim size As Integer = TcpSocket.Receive(bitData)
Dim chars(size) As Char
For i As Integer = 0 To size
chars(i) = Convert.ToChar(bitData(i)) // i want to get the string directly, how ?
Next
Dim newString As New String(chars)
Console.WriteLine(newString)
strMessage(counter) = newString
You can use ASCIIEncoding on the server as well. Just use its GetString() method rather than GetBytes().
Dim ascenc As New ASCIIEncoding
Dim newString As String = ascenc.GetString(bitData)
You already have it implemented with your code I suggested:
Dim MyString As String = New String(MyArray)
If you want to convert the byte array you can use:
Dim MyString As String = Encoding.ASCII.GetString(bytes)
I used vb .NET function ReadAllBytes to read a file and send it over a socket. When received, I used WriteAllBytes. The problem is they are not same size! The original is 16kb, but the received data is 24kb. My code is below. What am I doing wrong?
Dim bteRead() As Byte
Try
bteRead = IO.File.ReadAllBytes(filepath)
Catch ex As System.IO.IOException
End Try
Return bteRead
then i convert bytes to string and send it , and when received i convert it back from string to bytes and do the WriteAllBytes
Dim str As String = a(1)
Dim encod As New System.Text.UTF8Encoding
Dim byteData() As Byte = encod.GetBytes(str)
IO.File.WriteAllBytes("c:\lol.db", byteData)
Solution for me was to change:
Dim encod As New System.Text.UTF8Encoding
Dim byteData() As Byte = encod.GetBytes(str)
To
Dim byteData() As Byte = System.Text.Encoding.Default.GetBytes(str)
I figured this would be pretty straight forward but I have an issue with getting my AES Encryption function to return a Hex String. I can get it to work when I convert it to Base64 but I cannot get the String with Hex values. Here is my code. Any help would be appreciated.
Dim AES_ENCRYPTION As New System.Security.Cryptography.RijndaelManaged
Dim CODE_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = CODE_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES_ENCRYPTION.Key = hash
AES_ENCRYPTION.Mode = CipherMode.ECB
Dim AES_ENCRYPTOR As System.Security.Cryptography.ICryptoTransform = AES_ENCRYPTION.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = (Conversion.Hex(AES_ENCRYPTOR.TransformFinalBlock(Buffer, 0, Buffer.Length)))
Catch ex As Exception
End Try
Return encrypted
I've tried your example, and I've got nothing either.
So what I tried instead of having encrypted = (Conversion.Hex(AES_ENCRYPTOR.TransformFinalBlock(Buffer, 0, Buffer.Length))), I have used a loop to convert each byte to its hex equivalent, and concatenate it to encrypted.
Dim encrypted_byte() As Byte = AES_ENCRYPTOR.TransformFinalBlock(Buffer, 0, Buffer.Length)
For i As Integer = 0 To encrypted_byte.Length - 1
encrypted = encrypted & Hex(encrypted_byte(i)).ToUpper
Next
I'm not sure how you formatted your hex string in Java, but this should be at least a start.
Ok so i am working on this program which sends a packet to a minecraft server and in return it gives me information about the server;(message of the day, players online, max players)
The problem is the response is in UCS-2
So when i send the packet to the server and get the response in bytes. How do i convert it to ascii so i can work with it?
Here is my code so far
Dim client As New System.Net.Sockets.TcpClient()
client .Connect("178.33.213.54", 25565)
Dim stream As NetworkStream = client .GetStream
'Send Bytes
Dim sendBytes As [Byte]() = {&HFE}
stream.Write(sendBytes, 0, sendBytes.Length)
'Receive Bytes
Dim bytes(client .ReceiveBufferSize) As Byte
stream.Read(bytes, 0, CInt(leclient.ReceiveBufferSize))
'Convert it to ASCII
....
'Output it to Console
....
Here is the same code in PHP, python, and ruby.
php -> https://gist.github.com/1235274
python -> https://gist.github.com/1209061
ruby -> http://pastebin.com/q5zFPcXV
The documentation is here:
http://www.wiki.vg/Protocol#Server_List_Ping_.280xFE.29
Thanks in advance!
Vidhu
Tested and working.
Dim client As New System.Net.Sockets.TcpClient()
client.Connect("178.33.213.54", 25565)
Dim stream As System.Net.Sockets.NetworkStream = client.GetStream
'Send Bytes
Dim sendBytes As [Byte]() = {&HFE}
stream.Write(sendBytes, 0, sendBytes.Length)
''Receive Bytes
Dim bytes(client.ReceiveBufferSize) As Byte
stream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
Dim sb As New System.Text.StringBuilder
For i As Integer = 3 To bytes.GetUpperBound(0) Step 2
Dim byt2(1) As Byte
byt2(0) = bytes(i + 1)
byt2(1) = bytes(i)
Dim ii As Integer = BitConverter.ToInt16(byt2, 0)
'sb.Append(Hex(ii)) 'debug
sb.Append(ChrW(ii))
Next i
MsgBox(sb.ToString)
stream.Close()
I am creating an application to create a key that is unique to each computer. This info is derived from the OS serial number and processorID.
Is there a way to 'shorten' a string? Maybe by converting it to HEX or something else...
The reason is this: I used to use a VB6 section of code (http://www.planet-source-code.com/vb...48926&lngWId=1) that gets the details and the output is only 13 digits long. Mine is a lot longer, but gets the same info...
BTW, the link I posted above won multiple awards, but I am having huge trouble in converting it to .NET. Has anyone by any chance converted it, or know of someone who has? Or a tool that actually works?
Thanks
EDIT
Here is the full working link: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=48926&lngWId=1
It sounds like you want a 'hashing algorithm' or 'hash function'. They are a common concept: http://en.wikipedia.org/wiki/Hash_function
Generally speaking you can simply write your own function to take a string and return a hashed number but there is some suitable code here that uses the .NET framework: http://support.microsoft.com/kb/301053
Here is a working example which retrieves the Processor ID, for the first Processor found, and the OS Serial Number; it concatenates these to strings together and then performs various encodings on them.
This is a simple VB.Net Console project. Be sure to reference the System.Management assembly in your project. Just copy and paste this code example into the main module, set a breakpoint at the end of Sub Main(), and look at the various results.
Module Module1
Sub Main()
Dim uniqueID As String = GetUniqueID()
' convert it to a base64 string
Dim encoding As New Text.ASCIIEncoding()
Dim result1 = Convert.ToBase64String(encoding.GetBytes(uniqueID))
' compress it
Dim result2 As String = CompressString(uniqueID)
Dim result3 As String = DecompressString(result2)
' encrypt it
Dim result4 As String = AES_Encrypt(uniqueID, "password")
Dim result5 As String = AES_Decrypt(result4, "password")
' hash it
Dim result6 As String = HashString(uniqueID)
End Sub
Private Function GetUniqueID() As String
Dim result As String = GetOSSerialNumber()
Dim processorIDs() As String = GetProcessorIDs()
If ((processorIDs IsNot Nothing) AndAlso (processorIDs.Count > 0)) Then
result &= processorIDs(0)
End If
Return result
End Function
Private Function GetProcessorIDs() As String()
Dim result As New List(Of String)
Dim searcher = New System.Management.ManagementObjectSearcher("Select ProcessorId from Win32_Processor")
For Each managementObj In searcher.Get()
result.Add(CStr(managementObj.Properties("ProcessorId").Value))
Next
Return result.ToArray()
End Function
Private Function GetOSSerialNumber() As String
Dim result As String = ""
Dim searcher = New System.Management.ManagementObjectSearcher("Select SerialNumber from Win32_OperatingSystem")
For Each managementObj In searcher.Get()
result = CStr(managementObj.Properties("SerialNumber").Value)
Next
Return result
End Function
Public Function CompressString(ByVal source As String) As String
Dim result As String = ""
Dim encoding As New Text.ASCIIEncoding()
Dim bytes() As Byte = encoding.GetBytes(source)
Using ms As New IO.MemoryStream
Using gzsw As New System.IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Compress)
gzsw.Write(bytes, 0, bytes.Length)
gzsw.Close()
result = Convert.ToBase64String(ms.ToArray)
End Using
End Using
Return result
End Function
Public Function DecompressString(ByVal source As String) As String
Dim result As String = ""
Dim bytes() As Byte = Convert.FromBase64String(source)
Using ms As New IO.MemoryStream(bytes)
Using gzsw As New System.IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Decompress)
Dim data(CInt(ms.Length)) As Byte
gzsw.Read(data, 0, CInt(ms.Length))
Dim encoding As New Text.ASCIIEncoding()
result = encoding.GetString(data)
End Using
End Using
Return result
End Function
Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
End Try
Return encrypted
End Function
Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim decrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
Dim Buffer As Byte() = Convert.FromBase64String(input)
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
End Try
Return decrypted
End Function
Private Function HashString(ByVal source As String) As String
Dim encoding As New Text.ASCIIEncoding()
Dim bytes() As Byte = encoding.GetBytes(source)
Dim workingHash() As Byte = New System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(bytes)
Dim result As String = ""
For Each b In workingHash
result = result & b.ToString("X2")
Next
Return result
End Function
End Module