'QR Code in Base64 encoding for KSA E-Invoicing In Java [closed]

How to Create HEXA and BASE 64 QR code for E-Invoicing Saudia Arabia. There is problem converting hexa to base64 but that is not readable in E-Invoicing system.



Solution 1:[1]

The Complete Code for this HEXA and BASE64 string to generate QR code.

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import java.nio.charset.StandardCharsets;

public class TestMain {

    public static void main(String args[]) throws DecoderException {
        String tag1 = getHexString(1, "Irfan Nasim");
        String tag2 = getHexString(2, "1234567891");
        String tag3 = getHexString(3, "2021-11-17");
        String tag4 = getHexString(4, "300.00");
        String tag5 = getHexString(5, "75.00");

        String finalString = tag1 + tag2 + tag3 + tag4 + tag5;
        byte[] decodedHex = Hex.decodeHex(finalString.toCharArray());
        String result = Base64.encodeBase64String(decodedHex);
        System.out.println("==> " + result);

    }

    static String getHexString(int tagNo, String tagValue) {
        String tagNumLengthHexString = Integer.toHexString(tagNo);

        int tagValueLength = tagValue.length();
        String tagValueLengthHexString = Integer.toHexString(tagValueLength);

        byte[] tagValueBytes = tagValue.getBytes(StandardCharsets.UTF_8);
        String tagValueHexString = Hex.encodeHexString(tagValueBytes);

        return (0 + tagNumLengthHexString) + (0 + tagValueLengthHexString) + tagValueHexString;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Irfan Nasim