locked
How to Get Hex string with leading zeros RRS feed

  • Question

  • Hi all , 

    Can any one help me to convert hex string  to Hex string with leading Zero's .

    Suppose i have a character string as :-

    Rawdata = 3b5b4b;

    I want the output as "003b5b4b"

    My condition is if hex string is less then 8 char's then it should have leading zero's 

    Thsnks in Advance 

    Vaibhav


    Vaibhav Mishra

    Friday, February 15, 2013 11:51 AM

Answers

  • In case of MFC try this:

        CString s1 = _T("3b5b4b");

        CString s2;

        s2.Format(_T("%08s"), s1);

    Otherwise give details.

    • Proposed as answer by Elegentin Xie Wednesday, February 20, 2013 7:10 AM
    • Marked as answer by Elegentin Xie Monday, February 25, 2013 2:34 AM
    Friday, February 15, 2013 6:03 PM
  • The C function printf also uses width and fill specifications
    like the CString function in MFC:

    char Rawdata[] = "3b5b4b";
    printf("%s\n", Rawdata);
    printf("%08s\n", Rawdata);

    - Wayne
    • Proposed as answer by Elegentin Xie Wednesday, February 20, 2013 7:10 AM
    • Marked as answer by Elegentin Xie Monday, February 25, 2013 2:34 AM
    Friday, February 15, 2013 7:58 PM

All replies

  • In case of STL and outputting strings to screen try this:

        char * s = "3b5b4b";

        cout << setw(8) << setfill('0') << s << endl;

    Otherwise give details.

    • Edited by Viorel_MVP Friday, February 15, 2013 12:14 PM
    Friday, February 15, 2013 12:13 PM
  • I am not using the STL is there any way available to get my job done 

    Vaibhav Mishra

    Friday, February 15, 2013 5:36 PM
  • In case of MFC try this:

        CString s1 = _T("3b5b4b");

        CString s2;

        s2.Format(_T("%08s"), s1);

    Otherwise give details.

    • Proposed as answer by Elegentin Xie Wednesday, February 20, 2013 7:10 AM
    • Marked as answer by Elegentin Xie Monday, February 25, 2013 2:34 AM
    Friday, February 15, 2013 6:03 PM
  • The C function printf also uses width and fill specifications
    like the CString function in MFC:

    char Rawdata[] = "3b5b4b";
    printf("%s\n", Rawdata);
    printf("%08s\n", Rawdata);

    - Wayne
    • Proposed as answer by Elegentin Xie Wednesday, February 20, 2013 7:10 AM
    • Marked as answer by Elegentin Xie Monday, February 25, 2013 2:34 AM
    Friday, February 15, 2013 7:58 PM