简体   繁体   English

Java字节将值错误字符串插入字节

[英]Java byte Insert value error string to byte

I have class that have one member myByte as follows. 我有一个成员为myByte的类,如下所示。

public class ByteClass {
    private Byte myByte;
    public Byte getMyByte() {
        return myByte;
    }
    public void setMyByte(Byte myByte) {
        this.myByte = myByte;
    }
}

I have string value which is FF and I need to assign it to the class member, how should I do that since when I try it ass follows I got error in the compile time 我有一个字符串值FF ,我需要将其分配给该类成员,我应该怎么做,因为当我尝试它的屁股时,我在编译时出错

Type mismatch: cannot convert from byte[] to byte I understand that I cant use Array of byte for the string but I have tried to do that in several of ways without any success .any Idea how can I set the value FF to the class ? 类型不匹配:无法从byte []转换为byte我知道我不能为字符串使用byte Array,但是我尝试了几种方法,但均未成功。anyidea如何将FF值设置为该类?

public class ByteHanlder {

    public static void main(String[] args) {
        String str = "FF";
        byte temp = str.getBytes();
        Byte write_data = new Byte(temp);
        ByteClass byteClass = new ByteClass();
        byteClass.setMyByte(temp);
        System.out.println(byteClass.getMyByte());
    }
}

Assuming you really do just want to store a byte, you can use: 假设您确实只想存储一个字节,则可以使用:

int value = Integer.parseInt("FF", 16); 
x.setMyByte((byte) value);

Now that will give you a value of -1 if you look at it in the debugger - but that's just because bytes are signed, as other answerers have noted. 现在,如果您在调试器中查看该值,则将获得-1的值-但这仅是因为字节已签名,正如其他答复者所指出的那样。 If you want to see the unsigned value at any time, you can just use: 如果您想随时查看无符号值,则可以使用:

// Only keep the bottom 8 bits within a 32-bit integer, which ends up
// treating the original byte as an unsigned value.
int value = x.getMyByte() & 0xff;

You can still just store a byte - and then interpret it in an unsigned way rather than a signed way. 您仍然可以只存储一个字节-然后以无符号方式而不是有符号方式解释它。 If you really just want to store a single byte - 8 bits - I suggest you don't change to using a short . 如果你真的只想存储一个字节-8位-我建议你不要改用short

Having said all this, it's somewhat odd to need a mutable wrapper type just for a byte... perhaps you could give us more context as to why you want this, and we may be able to suggest cleaner alternatives. 说了这么多,仅需要一个字节就需要一个可变的包装器类型有些奇怪……也许您可以为我们提供更多关于为什么要使用此包装器的背景信息,并且我们可以提出更清洁的替代方法。

Your String "FF" seems to be a hex value, right? 您的字符串“ FF”似乎是一个十六进制值,对吗? So you actually want to convert it to the byte value of 255. 因此,您实际上要将其转换为255的字节值。

You can use a method like described here to convert a hex string to a byte array: Convert a string representation of a hex dump to a byte array using Java? 您可以使用此处所述的方法将十六进制字符串转换为字节数组: 使用Java将十六进制转储的字符串表示形式转换为字节数组? .

In your case, you could adapt that method to expect a 2-letter string and return a single byte instead of a byte array. 在您的情况下,您可以修改该方法以使用2个字母的字符串并返回单个字节而不是字节数组。

String.getBytes return a bytearray so you can not assign a bytearray to a byte. String.getBytes返回一个字节数组,因此您不能将一个字节数组分配给一个字节。

If you want to set FF hex value then you could use 如果要设置FF十六进制值,则可以使用

setMyByte((byte)0xff);

But there is still a problem 0xff is of 2 byte size but byte type is of 1 byte. 但是仍然存在一个问题,0xff的大小为2个字节,而字节类型的大小为1个字节。 In that case you can use short instead of byte 在这种情况下,您可以使用short而不是byte

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM