Graeme Brown

Ranch Hand

Posts: 193

posted 16 years ago

Here is a static method that will do roughly what you want. Since String is final, it is not possible to extend String to include the toHex method, so it is not possible to call s.toHex().

Also in your example "hello" is lower case, so the correct output should be

\u0068\u0065\u006c\u006c\u006f

Regards

Graeme

Also in your example "hello" is lower case, so the correct output should be

\u0068\u0065\u006c\u006c\u006f

Regards

Graeme

Joseph Russell

Ranch Hand

Posts: 290

tyler jones

Ranch Hand

Posts: 101

posted 16 years ago

This brings up a subject I have been wondering about. I've learned how to take a number and figure out it's binary equivelent, but I have no clue how to take a number or letter and figure out it's octal or hexidecimal equivelaent. Does anyone have a formula or something to figure this out?

Graeme Brown

Ranch Hand

Posts: 193

posted 16 years ago

Tyler

Since you can get from decimal to binary, it is then really easy to get from binary to octal or hex.

For hex split the binary number up into groups of 4 bits (padding with leading zeroes if necessary), then convert each group. For octal do the same with groups of 3 bits:

So

6748 decimal

= 0001 1010 0101 1100 binary

= 1A5C hex

= 001 101 001 011 100 binary

= 15134 octal

Since you can get from decimal to binary, it is then really easy to get from binary to octal or hex.

For hex split the binary number up into groups of 4 bits (padding with leading zeroes if necessary), then convert each group. For octal do the same with groups of 3 bits:

So

6748 decimal

= 0001 1010 0101 1100 binary

= 1A5C hex

= 001 101 001 011 100 binary

= 15134 octal

tyler jones

Ranch Hand

Posts: 101

posted 16 years ago

This didn't really explain how to convert it though, just to break it up into groups of four or three. I'm still wondering how to figure it out. BTW, I only know how to convert numbers from decimal to binary...I have no clue if there's a formula to figure out the binary equivalent of 'f' or some other character. Thanks for trying to help...but can you provide a bit more detail?

Originally posted by Graeme Brown:

Tyler

Since you can get from decimal to binary, it is then really easy to get from binary to octal or hex.

For hex split the binary number up into groups of 4 bits (padding with leading zeroes if necessary), then convert each group. For octal do the same with groups of 3 bits:

So

6748 decimal

= 0001 1010 0101 1100 binary

= 1A5C hex

= 001 101 001 011 100 binary

= 15134 octal

This didn't really explain how to convert it though, just to break it up into groups of four or three. I'm still wondering how to figure it out. BTW, I only know how to convert numbers from decimal to binary...I have no clue if there's a formula to figure out the binary equivalent of 'f' or some other character. Thanks for trying to help...but can you provide a bit more detail?

Marius Holm

Ranch Hand

Posts: 84

posted 16 years ago

Tyler,

The main point is to think in the base of the numbers we speak.

Usually, using decimal numbers with a base of 10, that means we have 10 'number tokens' to express the value we need, that's the numbers 0-9, altogether ten. When using binary numbers, you use a base of two, letting you use only two tokens, 0 and 1.

When thinking octal, just use the first eight tokens, 0-7.

Hexadecimal notation requires 16 tokens, so we need to extend the set of number tokens we use, this is done by adding the alfanumeric symbols a-f, that's six more tokens, to the 0-9 set.

This gives a the value of 10, b is 11, and up to f as 15.

Then, whatever 'token set' you use, you can perform math as usual. The point to keep in mind is

Anyway, to compute hex'es, you should start binary, and think about how many bits you can accommodate in one token. For hex'es that's four bits, and with octals it's three. That's what Graeme pointed out with his splitting of the binaries.

Does this help?

Marius

[This message has been edited by Marius Holm (edited January 22, 2001).]

The main point is to think in the base of the numbers we speak.

Usually, using decimal numbers with a base of 10, that means we have 10 'number tokens' to express the value we need, that's the numbers 0-9, altogether ten. When using binary numbers, you use a base of two, letting you use only two tokens, 0 and 1.

When thinking octal, just use the first eight tokens, 0-7.

Hexadecimal notation requires 16 tokens, so we need to extend the set of number tokens we use, this is done by adding the alfanumeric symbols a-f, that's six more tokens, to the 0-9 set.

This gives a the value of 10, b is 11, and up to f as 15.

Then, whatever 'token set' you use, you can perform math as usual. The point to keep in mind is

*how many characters, or numbers (tokens)*you need to express your value. As you already know, when using decimals, you can express ten different values with one token, after that you must use more tokens. Whereas with binaries, you must use for instance three tokens to express eight values. You have probably noted that the bases of number sets used mainly on computers, binary, octal and hex, all are based on the number '2' raised to some power. This of course has to do with the digital nature of computers.Anyway, to compute hex'es, you should start binary, and think about how many bits you can accommodate in one token. For hex'es that's four bits, and with octals it's three. That's what Graeme pointed out with his splitting of the binaries.

Does this help?

Marius

[This message has been edited by Marius Holm (edited January 22, 2001).]

Consider Paul's rocket mass heater. |