3

I been trying to remove all the zero characters from my string

My string is made from these hexadecimal bytes

00 44 00 65 00 6C 00 70 00 68 00 69

For every letter there is a Zero byte (null byte) in front of it.. I was guessing I had to use some kind of Unicode encoding or wide encoding to get the text without those zero's.

But I couldn't figure it out so I figured best way is to use a Replace but even that fails.

Dim packet() As String = {&H0, &H44, &H0, &H65, &H0, &H6C, &H0, &H70, &H0, &H68, &H0, &H69}
Dim str As String = Encoding.ASCII.GetString(packet, 0, 12)
str = str.Replace("\0", "")  'Compiles and fails
str = str.Replace(\0, "")  'No compile
str = str.Replace('\0', "")  'No compile
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
SSpoke
  • 5,656
  • 10
  • 72
  • 124

3 Answers3

8

If you want something that doesn't rely on the Microsoft.Visualbasic namespace:

str = str.Replace(Convert.ToChar(0),"")

The only alternative is to using String.Replace I can think of is to use a regex replace: http://msdn.microsoft.com/en-us/library/844skk0h(v=vs.110).aspx

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • 1
    Or simply `str = str.Replace(Char.MinValue, "")` – Bjørn-Roger Kringsjå May 16 '14 at 06:11
  • 3
    This is the wrong answer. It is a band-aid that addresses the symptom, not fixes the root cause - the wrong `Encoding` is being used to convert the bytes to a `String`. Fix the encoding and the replacement is not needed anymore. – Remy Lebeau May 19 '14 at 04:05
5

The real problem is not the null bytes, but in how you are decoding the bytes Ínto a String in the first place. You are using the wrong Encoding. You should be using Encoding.BigEndianUnicode instead of Encoding.ASCII, then you don't need to replace the nulls manually at all as they will be handled for you by the decoding process:

Dim packet() As Byte = {&H0, &H44, &H0, &H65, &H0, &H6C, &H0, &H70, &H0, &H68, &H0, &H69}
Dim str As String = Encoding.BigEndianUnicode.GetString(packet)
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Perfect I knew it was unicode but I didn't know why it didn't work. This is the one yup – SSpoke May 19 '14 at 08:29
  • @SSpoke In the Microsoft .NET Base Class libraries (and much of Microsoft's work), "Unicode" refers not to just the character set, but (esoterically) to the UTF-16 encoding of the Unicode character set. Remy points out, UTF-16 has a byte order that you need to consider. (For comparison, there are other encodings for Unicode. UTF-8 is very popular, in fact, it's the default for .NET's file streams.) – Tom Blodget May 20 '14 at 02:42
  • 2
    In particular, Microsoft uses UTF-16 **Little Endian** everywhere (what `Encoding.Unicode` uses). But some protocols/platforms use **Big Endian** instead, which is why `Encoding.BigEndianUnicode` is also offered. – Remy Lebeau May 20 '14 at 04:06
4

Solved it

 str = str.Replace(vbNullChar, "")

Still looking for a way to do this with a built-in function not relying on Replace function

SSpoke
  • 5,656
  • 10
  • 72
  • 124
  • 2
    What is 'Replace' if it is not "built-in"? – Matt Wilko Nov 28 '14 at 05:37
  • Replace is built-in Visual Basic.NET edition any String you just type a `.` dot and it shows you a list of possible commands you can use. Yeah in Visual Basic 6 you just use `Replace(str, "Hi", "Bye")` also built-in. OPPS HAHA I miss read my own answer. Yeah I goofed up. Just using functions like `Replace` is like a quick-fix in my opinion and not a solid fix. It's like relying on `Replace` when you need to use Regular Expressions. Or using Regular Expressions when you need to build a full-fletched HTML Parser. – SSpoke Nov 28 '14 at 05:44