Solved

Looking for information on how to do string Formatting

Posted on 2004-08-25
9
636 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
  • 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now