0

I have a string which comes from a dll. Supposedly, the string is converted from Byte() in the dll.

Below is snippet for how I get value from dll :

    'DLL declare : Public Function getData(boolData as Boolean, strData1 as String, strData2 as String) as Boolean 

    Call objValue.getValue(True, strValueA, strValueB) 

    strResultA = strValueA

I want to convert the string into hex value. When I tried to confirm the hex result is correct using hex converter in the internet. The hex value is not same.

I suspect it has some null value in the string because when I output the string into a textbox and convert it, it give the correct hex value. Eg: toHex(tbResultA.text), will give a correct hex value.

How to remove null character from a string? I have tried multiple ways to remove it, but it doesn't work. Here is snippet I have tried:

    1. strResultA = strValueA.Replace(vbNullChar, "")

    2. strResultA = strValueA.Replace(Chr(0, "")

    3. strResultA = strValueA.Replace(Convert.ToChar(0), "")

    4. strResultA = strValueA.Replace(Convert.vbNullChar, "")

    5. strResultA = Replace(strValueA, Chr(0), "")

Here is my snippet to convert string to hex :

    Private Function toHex(ByVal strValue As String) As String
      Dim byteArray() As Byte
      Dim hex As System.Text.StringBuilder = New System.Text.StringBuilder
      byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(strValue)
      For i As Integer = 0 To byteArray.Length - 1
         hex.Append(byteArray(i).ToString("x"))
      Next
      Return hex.ToString()
    End Function


    'My fuction calling, strHexA will be kept in a text file:
    Dim strHexA as String = toHex(strResultA)

Thanks.

Peter Macej
  • 4,831
  • 22
  • 49
MABY
  • 1
  • 3
  • 1
    How are you sure there are null characters in the string? What are the wrong and expected hex values of `strResultA` after the conversion. – preciousbetine Jan 16 '20 at 09:20
  • 1
    Are you sure that the string is encoded as ASCII? – Andrew Morton Jan 16 '20 at 09:21
  • 1
    Are you sure that the string content can be represented by ASCII chars alone? Maybe you have chars in there that can only be represented by Unicode codepoints. Try this string for example: `dim weirdString as string = "A ѕtrange SТRING" dim bytes = Encoding.ASCII.GetBytes(weirdString) dim weirdStringOut as string = Encoding.ASCII.GetString(bytes)`. What comes out of it? (Hoping that the comment editor doesn't ruin it :). Also, a TextBox of course can represent Unicode chars without a problem. – Jimi Jan 16 '20 at 09:30
  • If there are definitely null bytes, there is a distinct chance that the string uses UTF-16LE encoding, e.g. "ABC" would be `41 00 42 00 43 00`. – Andrew Morton Jan 16 '20 at 10:35
  • @Andrew Morton Or UTF8, if the codepoints fall beyond `0x80`. You get those bytes only if you use `Encoding.Unicode.GetBytes()`. Encoding.ASCII would return 3 bytes: `0x41 0x42 0x43`. There's a chance that the OP's string is a WideChar string marshaled incorrectly. Or simply, as already mentioned, contains chars that cannot be represented by ASCII chars. Quite common in non latin-1 zones. – Jimi Jan 16 '20 at 10:46
  • I am not sure if there was a null. But I am sure that it is encoded as ASCII. The string suppose to be like this "!N029dR_:OV7LHdN" before hex. Anyway, thanks. I got the answer already. Since I know that the string will alway be 16 character. I use strResultA = strValueA.Substring(0, 16) to get only the 16 character that I want. – MABY Jan 16 '20 at 10:54
  • 1
    It may simply be a common, null-terminated string. – Jimi Jan 16 '20 at 10:56
  • What do you mean by null-terminated string. How to mark this question as solve? Sorry, I`m new to this stuff. – MABY Jan 16 '20 at 11:01
  • @Asyraf If someone submits a correct answer (or you do yourself) the answer can be marked as accepted. – Craig Jan 16 '20 at 14:03
  • A null terminated string is simply a string that is terminated with a null character. i.e `string[string.Length] = 0`. – preciousbetine Jan 16 '20 at 17:54

1 Answers1

0

I am not sure if there was a null. But I am sure that it is encoded as ASCII. The string suppose to be like this "!N029dR_:OV7LHdN" before hex.

Anyway, thanks. I got the answer already. Since I know that the string will alway be 16 character.

I use **

strResultA = strValueA.Substring(0, 16)

** to get only the 16 character that I want.

MABY
  • 1
  • 3