Convert Hex to String: special characters not converted properly - vb.net

I have to convert the following hex encoded string:
56 6f 6e 68 c3 b6 67 65 6e
The result should be: Vonhöger
However, the result I get is: Vonhöger
What is wrong with my code?
For x = 0 To hexString.Length - 1 Step 2
Dim k As String = hexString.Substring(x, 2)
If (k <> "X'") Then
com &= Chr(Val("&h" & k))
End If
Next

You are converting each byte to a unicode character, but the data is UTF-8 encoded, so some bytes in combination forms a character. The bytes c3 b6 is the code for the ö character.
Convert the data into bytes, then decode it as UTF-8:
Dim hexString = "566f6e68c3b667656e"
Dim bytes() As Byte
ReDim bytes(hexString.Length \ 2 - 1)
For i As Integer = 0 To bytes.Length - 1
bytes(i) = Convert.ToByte(hexString.Substring(i * 2, 2), 16)
Next
Dim com As String = Encoding.UTF8.GetString(bytes)

Similar to the other suggestions:
<Extension>
Public Function FromHexToUnicodeString(target As String) As String
Dim bytes((target.Length \ 2) - 1) As Byte
For b = 0 To bytes.Length - 1
bytes(b) = Convert.ToByte(target.Substring(b * 2, 2), 16)
Next
Return Text.Encoding.UTF8.GetString(bytes)
End Function
Usage:
MessageBox.Show("566f6e68c3b667656e".FromHexToUnicodeString)

Related

VB.Net looping through a string

I have a string say "768932A3" that I want split up into two's like this:
76 89 32 A3 for purposes of conversion to binary. I have tried looping but it doesn't work. Kindly help:
For i = 0 To data.Length - 1
j = i + 2
incremented &= Convert.ToInt64(data.Substring(i, j), 16)
ascii &= Chr(Convert.ToInt64(data.Substring(i, j), 16))
i = j + 2
Next
The second parameter of Substring is not the ending position but the lenght of characters to consider from the starting position.
For i = 0 To data.Length - 1 Step 2
' Not clear what are you trying to do here
' incremented &= Convert.ToInt64(data.Substring(i, 2), 16)
Dim value As Long = Convert.ToInt64(data.Substring(i, 2), 16)
ascii &= Chr(Convert.ToInt64(data.Substring(i, 2), 16))
Next

Convert big string to decimal in vb.net

Hello i have big string value which is md5 of something now i need to convert it into the decimal value
for example
Dim md5_s As String = "6F05AF42533432A5513610FE839ACC86"
now i need output same like the online converters to this
"54 70 48 53 65 70 52 50 53 51 51 52 51 50 65 53 53 49 51 54 49 48 70
69 56 51 57 65 67 67 56 54 "
is it possible i don't want spaces in the above converted decimal ?
vb.net help please
Okay here i tried and got it n is my approach works fine will this fine n work always right
Dim t As String
Dim a As String = "6F05AF42533432A5513610FE839ACC86"
For Each c As Char In a
t &= Convert.ToInt32(c)
Next
TextBox1.Text = t
will this one is right ?
result is same what i am looking for as
5470485365705250535151525150655353495154494870695651576567675654
so i assume this is right huh ?
I'm not quite sure this is what you are really looking for but this is what you asked for
For count = 0 To md5_s.Length - 1
Dim tempChar As String = md5_s.Substring(count, 1)
Console.Write(Asc(tempChar))
Next
What is more likely what you want is something like this
Private Function HexToByteArray(ByVal hex As [String]) As Byte()
Dim NumberChars As Integer = hex.Length
Dim bytes As Byte() = New Byte(NumberChars / 2 - 1) {}
For i As Integer = 0 To NumberChars - 1 Step 2
bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
Next
Return bytes
End Function
either way ... hope this helps

Unsigned Byte flip with *.bin file [duplicate]

