Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将12个字符转换为长字符_Java_Android_Emv - Fatal编程技术网

Java 将12个字符转换为长字符

Java 将12个字符转换为长字符,java,android,emv,Java,Android,Emv,交易的授权金额。格式为“n 12”,长度为6字节,例如,1234567的值存储为十六进制00 00 01 23 45 67 TLV > 9F02 06 000001234567 Q:如何将000001234567转换为1234567 我尝试了以下方法,但不起作用: public static long byteArrayToLong(@NonNull byte[] from, int offset, @NonNull EEndian endian) { try {

交易的授权金额。格式为“n 12”,长度为6字节,例如,1234567的值存储为十六进制00 00 01 23 45 67

TLV > 9F02 06 000001234567
Q:如何将000001234567转换为1234567

我尝试了以下方法,但不起作用:

public static long byteArrayToLong(@NonNull byte[] from, int offset, @NonNull EEndian endian) {
    try {
        byte[] fromFixed = new byte[8];
        if(from.length < 8) {
            System.arraycopy(from, 0, fromFixed, fromFixed.length-from.length, from.length);
        }
        else  {
            System.arraycopy(from, 0, fromFixed, 0, fromFixed.length);
        }
        if (endian == EEndian.BIG_ENDIAN) {
            return ((fromFixed[offset] << 24) & 0xff00000000000000L) | ((fromFixed[offset + 1] << 16) & 0xff000000000000L)
                    | ((fromFixed[offset + 2] << 8) & 0xff0000000000L) | ((fromFixed[offset + 3]) & 0xff00000000L)
                    | ((fromFixed[offset + 4] << 24) & 0xff000000) | ((fromFixed[offset + 5] << 16) & 0xff0000)
                    | ((fromFixed[offset + 6] << 8) & 0xff00) | (fromFixed[offset + 7] & 0xff);
        } else {
            return ((fromFixed[offset + 7] << 24) & 0xff00000000000000L) | ((fromFixed[offset + 6] << 16) & 0xff000000000000L)
                    | ((fromFixed[offset + 5] << 8) & 0xff0000000000L) | ((fromFixed[offset + 4]) & 0xff00000000L)
                    | ((fromFixed[offset + 3] << 24) & 0xff000000) | ((fromFixed[offset + 2] << 16) & 0xff0000)
                    | ((fromFixed[offset + 1] << 8) & 0xff00) | (fromFixed[offset] & 0xff);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return -1;
}
我建议使用。

import java.math.BigInteger;

/**
 *
 * @author Sedrick
 */
public class Main
{

    public static void main(String[] args)
    {
        String value = "000001234567";
        BigInteger bigInteger = new BigInteger(value);
        System.out.println(bigInteger.longValue());
    }

}
/**
 *
 * @author Sedrick
 */
public class Main
{

    public static void main(String[] args)
    {
        String value = "000001234567";

        System.out.println(Long.parseLong(value));
    }

}
输出:

--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaTestingGround ---
1234567
------------------------------------------------------------------------
BUILD SUCCESS
1234567
------------------------------------------------------------------------
BUILD SUCCESS
另一个路由用于解析值。

import java.math.BigInteger;

/**
 *
 * @author Sedrick
 */
public class Main
{

    public static void main(String[] args)
    {
        String value = "000001234567";
        BigInteger bigInteger = new BigInteger(value);
        System.out.println(bigInteger.longValue());
    }

}
/**
 *
 * @author Sedrick
 */
public class Main
{

    public static void main(String[] args)
    {
        String value = "000001234567";

        System.out.println(Long.parseLong(value));
    }

}
输出:

--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaTestingGround ---
1234567
------------------------------------------------------------------------
BUILD SUCCESS
1234567
------------------------------------------------------------------------
BUILD SUCCESS
我建议使用。

import java.math.BigInteger;

/**
 *
 * @author Sedrick
 */
public class Main
{

    public static void main(String[] args)
    {
        String value = "000001234567";
        BigInteger bigInteger = new BigInteger(value);
        System.out.println(bigInteger.longValue());
    }

}
/**
 *
 * @author Sedrick
 */
public class Main
{

    public static void main(String[] args)
    {
        String value = "000001234567";

        System.out.println(Long.parseLong(value));
    }

}
输出:

--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaTestingGround ---
1234567
------------------------------------------------------------------------
BUILD SUCCESS
1234567
------------------------------------------------------------------------
BUILD SUCCESS
另一个路由用于解析值。

import java.math.BigInteger;

/**
 *
 * @author Sedrick
 */
public class Main
{

    public static void main(String[] args)
    {
        String value = "000001234567";
        BigInteger bigInteger = new BigInteger(value);
        System.out.println(bigInteger.longValue());
    }

}
/**
 *
 * @author Sedrick
 */
public class Main
{

    public static void main(String[] args)
    {
        String value = "000001234567";

        System.out.println(Long.parseLong(value));
    }

}
输出:

--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaTestingGround ---
1234567
------------------------------------------------------------------------
BUILD SUCCESS
1234567
------------------------------------------------------------------------
BUILD SUCCESS

您可以使用BigInteger吗?您正在将十六进制表示为
,它可以将索引转换为
19088743
。(
19088743
1234567
的十六进制十进制表示形式)。我尝试了新的BigInteger(“000001234567”,16)。toString(),结果是19088743@Giovanni对的谢谢我把基数从16改为10,它有效。你能用BigInteger吗?你把十六进制写成
long
,它可以转换成
19088743
。(
19088743
1234567
的十六进制十进制表示形式)。我尝试了新的BigInteger(“000001234567”,16)。toString(),结果是19088743@Giovanni对的谢谢我把基数从16改为10,即使每一个数字都是9,这也很小,可以很容易地放在一个长的数字里。我同意。更新答案。即使每个数字都是9,也足够小,可以很容易地放在一个长的数字内。我同意。更新答案。