简体   繁体   English

将 Long 数转换为字节数组

[英]Converting Long number to byte array

I am trying to convert a long number (convertex from Hex to Long) to a byte array.我正在尝试将长数字(从十六进制转换为长整数)转换为字节数组。 I'm trying the following code:我正在尝试以下代码:

    ByteBuffer b = ByteBuffer.allocate(4);
    // The literal 4328719365 of type int is out of range 
    b.putLong(4328719365);
    byte[] result = b.array();

but it's not compiling due to being out of range for int .但由于超出int范围而无法编译。

What can I do to solve this issue?我能做些什么来解决这个问题?

Suffix L (or l ) converts literal number to a long .后缀L (或l )将文字数字转换为long

So try this:所以试试这个:

b.putLong(4328719365L);

You can use literal long value just like number without L suffix.您可以使用文字 long 值,就像没有 L 后缀的数字一样。 Like assign them to variables:就像将它们分配给变量一样:

long myLongValue = 4328719365L;
b.putLong(myLongValue);

1) Suffix L to the value 2) Increase the ByteBuffer allocated size and try 3) Int will take max of 10 digits and putLong might internally add L and making it beyond 10. Please check reducing digits in number. 1) 为值添加 L 后缀 2) 增加 ByteBuffer 分配的大小并尝试 3) Int 将占用最多 10 位数字,而 putLong 可能会在内部添加 L 并使其超过 10。请检查减少位数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM