如何将字符串转换为字节以传递到 byteArray 中



我正在尝试从颜色选择器更改灯泡的颜色。

这是我的代码

ColorPickerDialogBuilder
.with(this)
.setTitle("Choose color")
.initialColor(R.color.colorPrimary)
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
.density(12)
.setOnColorSelectedListener(new OnColorSelectedListener() {
@Override
public void onColorSelected(int selectedColor) {
Toast.makeText(mBluetoothLeService, ""+Integer.toHexString(selectedColor), Toast.LENGTH_SHORT).show();
String abc = Integer.toHexString(selectedColor);
String aa = "7f";
byte bb = 0x & 7f;
String[] split = abc.split("");

BluetoothGattService Service = bluetoothGatt.getService(UUID.fromString("0000ffd5-0000-1000-8000-00805f9b34fb"));
BluetoothGattCharacteristic charac = Service.getCharacteristic(UUID.fromString("0000ffd9-0000-1000-8000-00805f9b34fb"));
byte[] byteArray1 = { 0x56,  0xff, (byte) 0x00, (byte) 0x2c, 0x00, (byte) 0xf0, (byte) 0xaa};
charac.setValue(byteArray1);
}
})
.setPositiveButton("ok", new ColorPickerClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
//changeBackgroundColor(selectedColor);
// byte byte1 =
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.build()
.show();

在onColorSelected方法中,我得到了一个所选颜色的十六进制字符串,例如"7ff002c".我想像"ff","00","2c"一样拆分这个字符串

现在如您所见,代码下方有一个字节数组

我想将此字符串合并到 byteArray 的值中,例如

byte[] byteArray1 = { 0x56,  0x"ff", (byte) 0x"00", (byte) 0x"2c", 0x00, (byte) 0xf0, (byte) 0xaa}; //note the inverted comma's

我怎样才能 this.is 这个逻辑可行? 你还有其他逻辑吗?

请帮忙

像这样使用:byte[] b = string.getBytes();

最新更新