MainGeneral Discussionrefer to building your own video game controller >

refer to building your own video game controller
 Hi,

there is one sentence on bytetohex converison on

builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));

can anyone tell me what does it suppose to mean? As I used this, my displayed number is appeared to have "3" in the beginning of every number.

Thanks

 agnes - December 23, 2009 03:30am

Re: Game Controller
 Hi agnes.

This line converts each byte in the comByte array to a string. It first makes sure that the string is at least 2 characters and appends an extra zero to the end if it's not. Then, it makes sure that the string is at least 3 characters and appends an extra space to the end if it's not. The result would look something like this:

A2 FF E1 ...

However, there are other ways of doing it. Here are 2 other examples:

public static string ByteToHex(byte[] comByte)
{
StringBuilder hex = new StringBuilder(comByte.Length * 2);
foreach (byte data in comByte)
hex.AppendFormat("{0:x2}", data);
return hex.ToString();
}

or

public static string ByteToHex(byte[] comByte)
{
string hex = BitConverter.ToString(comByte);
return hex.Replace("-","");
}

 ProxyProdigy - December 31, 2009 10:50pm



 You must be logged in to post comments.

 Not a member?

 SIGN-UP NOW
 Registration is free and easy!
   Log In

 Already a member? Login here.

User Name: 
Password: 
 
 
Forgot: Username | Password