This question already has answers here:
16 bit unsigned short byte flip in VB.NET
(2 answers)
Closed 8 years ago.
I need example in code, how to byte flip whole binary file. VB.NET
Example
02 00 0D 78 10 20 40 80 F1 F2 F4 F8 1F 2F 4F 8F
Flip Operation
00 02 78 0D 20 10 80 40 F2 F1 F8 F4 2F 1F 8F 4F
Whole, binary file using OpenFileDialog (OFD)
You just need to loop all items of the array and swap each bytes.
Dim bytes() As Byte = {&H2, &H0, &HD, &H78, &H10, &H20, &H40, &H80, &HF1, &HF2, &HF4, &HF8, &H1F, &H2F, &H4F, &H8F}
For i As Integer = 0 To bytes.Length - 1 Step 2
bytes(i) = bytes(i) Xor bytes(i + 1)
bytes(i + 1) = bytes(i + 1) Xor bytes(i)
bytes(i) = bytes(i) Xor bytes(i + 1)
Next
If you want, use a temp variable
For i As Integer = 0 To bytes.Length - 1 Step 2
Dim tempByte As Byte = bytes(i)
bytes(i) = bytes(i+1)
bytes(i + 1) = tempByte
Next
erm, you could write as you go, something like,
Dim file = dialog.FileName
Using output = New BinaryWriter(New FileStream( _
file, _
FileMode.Append, _
FileAccess.Write, _
FileShare.Read))
Using input = New BinaryReader(New FileStream( _
file, _
FileMode.Open, _
FileAccess.Read, _
FileShare.ReadWrite))
Dim bufferLength = 2
While bufferLength = 2
Dim buffer = input.ReadBytes(2)
bufferLength = buffer.Length
If bufferLength = 2 Then
output.Write(buffer(1))
output.Write(buffer(0))
Else If bufferLength = 1 Then
output.Write(buffer(0))
End If
End While
End Using
End Using

How to generate a 26-character hex string that equals to 106 bits and ((53 Ones - 53 Zeros) in binary)

I am looking for a way to generate a hexadecimal string that equals out to 106 bits, more specifically fifty three 1's and fifty three 0's after each hex char is converted to binary and added together. I'd like to keep it as random as possible considering the parameters of the request. How would I go about keeping an eye on the construction of the string so that it equals out the way I want?
For example:
(a8c05779f8934b14ce96f8aa93) =
(1010 1000 1100 0000 0101 0111 0111 1001 1111 1000 1001 0011 0100
1011 0001 0100 1100 1110 1001 0110 1111 1000 1010 1010 1001 0011)
One option is to create a list with an equal number of 0s and 1s and then sort it with an array of random keys:
Sub Main()
' Start with a list of 53 0's and 1's
Dim bitsList = New List(Of Integer)
For i = 1 To 53
bitsList.Add(1)
bitsList.Add(0)
Next
Dim bits = bitsList.ToArray()
' Create list of random keys
Dim keys = New List(Of Integer)
Dim rand = New Random()
For i = 1 To bits.Count
keys.Add(rand.Next())
Next
' Sort bits by random keys
Array.Sort(keys.ToArray(), bits)
' Create hex string
Dim s = ""
For i = 1 To bits.Length - 4 Step 4
Dim digit = bits(i + 3) * 8 + bits(i + 2) * 4 + bits(i + 1) * 2 + bits(i)
s = s + Hex(digit)
Next
Console.WriteLine(s)
End Sub
You can place 52 ones randomly in a 104 bit number by keeping track of how many ones has been placed already and calculate the probability that the next digit should be one. The first digit always has 1/2 probability (52/104), then the second digit has 51/103 or 52/103 probability depending on what the first digit was, and so on.
Put the bits in a buffer, and when it is full (four bits), that makes a hexadecimal digit that you can add to the string:
Dim rnd As New Random()
Dim bin As New StringBuilder()
Dim buf As Integer = 0, bufLen As Integer = 0, left As Integer = 52
For i As Integer = 104 To 1 Step -1
buf <<= 1
If rnd.Next(i) < left Then
buf += 1
left -= 1
End If
bufLen += 1
If bufLen = 4 Then
bin.Append("0123456789abcdef"(buf))
bufLen = 0
buf = 0
End If
Next
Dim b As String = bin.ToString()
To make a 106 bit value, change these lines:
Dim buf As Integer = 0, bufLen As Integer = 0, left As Integer = 53
For i As Integer = 106 To 1 Step -1
The resulting string is still 26 characters, the two extra bits are in the buf variable. It has a value between 0 and 3 that you can use to create the 27th character, however that is done.
To add a 22 bit hash to the string, you can use code like this:
bin.Append("048c"(buf))
Dim b As String = bin.ToString()
Dim m As New System.Security.Cryptography.SHA1Managed
Dim hash As Byte() = m.ComputeHash(Encoding.UTF8.GetBytes(b))
'replace first two bits in hash with bits from buf
hash(0) = CByte(hash(0) And &H3F Or (buf * 64))
'append 24 bits from hash
b = b.Substring(0, 26) + BitConverter.ToString(hash, 0, 3).Replace("-", String.Empty)

