byte

How to convert binary string to the byte array of 2 bytes in java

此生再无相见时 提交于 2019-12-10 13:20:34
问题 I have binary string String A = "1000000110101110" . I want to convert this string into byte array of length 2 in java I have taken the help of this link I have tried to convert it into byte by various ways I have converted that string into decimal first and then apply the code to store into the byte array int aInt = Integer.parseInt(A, 2); byte[] xByte = new byte[2]; xByte[0] = (byte) ((aInt >> 8) & 0XFF); xByte[1] = (byte) (aInt & 0XFF); System.arraycopy(xByte, 0, record, 0, xByte.length);

Java Byte.parseByte() error

╄→尐↘猪︶ㄣ 提交于 2019-12-10 13:06:19
问题 I'm having a small error in my code that I can not for the life of me figure out. I have an array of strings that are representations of binary data (after converting them from hex) for example: one index is 1011 and another is 11100. I go through the array and pad each index with 0's so that each index is eight bytes. When I try to convert these representations into actual bytes I get an error when I try to parse '11111111' The error I get is: java.lang.NumberFormatException: Value out of

Fast string to byte[] conversion

自古美人都是妖i 提交于 2019-12-10 12:53:32
问题 Currently I am using this code for converting string to byte array: var tempByte = System.Text.Encoding.UTF8.GetBytes(tempText); I call this line very often in my application, and I really want to use a faster one. How can I convert a string to a byte array faster than the default GetBytes method? Maybe with an unsafe code? 回答1: If you don't care too much about using specific encoding and your code is performance-critical (for instance it's some kind of DB serializer and needs to be run

parse http response bytes in java

只愿长相守 提交于 2019-12-10 12:49:15
问题 I'm trying to parse a byte[] in java, which is a representation of an HTTP response. There is this question Is there any simple http response parser for Java?, which is exactly my question, but the accepted answer doesn't help me. If I look at http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/io/HttpMessageParser.html, I do not understand how this will help me. 回答1: I hope this should get you started String s = "HTTP/1.1 200 OK\r\n" + "Content-Length: 100\r\n" +

getting bytes from unicode string in python

本秂侑毒 提交于 2019-12-10 12:44:53
问题 I have an 16bit big endian unicode string represented as u'\u4132' , how can I split it into integers 41 and 32 in python ? 回答1: Here are a variety of different ways you may want it. Python 2: >>> chars = u'\u4132'.encode('utf-16be') >>> chars 'A2' >>> ord(chars[0]) 65 >>> '%x' % ord(chars[0]) '41' >>> hex(ord(chars[0])) '0x41' >>> ['%x' % ord(c) for c in chars] ['41', '32'] >>> [hex(ord(c)) for c in chars] ['0x41', '0x32'] Python 3: >>> chars = '\u4132'.encode('utf-16be') >>> chars b'A2' >>>

Converting kilobytes, megabytes etc. to bytes in R

戏子无情 提交于 2019-12-10 12:41:53
问题 Is there a standard function in R to convert strings representing numbers of bytes such as 11855276K 113M 2.40G to integer numbers of bytes? I came across humanReadable in the package gdata, but this does the conversion the other way round. I know that I can parse the string and then do the maths myself, but I wondered whether something exists already. 回答1: A simple function to do this: x <- c("11855276K", "113M", "2.40G", "1234") convb <- function(x){ ptn <- "(\\d*(.\\d+)*)(.*)" num <- as

Using char as an unsigned 16 bit value in Java?

こ雲淡風輕ζ 提交于 2019-12-10 11:37:06
问题 I need an unsigned 8 bit integer in Java, and char seems to be the only thing close to that. Although it's double the size, it's unsigned which makes it practical for what I want to use it for (writing a basic emulator which requires unsigned bytes). The problem is that I've heard other programmers say that one shouldn't use char in that manner and should just use int or so. Is this true, and why so? 回答1: If you need an unsigned 8 bit integer then use byte . It's easy to make it unsigned in

Java byte to string

て烟熏妆下的殇ゞ 提交于 2019-12-10 11:33:31
问题 So I want to convert a byte array returned by the DatagramPacket's getData() function into a string. I know the way to convert the whole byte array to string is just: String str = new String(bytes); However, this prints out null characters at the end. So if the byte array was [114, 101, 113, 117, 101, 115, 116, 0, 0, 0] The 0's print out empty boxes on the console. So I basically only want to print out: [114, 101, 113, 117, 101, 115, 116] So I made this function: public void print

String to binary and vice versa: extended ASCII

隐身守侯 提交于 2019-12-10 11:28:57
问题 I want to convert a String to binary by putting it in a byte array ( String.getBytes[] ) and then store the binary string for each byte ( Integer.toBinaryString(bytearray) ) in a String[]. Then I want to convert back to normal String via Byte.parseByte(stringarray[i], 2) . This works great for standard ASCII-Table, but not for the extended one. For example, an A gives me 1000001 , but an Ä returns 11111111111111111111111111000011 11111111111111111111111110000100 Any ideas how to manage this?

Write one single bit to binary file using BinaryWriter

走远了吗. 提交于 2019-12-10 10:54:24
问题 I want to write one single bit to a binary file. using (FileStream fileStream = new FileStream(@"myfile.bin", FileMode.Create)) using (BinaryWriter binaryWriter = new BinaryWriter(fileStream)) { binaryWriter.Write((bool)10); } Something like binaryWriter.Write((bit)1); When I use binaryWriter.Write((bool)1) the file has one byte, but I want to write one single bit. Is this possible? 回答1: You cannot store only 1 bit in a file. Almost all modern filesystems and hardware store data in segments