首页 > 解决方案 > 将 HEX 转换为 UINT32 Little Endian

问题描述

我正在 Android Studio 中编写应用程序,并且必须在 Java 中将十六进制值转换为 UINT32 Little Endian。我怎么做?

这是代码:

public void onNewIntent(Intent intent) {
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            info.setText(bytesToHex(tag.getId()));

            String hex = bytesToHex(tag.getId()).replace(" ", "");

            long value = Long.parseLong(hex, 16);

            ByteBuffer buffer = ByteBuffer.allocate(4);
            buffer.asLongBuffer().put(value);
            buffer.order(ByteOrder.LITTLE_ENDIAN);
            long kort_id = buffer.asLongBuffer().get();

            SharedPreferences pref = getApplicationContext().getSharedPreferences("Storage", 0);
            SharedPreferences.Editor editor = pref.edit();

            editor.putString("KortID", String.valueOf(kort_id));
            editor.putBoolean("initializing", true);
            editor.apply();

            //Intent startMainActivity = new Intent(this, MainActivity.class);
            //startActivity(startMainActivity);
            //finish();
        }

运行时出现 BufferOverflowException 错误。

标签: javaandroid-studio

解决方案


推荐阅读