Convert Arabic string to an array of bytes - vb.net

I have a function which converts string to an array of bytes. If the string is written in English, the function works fine. But if the input string is Arabic, the function doesn't return, and I get this error:
Value was either too large or too small for an unsigned byte
Friend Function StringtoByteArray(ByRef value As String) As Byte()
Dim temp() As Byte
ReDim temp(Len(value) - 1)
Dim i As Integer
For i = 0 To Len(value) - 1 Step 1
temp(i) = Convert.ToByte(Convert.ToChar(Mid(value, i + 1, 1)))
Next
StringtoByteArray = temp
End Function
What should I change to convert Arabic characters to byte?
I am using VB.NET.

You don't need to write your function for that, this should work:
Dim b As Byte() = System.Text.Encoding.Unicode.GetBytes(value)

Related

Change byte array to integer in VB.net

I have a byte array need to convert to integer, and this array only have one value. I tried Bitconverter, convert.ToInt32 both are not working for me. my code as follows:
Dim a As new Byte() ={&H1C} ' the value range is {&H01} to {&HFF}
Dim key As integer = BitConverter.ToInt32(a,1)
I need the result with key = 28, which convert function I should use?
Thank you very much.
BitConverter.ToInt32 needs 4 bytes to work with, so you just need to put your one byte value into a 4 byte array. Allowing for the endianness, something like this:
Dim a() As Byte = { &H1C }
Dim b(3) As Byte
If BitConverter.IsLittleEndian Then
b(0) = a(0)
Else
b(3) = a(0)
End If
Dim key As Integer = BitConverter.ToInt32(b, 0)
You are not converting an array of values, but rather a single array element.
That said, there is no need to call a conversion function to convert a single Byte to an Integer. Just assign the value.
Dim key As Integer = a(0)

Convert Decimal to Ascii?

this is how to convert decimal to Ascii, but i have decimal in defferent format, and i want ot convert it to ascii.
Public Shared Function DecimalToASCII(dec As String) As String
Dim ascii As String = String.Empty
For i As Integer = 0 To dec.Length - 1 Step 3
ascii += CChar(ChrW(Convert.ToByte(dec.Substring(i, 3))))
Next
Return ascii
End Function
EX:
This is in Decimal and i want ot input it in textbox1.text:
"912,697,583,1065,261"
and i want to do operation to each group of numbers between comma and then convert it to Ascii???
To split the string into groups, you can just use the Split command. Your code is a long winded way of doing this - although it will fall over when it tries to deal with the four digit number.
Create a string array with no predefined number of elements like this -
Dim myArray()
Populate it with this code
myArray = Split (dec,",")
So now, using your input example, your ascii string array contains this data
myArray (0) = "912"
myArray (1) = "697"
myArray (2) = "583"
myArray (3) = "1065"
myArray (4) = "261"
If you want to have numbers that you can use in arithmetic operations, use this code instead. The function assumes that you're using integers, but if you want to use another type, just change all the occurrences of Integer to the type you want to handle, and change the CInt function to CDbl or cSng.
Public Shared Function DecimalToASCII(dec As String) As Integer()
'create an array of strings
Dim ascii() As String
'split each group into the array
ascii = Split(dec, ",")
'declare numbers array that is the same size as the ascii array
Dim numbers(ascii.GetUpperBound(0)) As Integer
'convert the array of strings to an array of numbers
For i As Integer = 0 To ascii.GetUpperBound(0) - 1
numbers(i) = CInt(ascii(i))
Next
'return an array of numbers containing each group
Return numbers
End Function
Use it like this
Dim dec As String = "912,697,583,1065,261"
Dim MyNumbersArray() As Integer = DecimalToASCII(dec)
With this code, you will have an array of Integers like this
MyNumbersArray(0) = 912
MyNumbersArray(1) = 697
MyNumbersArray(2) = 583
MyNumbersArray(3) = 1065
MyNumbersArray(4) = 261
Now you can perform whatever math you want using the array elements.

How to replace bytes in VB.NET?

