繁体   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