[英]String as \x03\x00\x00\x00 to integer
Helo: Helo:
I have a question, that I solved in c, but now I want to do it in hava: I have a String like: '\\x03\\x00\\x00\\x00' This is representing an hexadecimal value of a integer. 我有一个问题,我在c中解决了,但是现在我想在hava中完成它:我有一个像这样的字符串:'\\ x03 \\ x00 \\ x00 \\ x00'这表示一个整数的十六进制值。 I transform to 0x03\\0x00... And now I want to obtain the integer, but I don't know how to do it in java
我转换为0x03 \\ 0x00 ...现在我想获取整数,但是我不知道如何在Java中进行操作
could someone give me some idea ? 有人可以给我一些想法吗?
Thanks in advance 提前致谢
(Is it some way to use this format ('\\x03\\x00\\x00\\x00' ) directly without use byte[] arrays? and in C can I use this format directly to build a integer (int)?) (是否可以通过某种方式直接使用此格式('\\ x03 \\ x00 \\ x00 \\ x00'而不使用byte []数组?在C语言中我可以直接使用此格式来构建整数(int)吗?)
Here is my one line solution 这是我的一线解决方案
Integer.parseInt("\\x03\\x00\\x00\\x00".replaceAll("\\\\x", ""), 16)
Of course, you might wrap this in a function. 当然,您可以将其包装在函数中。
I'd do it is something like this: 我会做这样的事情:
Scanner sc = new Scanner(inputString);
int value = 0;
for (int i = 0; i < 4; i++) {
sc.next("\\\\x");
byte b = sc.nextByte(16);
value = (value << 8) | (b & 0xff);
}
if (sc.hasNext()) {
throw new SomeException("Unexpected stuff at the end");
}
( UPDATE - corrected a sign extension bug ... and to detected unexpected stuff at the end of the input string.) ( 更新 -更正了符号扩展错误...,并在输入字符串的末尾检测到意外内容。)
Now, if this encounters input that it can't cope with, it will throw an exception. 现在,如果遇到无法处理的输入,它将引发异常。 In this case it is designed to match a sequence of EXACTLY 4 bytes.
在这种情况下,它被设计为匹配一个完全为4个字节的序列。
But (IMO) that is preferable to accepting the bad input and producing an incorrect result. 但是(IMO)最好接受错误的输入并产生不正确的结果。 That is the risk with clever solutions that use pattern matching tricks to avoid doing proper parsing.
这就是使用模式匹配技巧避免执行正确解析的聪明解决方案的风险。
Thanks very much for the answers I don't understand this solution completely and it give me errors in sc.next("\\\\x");
非常感谢您的回答,我不完全理解此解决方案,它给了我sc.next(“ \\\\ x”);中的错误。
The errors will be because you are using this on a string that has less than 4 bytes in it, or that has something that simply doesn't match the format of your example. 发生错误的原因是,您在少于4个字节的字符串上使用此字符串,或者该字符串与示例格式完全不匹配。 If you want the code to handle sequences of less than (or more than) 4 bytes, you will need to change it.
如果您希望代码处理少于(或大于)4个字节的序列,则需要对其进行更改。
I would like to know why you make:
value=(value <<8) | b
我想知道你为什么做:
value=(value <<8) | b
value=(value <<8) | b
(but i'm working to understand it) thanks very muchvalue=(value <<8) | b
(但是我正在努力理解)非常感谢
Well there was a bug in that. 好吧,那是一个错误。 I corrected it to
我改正为
value = (value << 8) | (b & 0xff);
It is doing some bit-twiddling to tack the byte b
onto the least significant end of the number in value
. 它正在做一些位纠结操作,以将字节
b
固定在value
的数字的最低有效位上。 Read up on the Java <<
shift operator and the bit-wise &
and |
阅读Java
<<
移位运算符和按位&
和|
operators. 操作员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.