Solved

Looking for information on how to do string Formatting

Posted on 2004-08-25
9
649 Views
Last Modified: 2010-04-24
Hey everybody;

I am new to the whole .Net environment. Up until now I have been using the formatting scheme that goes with printf and the like. However I guess in .net the whole scheme that follows with String::Format, etc has changed, I have figured out that istead of using the % character you use {} with a number designation. However I have been able to find almost nothing on beyond just the basics. In particular I want to pad a String holding a MacAddress with zeros if the particular portion of the address has only one character for example

String* macAddress[] = new String*[6];

            macAddress[0] = Convert::ToString(myDevInfo.mac_address[0],16);
            macAddress[1] = Convert::ToString(myDevInfo.mac_address[1],16);
            macAddress[2] = Convert::ToString(myDevInfo.mac_address[2],16);
            macAddress[3] = Convert::ToString(myDevInfo.mac_address[3],16);
            macAddress[4] = Convert::ToString(myDevInfo.mac_address[4],16);
            macAddress[5] = Convert::ToString(myDevInfo.mac_address[5],16);
            
            String* macAddressCmp = String::Format("{0}:{1}:{2}:{3}:{4}:{5}",macAddress);

This works but if say macAddress[0] were "0" then the address would show up as 0:40:9d:24:1a:a8 instead of 00:40:9d:24:1a:a8 can anyone recommend a resource that has information about the syntax for padding, or spacing, etc. I would greatly appreciate it
0
Comment
Question by:komeisa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 100 total points
ID: 11897062
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11897128
I think you need something like:

String* macAddressCmp = String::Format("{0:00}:{1:00}:{2:00}:{3:00}:{4:00}:{5:00}",macAddress);
0
 

Author Comment

by:komeisa
ID: 11897651
Hey james


I went there, but am having finding things on formatting string format strings. They have lots of things for numeric format strings, and date/time format strings. But what about format strings that deal string objects.
0
[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11899240
What about this:

System::Text::StringBuilder* mac = new System::Text::StringBuilder();
for (int=0; i<6; i++) {
    mac->Append(Convert::ToString(myDevInfo.mac_address[0],16).PadLeft(2,'0'));
    if (i<5)
           mac->Append(S":");
}

Or,

String* macAddressCmp = String::Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}",
myDevInfo.mac_address[0],myDevInfo.mac_address[1],myDevInfo.mac_address[2],
myDevInfo.mac_address[3],myDevInfo.mac_address[4],myDevInfo.mac_address[5]);


0
 

Author Comment

by:komeisa
ID: 11904569
Hey james:

I Tried the String::Format function you supplied. however I can't seem to get it to compile. If I try it like it is above I get this error

error C2661: 'System::String::Format' : no overloaded function takes 7 arguments
//corresponding Code
      Byte myDevInfo[] = new Byte[6];

      myDevInfo[0] = 0x00;
      myDevInfo[1] = 0x40;
      myDevInfo[2] = 0x9d;
      myDevInfo[3] = 0x24;
      myDevInfo[4] = 0x1a;
      myDevInfo[5] = 0xa8;

      String* macAddressCmp = String::Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}",
            myDevInfo[0],myDevInfo[1],myDevInfo[2],myDevInfo[3],myDevInfo[4],myDevInfo[5]);

So I tried submitting just the array but I get this runtime error
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
//corresponding Code
      Byte myDevInfo[] = new Byte[6];

      myDevInfo[0] = 0x00;
      myDevInfo[1] = 0x40;
      myDevInfo[2] = 0x9d;
      myDevInfo[3] = 0x24;
      myDevInfo[4] = 0x1a;
      myDevInfo[5] = 0xa8;

      String* macAddressCmp = String::Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}",myDevInfo);

So I tried just for kicks to do just one variable but get this error
error C2665: 'System::String::Format' : none of the 5 overloads can convert parameter 2 from type 'unsigned char'
//corresponding code
      Byte myDevInfo[] = new Byte[6];

      myDevInfo[0] = 0x00;
      myDevInfo[1] = 0x40;
      myDevInfo[2] = 0x9d;
      myDevInfo[3] = 0x24;
      myDevInfo[4] = 0x1a;
      myDevInfo[5] = 0xa8;

      String* macAddressCmp = String::Format("{0:X2}",myDevInfo[0]);

I was hoping that you could shed some light onto this problem Any information would be incredibly helpful
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11904632
Sorry, I haven't a VC++.net right now in my office to test. I was writing from my memory (bad memory, I see). Sometimes it is easier to use those old-C functions like:

char macAdr[30];  // Reserve enough space
sprintf(macAddr, "%02X:%02X:%02X:%02X:%02X:%02X", myDevInfo.mac_address[0], myDevInfo.mac_address[1],myDevInfo.mac_address[2], myDevInfo.mac_address[3], myDevInfo.mac_address[4], myDevInfo.mac_address[5]);

String *macAddressCmp = macAddr;
0
 

Author Comment

by:komeisa
ID: 11904820
I hear you there

I guess to narrow the question feild I was wondering why the String::Format function cannot convert from type Byte or unsigned char. That is really the heart of the problem. I think if I could get the Format function to accept type Byte then I could use the {0:X2} but I cannot get it to accept anything beyond strings. I tried int, UInt32, Int32, etc, and it won't accept anything but strings. Any advice would be greatly appreciated.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11905469
try with Int16
0
 

Author Comment

by:komeisa
ID: 11905588
error C2665: 'System::String::Format' : none of the 5 overloads can convert parameter 2 from type 'short'

no such luck
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question