byte octal and hexadecimal
In this chapter you will learn:
- Convert byte value to its base 2(Binary), base 8(Octal) and base 16(Hex) forms
- 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:
Home » C# Tutorial » Byte, Integer, Long