ilja
ilja

Reputation: 79

What is the size of char[]?

I'm making hash algorithm, the block of message is 512 bits. In C/C++ I can store in char[64], but Java char takes 2 bytes.

Question: 512 bits of information are char[32] or char[64]?

Upvotes: 4

Views: 2519

Answers (3)

Uwe Plonus
Uwe Plonus

Reputation: 9954

Why use a char[]?

A hash value consists of bytes so the logical choice would be to use a byte[64].

The datatype char is intended to be used as a character and not as a number.

Upvotes: 1

Ori Lentz
Ori Lentz

Reputation: 3688

From the documentation:

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

So in order to store 512 bits, you should have an array of size 32.

Upvotes: 1

Veselin Davidov
Veselin Davidov

Reputation: 7071

Char is 16bit in Java. So char[32] should be enough for 512bits. I think using byte[64] is better though because everyone know a byte is 8 bits and char[32] makes the code harder to read. Also you don't store characters but bits.

Upvotes: 6

Related Questions