How to convert signed byte array to ascii in Java

NRaj

In Java program I have signed byte array as

[-112, 21, 64, 0, 7, 50, 54, 127]

how i can convert into ascii number which is equal to

901540000732367F
Alex Rudenko

It seems that the order of bytes in the result is reverse to that of the array, so you should iterate the array in the reverse order and add each element with a shift by the predefined number of bits:

private static String convertToHexFullByte(byte[] arr) {
    return convertToHex(arr, 8);
}
    
private static String convertToHexHalfByte(byte[] arr) {
    return convertToHex(arr, 4);
}
    
private static String convertToHex(byte[] arr, int bits) {
    long mask = 0;
    for (int i = 0; i < bits; i++) {
        mask |= 1 << i;
    }
    long res = 0;
    for (int i = arr.length - 1, j = 0; i >= 0; i--, j += bits) {
        res |= (arr[i] & mask) << j;
    }

    return Long.toHexString(res).toUpperCase();        
}

Test

public static void main(String args[]) {
    byte[] arr4 = {49, 55, 48, 51, 55};
        
    System.out.println(convertToHexHalfByte(arr4));
        
    byte[] arr8 = {-112, 21, 64, 0, 7, 50, 54, 127};
        
    System.out.println(convertToHexFullByte(arr8));
}

output

17037
901540000732367F

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java DataInputStream convert to byte array?

How to convert array byte to string in java

Convert ByteBuffer to byte array java

How to convert a Scala Array[Byte] to Java byte[]?

how to convert image to byte array in java?

Convert integer into byte array (Java)

Convert InputStream to byte array in Java

How to convert a Java String to an ASCII byte array?

How to Convert byte array to hexString in java?

How to convert array of byte to String in Java?

How to convert a String array to a Byte array? (java)

How to convert a byte array to a hex string in Java?

Convert a StringBuffer to a byte Array in Java

Convert Java string to byte array

In Java, how can I convert an InputStream into a byte array (byte[])?

How do I convert a Java byte array into a Scala byte array?

Convert unsigned byte to signed byte

Convert string representation of a hexadecimal byte array to a string with non ascii characters in Java

How to convert byte array to Mat object in Java

How BigInteger convert a byte array to number in Java?

Convert byte array (hex) to signed Int

byte array convert to ascii string is garbled

Java convert Byte Array of ASCII values to int

How to convert an integer value into 'n' digit ascii byte array in java

Convert the byte array to java types

How to convert string to ascii byte array in php

how to convert a hex value from a byte array as an interpreted ASCII number into an integer?

Convert byte array (signed bytes) to int array of corresponding unsigned bytes

How to convert array of 16-bit signed integers to byte array in Java?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive