简体   繁体   English

将十六进制字符串转换为字节而不是字节数组

[英]convert hex string to byte not byte array

i needed to convert hex which i get from color code 我需要转换从颜色代码获得的十六进制

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);

log generated is........ 0xb30x120xff 生成的日志是........ 0xb30x120xff

which is completed i wanted 我想要完成的

but i want to convert this in byte and then join them as byte array like this which is working completely ok 但是我想将其转换为字节然后像这样将它们作为字节数组加入其中,这完全可以工作

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

my question is how to convert this string to like this so i can send data.... 我的问题是如何将此字符串转换为这样,以便我可以发送数据。...

byte byteRed = Byte.valueOf(hexRed);

this is not working number format exception also tried other solutions which are not working 这不是工作号码格式异常也尝试了其他不起作用的解决方案

advance thx for help 提前寻求帮助

You can only convert Strings to a byte array and vice versa. 您只能将字符串转换为字节数组,反之亦然。

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

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

Google "convert String to byte" and you will find plenty on converting string to byte[] Google“将字符串转换为字节”,您会发现很多将字符串转换为字节[]的方法

http://www.mkyong.com/java/how-do-convert-string-to-byte-in-java/ 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