I have two strings:
Dim Original_Hex_Bytes as string = "616572646E61"
Dim Patched_Hex_Bytes as string = "616E64726561"
Then I have a binary file and I need to search for the Original_Hex_Bytes and replace them with Patched_Hex_Bytes; I don't konw the offset where begin to write new bytes :(
How can I do this?
If needed, I also know how to convert Hex strings in bytes, I use this:
Private Function Hex_To_Bytes(ByVal strinput As String) As Byte()
Dim i As Integer = 0
Dim x As Integer = 0
Dim bytes(strinput.Length / 2) As Byte
Do While (strinput.Length > i + 1)
Dim lngDecimal As Long = Convert.ToInt32(strinput.Substring(i, 2), 16)
bytes(x) = Convert.ToByte(lngDecimal)
i += 2
x += 1
Loop
Return bytes
End Function
You can use BinaryReader and BinaryWriter classes to achieve this.
But in this case, as you do not know the file structure, need to read the entire file and sweep it in search of bytes array and will be easier to use ASCII strings as aerdna and andrea.
When you know the structure of a file is more appropriate to work with data structure to manipulate its contents.

Convert string of byte array back to original string in vb.net

I have a plain text string that I'm converting to a byte array and then to a string and storing in a database.
Here is how I'm doing it:
Dim b As Byte() = System.Text.Encoding.UTF8.GetBytes("Hello")
Dim s As String = BitConverter.ToString(b).Replace("-", "")
Afterwards I store the value of s (which is "48656C6C6F") into a database.
Later on, I want to retrieve this value from the database and convert it back to "Hello". How would I do that?
You can call the following function with your hex string and get "Hello" returned to you. Note that the function doesn't validate the input, you would need to add validation unless you can be sure the input is valid.
Private Function HexToString(ByVal hex As String) As String
Dim result As String = ""
For i As integer = 0 To hex.Length - 1 Step 2
Dim num As Integer = Convert.ToInt32(hex.Substring(i, 2), 16)
result &= Chr(num)
Next
Return result
End Function
James Thorpe points out in his comment that it would be more appropriate to use Encoding.UTF8.GetString to convert back to a string as that is the reverse of the method used to create the hex string in the first place. I agree, but as my original answer was already accepted, I hesitate to change it, so I am adding an alternative version. The note about validation of input being skipped still applies.
Private Function HexToString(ByVal hex As String) As String
Dim bytes(hex.Length \ 2 - 1) As Byte
For i As Integer = 0 To hex.Length - 1 Step 2
bytes(i \ 2) = Byte.Parse(hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber)
Next
Return System.Text.Encoding.UTF8.GetString(bytes)
End Function

Code Returns Unicode characters How to Convert to ASCII

Imports Microsoft.VisualBasic
Public Class VigenereCipher
Public Shared Function Encrypt(ByVal cipherTxt As String, ByVal key As String)
Dim encryptedText As String = ""
For i As Integer = 1 To cipherTxt.Length
Dim temp As Integer = Asc(GetChar(cipherTxt, i)) _
+ Asc(GetChar(key, i Mod key.Length + 1))
encryptedText += Chr(temp)
Next
Return encryptedText
End Function
Public Shared Function Decrypt(ByVal cipherTxt As String, ByVal key As String)
Dim decryptedText As String = ""
For i As Integer = 1 To cipherTxt.Length
Dim temp As Integer = Asc(GetChar(cipherTxt, i)) _
- Asc(GetChar(key, i Mod key.Length + 1))
decryptedText += Chr(temp)
Next
Return decryptedText
End Function
End Class
I would want the program to return regular characters because it outputs unicode characters.
Straight from MSDN: You should use the Encoding.Convert Method
Example code (from MSDN):
Public Function UnicodeToAscii( Byval unicodeString as String) As String
Dim ascii As Encoding = Encoding.ASCII
Dim unicode As Encoding = Encoding.Unicode
' Convert the string into a byte array.
Dim unicodeBytes As Byte() = unicode.GetBytes(unicodeString)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes)
' Convert the new byte array into a char array and then into a string.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)
Return asciiString
End Function
The code works fine, since i got the same plain text after decryption : I execute the encryption code with input Encrypt="abcdef" and key=""hxc" it produces the output for temp as follows
when
i=1 temp= 217 'Ù
i=2 temp= 197 'Å
i=3 temp= 203 'Ë
i=4 temp= 220 'Ò
i=5 temp= 200 'Ò
i=6 temp= 206 'Î
from further checking i got the conclusion that your algorithm will produce a value higher than 150 for most of the inputs. The reference says that, The extended ASCII codes (character code 128-255)
There are several different variations of the 8-bit ASCII table. The table below is according to ISO 8859-1, also called ISO Latin-1. Codes 129-159 contain the Microsoft® Windows Latin-1 extended characters.
Comclusion: The problem for producing Unicode characters is that the algorithm you ware choosing will produce a value greater than 127 for temp.