Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ascii to binary - and visaversa 1

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
0
0
US
I didn't know what forum to post this question into, but it has to do with Perl, so that's why I am here.

Anyhow, my question has to do with converting binary to ascii and visaversa.

In this example:

C is 0x43 and c is 0x63; in binary C is 1000011 and c is 1100011

Now, typically I thought binary was 1,2,4,8,16,32,64

But, it appears that this is 1,2,4,8,16,32,64,128

WHY?

And, if C is equivalent to 43 in ASCII, how is this converted to the 1000011 in the binary example shown above?

I am trying to understand this.

Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
I feel a little bit like an idiot.

But anyhow, ... it appears that the number 43 is something like 0x43 (which I don't understand), but this is HEX code.

So, evidently HEX 43 is being represented by the 1000011 (as well as 01000011 - which I dont' understand). (SORRY)

Anyhow ... how is HEX coming into play with this?

And then the decimal value of C - is 67?? (Again I don't get this as well.)

But ... in Perl - it appears that this is something I should be able to understand and at least be able to comprehend how this is coming together.

Your help will be appreciated!!

Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
okay, number lesson. i'll just be as thourough as i can.

you understanding of binary is correct. every subsequent digit is a factor of two greater. you can convert any number into a binary number, it just gets longer.
all the other number systems work exactly the same. in octal, every digit is x8 more, hexadecimal is x16 more. hence the name "base 8" or "base 16". or, our normal decimal system is "base 10".

now, the computer ultimately has everything stored as a binary number. people don't always work best with binary numbers. it works okay for, say, the permissions on a file, because we can envision bits being turned off and on. however, big numbers turn into really long strings of 1's and 0's that are hard to understand. that's where octal and hexadecimal become useful. because both 8 and 16 are powers of 2, the conversion from binary still leaves alot of the visialization intact. you map any one digit from octal onto 3 digits of binary. for every digit in hexadecimal, there are 4 digits of binary. but, since the number is then greatly compacted, it's alot easier to work with for a human brain and for fingers to type. that's why permissions on files are usual done with octal. 0644 is really just 110100100, but it's easier use as octal. notice that you can clearly see how 6-4-4 is turned into 110-100-100...
really, it doesn't matter what base you use, as long as it represents the correct number and gets used correctly. also, you can put as many zeros in front of a number, and it won't make a bit of difference. 000001 is the same as 1 (as long as they're both in the same number system).
convention has it that characters are usually represented as either octal or hexadecimal numbers. with regular expressions, the escape sequence "\###" is the character represented by that octal number. "\x##" is the same for hexidecimal. however, there's no system to do a decimal escape sequence.
in general, here's how you tell them apart:
octal numbers start with a zero.
decimal numbers are normal.
hexadecimal numbers start with a '0x' or '0X'.

oh, and one last thing. here's how you count in hexadecimal (assume a '0x'):[tt]
1 2 3 4 5 6 7 8 9 A B C D E F 10
11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20
...
A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0
...
F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF 100[/tt]
as you can see, 10(decimal) is 0xA in hexadecimal, 11 is 0xB, 12 is 0xC, 13 is 0xD, 14 is 0xE, 15 is 0xF, and 16 is 0x10. 256 (16x16) is 0x100.
in perl, as long as you write a number in the way it should be written, it usually gets interpreted into the right number system.

HTH "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Ok,

Now excuse me for my ignorance ... but in the following example, I still don't understand:

C is 0x43 and c is 0x63; in binary C is 1000011 and c is 1100011

C 1000011 - in Binary: 67

How is this being converted to 0x43 in Hex???

Just still confused on this one. But .... YOUR EXPLANATION WAS WONDERFUL!! Thank you for that.

Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
0x43
4 is in the ten's place,
but since it's hexadecimal,
that's the '16's place.
so, 4 times 16 is 64.
the 3 is in the one's place,
which is the same.
3 times 1 is 3.
64 plus 3 is 67.
0x43 = 67
"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top