java - Hexadecimal representation taking more space than it should -


i have strings, hexadecimal, "ff", "bb", "aa" etc. did small experiment encoding stuff, , looks hexadecimal taking double number of bytes these things in string representation.

my code's this:

        string hex ="ff";          byte[] b = hex.getbytes();         string enc = base16().encode(hex.getbytes());         byte[] c = enc.getbytes(); 

i'm using guava utils encoding stuff.

it appears hex taking 2 bytes, b of length 2. encode hexadecimal. "ff" 255 in decimal, needs take 1 byte. enc 4 bytes , equal "4646".

next, c 4 bytes.

i don't understand point enc getting generated. want c take 1 byte. can throw light?

thank you!

the getbytes() method doesn't think does. doesn't parse hexadecimal number; gives character encodings. character f number 70, hex.getbytes() gets two-byte array of 'f', 'f', or 70, 70.

encodes string sequence of bytes using platform's default charset, storing result new byte array.

to parse hexadecimal number, can use integer.parseint radix of 16.

byte[] c = { (byte) integer.parseint(hex, 16) }; 

integer.parseint used instead of byte.parsebyte because ff large signed byte.

output:

[-1] 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -