• Binary Numbers to Ascii Value? or intstr()
    3 replies, posted
I've always been confused how this works at a baseline level. For instance how does it know 101010 is equal to 42 in a ascii string format. I'd like to know how its done / calculated. Might be a dumb question, no one might know what I'm trying to ask. Dunno worth a shot.
[QUOTE=Ybbat;24701858]I've always been confused how this works at a baseline level. For instance how does it know 101010 is equal to 42 in a ascii string format. I'd like to know how its done / calculated. Might be a dumb question, no one might know what I'm trying to ask. Dunno worth a shot.[/QUOTE] Convert binary to base-10, 8 bits is a byte so 0-255, convert that to ascii.
[QUOTE=Ybbat;24701858]101010 is equal to 42 in a ascii string format[/QUOTE] ASCII isn't what makes 101010 equal 42. Ascii is a mapping between numbers in the range 0-127 and various characters. What you've listed there (101010 and 42) are just two different ways of representing the same number. 101010 is in base-2, or binary, and 42 is in base-10, or decimal. You can convert base-2 to decimal by adding the values at each place: [code] 1 0 1 0 1 0 = 1*2^5 + 0*2^4 + 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 1*32 + 0*16 + 1*8 + 0*4 + 1*2 + 0*1 = 32+8+2 = 42 [/code] 42, or 101010 in binary, is defined as 'B' (if I recall correctly, I'm doing this from memory) in the ASCII standard, so when a string contains an element with the value 42, the computer prints a 'B'. [editline]03:29AM[/editline] Whoops, 0x42 (base-16 or hexadecimal) is 'B'. 42 decimal is '*'
It is just base conversions. With computers everything is done in binary, but we can also convert a binary number to a decimal number. I'm not sure if looking at the binary is that much important outside of programming for hardware, but when you're programming on microprocessors, hex usually makes the most sense to use when setting ports although we could use the binary equivalent. This is the basic formula for figuring out the bases. [code]... x4 * base^4 + x3 * base^3 + x2 * base^2 + x1 * base ^1 + x0 * base^0 . + 1x * base^-1 + 2x * base^-2 ...[/code] Converting isn't really hard at all. Note that the base^0 is going to be the first digit. Anything to the negative power is going to be a fraction. To put it simply, any number can be be represented in any base, it just takes a conversion.
Sorry, you need to Log In to post a reply to this thread.