Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:51:17+00:00 2026-06-13T16:51:17+00:00

I am a C++ developer and shifted to wpf last week. Well I have

  • 0

I am a C++ developer and shifted to wpf last week. Well I have used sprintf in my c++ apps many time and now I have across the need of having something equivalent to that in C#. Here is what I had done is C++:

char t_str[4] = {};
for(int i = 0; i < 4; i++)
{ 
    sprintf(t_str, "%02X", buffer[i]);
m_apiResponse[i]->setText(String(t_str));
}

where buffer[0] = 20;
buffer[1] = 30;
buffer[2] = 40;
buffer[3] = 50;
and m_apiResponse is a textbox.

I had done this in my C# as follows:

Byte[] t_str = new Byte[4];
for (int i = 0; i < 4; i++)
{
         string bufstring = String.Format("{0:02}", buffer[i]);
         t_str = Encoding.UTF8.GetBytes(bufstring);
}

Response1Box = Convert.ToString(t_str[0]);
Response2Box = Convert.ToString(t_str[1]);
Response3Box = Convert.ToString(t_str[2]);
Response4Box = Convert.ToString(t_str[3]);

where ResposeBox is a textbox which I have bind to:

// Description of Response1Box
    private string _Response1Box;
    public string Response1Box
    {
        get
        {
            return _Response1Box;
        }

        set
        {
            _Response1Box = value;
            OnPropertyChanged("Response1Box");
        }
    }

buffer[64] is byte[].

Its not giving me the expected answer which my C++ method does. Is this the right way to do: string bufstring = String.Format("{0:02}", buffer[i]);
t_str = Encoding.UTF8.GetBytes(bufstring);
Where am i wrong???

Please help 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:51:18+00:00Added an answer on June 13, 2026 at 4:51 pm

    Try with

    string bufstring = String.Format("{0:D2}", buffer[i]);
    

    or (for hex output)

    string bufstring = String.Format("{0:X2}", buffer[i]);
    

    or (with C/C++ style hex prefix)

    string bufstring = String.Format("0x{0:X2}", buffer[i]);
    

    See Composite Formatting and Standard Numeric Format strings

    Of course, inside the C++ loop you call the m_apiResponse[i]->setText, you should do something similar in the C# loop to achieve the same result.

    string[] response = new TextBox[] {Response1Box, Response2Box, Response3Box, Response4Box};
    for (int i = 0; i < 4; i++)
         response[i] = String.Format("{0:X2}", buffer[i]);
    

    UPDATE: It is not clear to me what ResponseBox is, but if you have 4 string properties named Response[X]Box inside an object instance called ResponseBox then you could set these properties outside the loop in this way

    ResponseBox.Response1Box = response[0];
    ResponseBox.Response2Box = response[1];
    .......
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a C++ Developer and have recently shifted to C#. I am developing
I am developing a WPF application. I am basically a C++ developer and have
I am interested in C# windows application development. But now that I have shifted
I am a .Net developer but recently shifted to PHP world. I have worked
I am a c++ developer and recently shifted to this amazing WPF world. I
I am a C++ developer and i recently shifted to C# WPF. I am
I am a C++ developer and recently shifted to the world of WPF C#.
http://developer.android.com/tools/publishing/app-signing.html I have a keystore that I already have an alias and password for.
Web Developer here and need some advice on how to achieve what must be
The developer documentation has seemed to have failed me here. I can create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.