473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

String formatting question

How would you do the following in C#?

sprintf(szBuff, "MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);

assuming that MACData had previously been defined as: unsigned char
MACData[6];
Nov 17 '05 #1
2 14559
The easiest way to do this would be with the string.Format() method, for the
first argument we will use the string
"{0:X2}-{1:X2}-{2:X2}-{3:X2}-{4:X2}-{5:X2}". Similar to the sprintf method, #
to the right of the : indicates the argument/value used for that location,
while the capitol X specifies that we want a hexadecimal value that is
capitalized, and the 2 indicates the minimum length so that we get 0 padding.

One issue you may run into with this is that C# does not have an unsigned
char type, often for such a thing the byte works the best.

In the end, the code you are looking for would be:

string szBuff = string.Format("{0:X2}-{1:X2}-{2:X2}-{3:X2}-{4:X2}-{5:X2}",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);

Brendan

"DanC" wrote:
How would you do the following in C#?

sprintf(szBuff, "MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);

assuming that MACData had previously been defined as: unsigned char
MACData[6];

Nov 17 '05 #2
First, I think you meant,

byte[] MACData = new byte[6];

There is no unsigned char in C#. A byte is equivalent.

Based on this, you might do something like,

string pleaseNoHungarian =
string.Format("{0:x2}-{1:x2}-{2:x2}-{3:x2}-{4:x2}-{5:x2}", MACData[0],
MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);

Frisky

---
Here was my complete sample in Snipet Compiler...

public static void Main()
{
byte[] MACData = new byte[6];
MACData[0] = (byte) 'A';
MACData[1] = (byte) 'B';
MACData[2] = (byte) 'C';
MACData[3] = (byte) 'D';
MACData[4] = (byte) 'E';
MACData[5] = (byte) 'F';

string pleaseNoHungarian =
string.Format("{0:x2}-{1:x2}-{2:x2}-{3:x2}-{4:x2}-{5:x2}", MACData[0],
MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
Console.WriteLine(pleaseNoHungarian);
}

"DanC" <Da**@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
How would you do the following in C#?

sprintf(szBuff, "MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);

assuming that MACData had previously been defined as: unsigned char
MACData[6];

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Oliver S. | last post by:
I've developed a string-class that holds the string in an array which is a member-variable of the class and that has maximum-size which is con- figurable through a template-parameter. If any...
7
by: Roger Helliwell | last post by:
Hello Everyone, Has anyone found a quick reference for the many string formatting options for string.Format() ? I'm spending a ridiculous amount of time trying to write one line of code that...
2
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have a string, it could look like this 455 56 Gothenburg or 45556 Gothenburg What i want to do is to separate the numbers from the text into two different strings and the numbers i...
5
by: Burak | last post by:
Hello, I would like to format the string "11304200" into "11-3042.00". Can I do this with String.Format method? I have not come across any good documentation. Thank you,
17
by: Modica82 | last post by:
Hi All, I am very new to web services, so am struggling to understand exactly what I am doing with regards to its returns etc. I have been given the task of designing and developing a web...
7
by: Steven D'Aprano | last post by:
I have a sinking feeling I'm missing something really, really simple. I'm looking for a format string similar to '%.3f' except that trailing zeroes are not included. To give some examples: ...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
11
by: Dustan | last post by:
Is there any builtin function or module with a function similar to my made-up, not-written deformat function as follows? I can't imagine it would be too easy to write, but possible... 'I am...
14
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my...
1
by: Rob | last post by:
Hey folks, I should be ashamed for not knowing the answer to this question - but I don't get the opportunity to program all that much anymore... I have a data tracking program used by over 150...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.