Advertisement
Guest User

Bytes to Korean

a guest
Aug 28th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.35 KB | None | 0 0
  1. Private Sub ButtonConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonConvert.Click
  2.     Dim text As String = TextBoxInput.Text
  3.     If text.Length > 0 Then
  4.         Dim array As Byte() = StringToArray(text)
  5.         If array IsNot Nothing Then
  6.             TextBoxOutput.Text = System.Text.Encoding.Unicode.GetString(array)
  7.         End If
  8.     End If
  9. End Sub
  10.  
  11. Private Function StringToArray(ByVal text As String) As Byte()
  12.     Dim errorText As String = Nothing
  13.     If text IsNot Nothing Then
  14.         text = text.Replace(" ", Nothing).Trim()
  15.         Dim textLength As Integer = text.Length
  16.         If textLength Mod 2 = 0 Then
  17.             textLength /= 2
  18.             Dim array(textLength - 1) As Byte
  19.             Dim byteCounter As Integer = 0
  20.             Try
  21.                 For byteCounter = 0 To textLength - 1
  22.                     array(byteCounter) = Convert.ToByte(text.Substring(byteCounter * 2, 2), 16)
  23.                 Next
  24.                 Return array
  25.             Catch ex As Exception
  26.                 byteCounter += 1
  27.                 errorText = "An error occured while translating the " & byteCounter & IIf(byteCounter = 1, "st", IIf(byteCounter = 2, "nd", IIf(byteCounter = 3, "rd", "th"))) & " Byte."
  28.             End Try
  29.         Else
  30.             errorText = "Input is mal-formatted"
  31.         End If
  32.     Else
  33.         errorText = "No input has been defined!"
  34.     End If
  35.  
  36.     If errorText IsNot Nothing Then
  37.         MessageBox.Show(errorText, "Parsing-Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  38.     End If
  39.     Return Nothing
  40. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement