index > Visual C# General > Converting '00' hex character into '20' hex.

Converting '00' hex character into '20' hex.

Hi,

I am having difficulty handling a byte array that contains the hex equivalent of '00'. .Net seem to treat this as an escape character when I use the byte array converted to a string. I would like to replace the special character with a space ('20') but I cannot seem how to do this. Mainly because '00' does not have a string equivalent. Any ideas?

Thanks.

Shakster

If it is in a byte array then you can enumerate the array and replace all '00' values with '20' values.

for (int nIdx = 0; nIdx < arr.Length; ++nIdx)
{
if (arr[nIdx] == 0)
arr[nIdx] = 0x20;
}

You could get fancy and use the Array.ForEach method as well.

static class ArrayEx
{
public static void Replace<T> ( T[] array, T oldValue, T newValue )
{
Replacer<T> replacer = new Replacer<T>(array, oldValue, newValue);
Array.ForEach<T>(array, replacer.Replace);
}

private sealed class Replacer<T>
{
public Replacer ( T[] array, T oldValue, T newValue )
{
m_Array = array;
m_Old = oldValue;
m_New = newValue;
}

public void Replace ( T value )
{
if (value.Equals(m_Old))
m_Array[m_nIndex] = m_New;
++m_nIndex;
}

private T[] m_Array;
private int m_nIndex;
private T m_Old;
private T m_New;
}
}

class Program
{
static void Main ( string[] args )
{
byte[] arr = { 4, 5, 6, 0, 1, 2, 3, 0, 8, 9, 0 };

ArrayEx.Replace<byte>(arr, 0, 32);

Array.ForEach<byte>(arr, delegate( byte value ) { Console.WriteLine(value); });
}
}

Notice here that it requires a lot more work but it can be generalized such that you can reuse the replace routine for any array type and values.

Michael Taylor - 9/6/06

TaylorMichaelL

I'd recommend again the Replacer method, because it assumes that the Action delegate will be called for each item in the array in order.  Now, that is almost certainly the way it is implement, but that is not guarunteed by the specification.  For example, a completely valid (and for some platforms, completely reasonable) implementation would be for ForEach to spawn a separate thread for each item in the array, at which point it's up to the whim of the scheduler which goes first.

Futher, is that really any more generalized/reuseable than the following? ...

static class ArrayEx
{
  
public static void Replace<T> ( T[] array, T oldValue, T newValue )
   {
           forach(int i =0; i < array.Length; ++i)
          {
                 if (array[ i ] == oldValue)
                       array[ i ] = newValue;
         }
}




Truth, James (http://www.honestillusion.com)
James Curran

The order is determinate. The order is determined by the IEnumerator object that is returned when a call is made to the IEnumerable.GetEnumerator method. This is how the standard for each loop is implemented. It is the standard "I need to enumerate a list" method used throughout .NET. There would not be any reason why MS would change how a list is enumerated in this particular method. Although in theory enumeration could be parallelized that would break many existing applications and therefore would not be a reasonable thing to do. Instead a new parallel version would be added that does not guarantee the order in which items are processed. IMHO.

Michael Taylor - 9/8/06

TaylorMichaelL

Where in the specification for Array.ForEach (http://msdn2.microsoft.com/en-us/library/zecdkyw2.aspx) does it say that the GetEnumerator method is used ? All it says is that "The elements of array

James Curran

Shakster,
if your array was created with some specific encoding (e.g. ASCII) then you could simply retrieve the string using the same encoding and replace your 0x00 values later (if you still need to do so). 0x00 does have a string representation, in ASCII, and that's '\0'.

For instance:

byte [] mybytes = { 0x30, 0x31, 0x32, 0x33, 0x00, 0x30, 0x31, 0x32, 0x00, 0x30, 0x31, 0x32 };
string result = Encoding.ASCII.GetString (mybytes).Replace ('\0', ' ');

I'm not claiming this is more efficient than what has been advised before... just slightly shorter to write.

HTH
--mc

Mario Cossi
reply 6

You can use google to search for other answers

 

More Articles

• Need Help in developing MS WORD like application
• DataGrid/DataGridView
• Printing without displaying the PrintDialog
• Progromatically Get Server Name for Connection String
• read probing path from dll.config file
• compact framework data types with symbian data types
• How to load a textbox?
• Please read This!!! Buttons question
• ~ operator
• Lock "controlBox" for userinput
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Merging MSSQL Databases in C#
• c# printing scale
• MFC Control on c# WinForm
• Caret Position SelectionStart, Simple?
• enums, ints and strings
• General Questions ADO ole...
• Search a big generic list for an occuren
• How can I get the current position record?
• Sql Distinct
• classname[from Metadata] file
• Forcing VS2K5 not to use partial classes
• how to catch all exceptions with a singl
• The best way
• intellisense not working when writing ge
• Web Page using C#

Hot Articles

• Project Creation failed error
• TaskBar Context Menu
• Chellange for genious, I want to Find ou
• How do I write text to an RTF Text Box
• I Need A Smple For Receiving SMS In Side
• Speed comparison: Filestream.Seek vs. St
• I would like to display the same ListVie
• Microsoft Configuration
• Library help: a set class needed
• How to update a combobox that contains o
• web forms or html forms
• Office, Windows Media Player components
• Get Random Row From Table
• Is this code thread safe?
• Custom dictionary keys identity

Recommend Articles

• MessageBox.Show() - specify location of
• Implement "file download" in C#
• DataView.RowFilter Problem
• IVsaSite vs ICodeCompiler
• File Upload with restricted extensions n
• Math.Round issue
• How to programmatically set FocusedItem
• Image Verification for asp.net in C# - r
• Application settings.
• /Wp64 switch + /we<xxxx> generates
• Write strings into a richTextBox
• Client is closed in brutal.
• Problem whene trying to use ASP.NET Conf
• C# collision detection using direct X an
• Regex.Matches question....