Exploring Java Long toUnsignedString Method

Beginner

Introduction

In this lab, we will learn about the toUnsignedString(long,int) method of the Long class in Java. This method is used to return the unsigned integer value of the long value passed as an argument in the base(radix) passed as a String.

Import the java.lang.Long package and create the main method

import java.lang.Long;

public class LongToUnsignedString {
    public static void main(String[] args) {
        // code here
    }
}

Define and initialize some long and integer values

    long a = -78L;
    int b = 78;
    int d = 10;
    int h = 16;
    int o = 8;

Convert the long values to unsigned String representations using the toUnsignedString() method

    String s1 = Long.toUnsignedString(a,d);
    String s2 = Long.toUnsignedString(a,h);
    String s3 = Long.toUnsignedString(a, o);
    String s4 = Long.toUnsignedString(b, d);
    String s5 = Long.toUnsignedString(b, h);
    String s6 = Long.toUnsignedString(b, o);

Display the unsigned String representations

    System.out.println("Equivalent String Value = " + s1);
    System.out.println("Equivalent String Value = " + s2);
    System.out.println("Equivalent String Value = " + s3);
    System.out.println("Equivalent String Value = " + s4);
    System.out.println("Equivalent String Value = " + s5);
    System.out.println("Equivalent String Value = " + s6);

Compile and run the program

javac LongToUnsignedString.java && java LongToUnsignedString

The output should be:

    Equivalent String Value = 18446744073709551538
    Equivalent String Value = ffffffffffffffb2
    Equivalent String Value = 1777777777777777777662
    Equivalent String Value = 78
    Equivalent String Value = 4e
    Equivalent String Value = 116
  1. In the next step, we will create a program that allows the user to input their own value and base to convert it to an unsigned String.

Modify the main method

    public static void main(String[] args) {
        try {
            System.out.println("Enter the value and base:");
            Scanner sc = new Scanner(System.in);
            long val = sc.nextLong();
            int b = sc.nextInt();
            System.out.println("Output: " + Long.toUnsignedString(val, b));
        }
        catch(Exception e) {
            System.out.println("Invalid Input!!");
        }
    }

Compile and run the program

javac LongToUnsignedString.java && java LongToUnsignedString

The output should be:

    Enter the value and base:
    7445 8
    Output: 16425

When prompted, the user should enter a value and the base in order to convert it to an unsigned String.

Summary

In this lab, we learned about the toUnsignedString(long,int) method of the Long class in Java. We created two Java programs: one that used this method to convert long values to their equivalent unsigned String representations based on the radix, and another that allowed the user to input their own value and base to convert it to an unsigned String. By completing this lab, we gained a better understanding of the Long class and its methods.

Other Tutorials you may like