Generate random string in text field

We have that old software (made by one of the first employees many years ago) in company that uses Microsoft Access to run. Boss asked me to add a random string generation in the specific text box on click but i have no idea how to do that. I dont have any Microsoft Access programming experience, thats why i am askin you to help.
I managed to create button and text field so far. Thats where it stops. I also managed to access the code for the button action:
Private Sub command133_Click()
End Sub
This is one way, will work in Access VBA (which is an older basic than vb.net). It will generate a string with letters and numbers.
Sub test()
Dim s As String * 8 'fixed length string with 8 characters
Dim n As Integer
Dim ch As Integer 'the character
For n = 1 To Len(s) 'don't hardcode the length twice
Do
ch = Rnd() * 127 'This could be more efficient.
'48 is '0', 57 is '9', 65 is 'A', 90 is 'Z', 97 is 'a', 122 is 'z'.
Loop While ch < 48 Or ch > 57 And ch < 65 Or ch > 90 And ch < 97 Or ch > 122
Mid(s, n, 1) = Chr(ch) 'bit more efficient than concatenation
Next
Debug.Print s
End Sub
Try this function:
Public Function GetRandomString(ByVal iLength As Integer) As String
Dim sResult As String = ""
Dim rdm As New Random()
For i As Integer = 1 To iLength
sResult &= ChrW(rdm.Next(32, 126))
Next
Return sResult
End Function
Workin on #Bathsheba code, I did this. It will generate a random string with the number of characters you'd like.
Code :
Public Function GenerateUniqueSequence(numberOfCharacters As Integer) As String
Dim random As String ' * 8 'fixed length string with 8 characters
Dim j As Integer
Dim ch As Integer ' each character
random = ""
For j = 1 To numberOfCharacters
random = random & GenerateRandomAlphaNumericCharacter
Next
GenerateUniqueSequence = random
End Function
Public Function GenerateRandomAlphaNumericCharacter() As String
'Numbers : 48 is '0', 57 is '9'
'LETTERS : 65 is 'A', 90 is 'Z'
'letters : 97 is 'a', 122 is 'z'
GenerateRandomAlphaNumericCharacter = ""
Dim i As Integer
Randomize
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
Randomize
Select Case i
Case 1 'Numbers
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 9 + 48)
Case 2 'LETTERS
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 65)
Case 3 'letters
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 97)
End Select
End Function
I use it with random number of characters, like this :
'Generates random Session ID between 15 and 30 alphanumeric characters
SessionID = GenerateUniqueSequence(Rnd * 15 + 15)
Result :
s8a8qWOmoDvC4jKRjPr5hOY12u 26
TB24qZ4cNfr6EdyY0J 18
6LZRQ9P5WHLNd71LIdqJ 20
KPN0RmlhhJKnVzPTkW 18
R2pNOKWJMKl9KpSoIV2egUNTEb1QC2 30
X8jHuupP6SvEI8Dt2wJi 20
NOTE: This is still not completely random. It will give a higher count of numbers than normal as approx 1/3 of all chars generated will be numbers.
Normally distribution will look like:
10 numbers plus 26 lowercase plus 26 uppercase = 62 possible chars.
Numbers will normally be 10/62 parts of the string or 1/6.2
With the code
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
the count of numbers is pushed up to 1/3 (on average)
Probably not too much of a worry - unless you are trying to beat the NSA and then you have decreased your range significantly.