Solved

Formatting or Padding Bytes

Posted on 2012-04-11
5
250 Views
Last Modified: 2012-04-11
Hi, I'm using MS Visual Studio C# 2010. I am trying to read a file into a textbox as a Binary String. My code is at the end of this question.

My question is... when I get to bytes that are single digits like "0" or "A", I only get one digit printing out, rather than two. Such as below:

6C 79 2E D A 0 0 0 0 0 0 0 0 0

How should I format or pad my output, so it prints out as show below?

6C 79 2E 0D 0A 00 00 00 00 00 00 00 00 00


Code:
private void btnRead_Click(object sender, EventArgs e)
        {
            string path = @"F:\Mary had a little Lamb.txt";

            if (File.Exists(path))
            {
                Console.WriteLine("The file exists");
                StreamReader sr = new StreamReader(File.OpenRead (path));
                byte[] buffer = new byte[1000];
                sr.BaseStream.Read(buffer, 0, 1000);
                foreach ( byte myByte in buffer)
                {
                    tbxText.Text += myByte.ToString("X") + " ";
                }
             
                sr.Dispose();
            }
            else
            {
                Console.WriteLine("The file does NOT exists");
            }

Open in new window

0
Comment
Question by:Mr_Fulano
[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
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:James Atkin
ID: 37831343
Hi,

Try this:

    tbxText.Text += myByte.ToString("X").PadLeft(2, '0') + " ";

It will pad using the char specified

Hope this helps,

James
0
 
LVL 4

Expert Comment

by:James Atkin
ID: 37831373
Hi,

The PadLeft function actually performs the length test for you. The length specified in the call is the TOTAL length of the resulting string.

i.e.
"AA" would return "AA"
"A" would return "0A"

Regards,

James
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37834150
Try:

tbxText.Text += myByte.ToString("X2") + " ";
0
 

Author Comment

by:Mr_Fulano
ID: 37835626
Thank you both for your great replies. Although James was first to respond, Wdosanjos had the best approach. Thank you again for your help!

Fulano
0
 

Author Closing Comment

by:Mr_Fulano
ID: 37835629
Very good and effective approach.
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

630 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