byte octal and hexadecimal

In this chapter you will learn:

  1. Convert byte value to its base 2(Binary), base 8(Octal) and base 16(Hex) forms
  2. Format byte value to its hexadecimal form

Binary, Octal and Hex forms

using System;/*  ja va 2  s  .  c  o m*/

public class Example
{
   public static void Main()
   {

        byte[] numbers ={ 0, 15, 100, 213 };
        Console.WriteLine("{0}   {1,8}   {2,5}   {3,5}","Value", "Binary", "Octal", "Hex");
        foreach (byte number in numbers) {
           Console.WriteLine("{0,5}   {1,8}   {2,5}   {3,5}",
                             number, Convert.ToString(number, 2),
                             Convert.ToString(number, 8),
                             Convert.ToString(number, 16));
        }  
   }
}

Format byte value to its hexadecimal form

using System;//j a  v  a 2 s . c  om

public class Example
{
   public static void Main()
   {

        byte[] numbers = { 0, 16, 104, 213 };
        foreach (byte number in numbers) {
           // Display value with hexadecimal.
           Console.Write(number.ToString("X2") + "   ");

           // Display value with four hexadecimal digits.
           Console.WriteLine(number.ToString("X4"));
        } 
   }
}

Next chapter...

What you will learn in the next chapter:

  1. Byte value binary and operation
Home » C# Tutorial » Byte, Integer, Long
Integer Family
Integer ranges
byte
Convert to byte
Parse string to byte
byte format
byte octal and hexadecimal
byte binary operation
byte overflow with unchecked
int
int value
Compare int value
int calculation
int value overflow
int binary operation
Parse string to int value
Format int value
int in binary, octal and hexadecimal
long
long value calculation
ulong