簡體   English   中英

將十六進制字符串轉換為字節而不是字節數組

[英]convert hex string to byte not byte array

我需要轉換從顏色代碼獲得的十六進制

int color = singleColor.getColor();

                String rgbString = "R: " + Color.red(color) + " B: " + Color.blue(color) + " G: " + Color.green(color);
                String hexRed = "0x" + Integer.toHexString(Color.red(color));
                String hexGreen = "0x" + Integer.toHexString(Color.green(color));
                String hexBlue= "0x" + Integer.toHexString(Color.blue(color));
                Log.e("hex string", hexRed + hexGreen + hexBlue);

生成的日志是........ 0xb30x120xff

我想要完成的

但是我想將其轉換為字節然后像這樣將它們作為字節數組加入其中,這完全可以工作

byte[] hex_txt = {(byte)0xff, (byte)0x00, (byte)0x00};
sendData(hex_txt);

我的問題是如何將此字符串轉換為這樣,以便我可以發送數據。...

byte byteRed = Byte.valueOf(hexRed);

這不是工作號碼格式異常也嘗試了其他不起作用的解決方案

提前尋求幫助

您只能將字符串轉換為字節數組,反之亦然。

        String example = "This is an example";
        byte[] bytes = example.getBytes();

        System.out.println("Text : " + example);
        System.out.println("Text [Byte Format] : " + bytes);

Google“將字符串轉換為字節”,您會發現很多將字符串轉換為字節[]的方法

http://www.mkyong.com/java/how-do-convert-string-to-byte-in-java/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM