Binary (number system): How do you convert "P" from ASCII to hexadecimal?
-
Say I want to convert the letter "p" into ASCII then to two digit hexidecimal. I would start by putting each letter into ASCII. The ASCII code for p is 112. Would I convert 112 into hexadecimal or would I convert 1, 1 and 2 separately?
-
Answer:
I think it's likely that you want to convert 112 to hex, which is what would happen in C code. Code #include <stdio.h> int main() { char p = 'p'; printf("%c is %d in decimal and %x in hex.\n", p, p, p); return 0; } Output p is 112 in decimal and 70 in hex. You can convert 112 directly to hex but it is probably easier to convert to binary first and then convert to hex if you're doing the work by hand. 112 / 2 = 56 R 0 56 / 2 = 28 R 0 28 / 2 = 14 R 0 14 / 2 = 7 R 0 7 / 2 = 3 R 1 3 / 2 = 1 R 1 1 / 2 = 0 R 1 Then you have two groups of 4 binary digits, 0111 and 0000, which correspond to 7 and 0 in hex respectively. 70
Peter Enns at Quora Visit the source
Other answers
You can convert directly to hex by dividing for 16. 112/16 = 7 ===> 07
Adalberto Silva
Hexadecimal is simply a notation used to express an integer quantity. "p" is another representation, and is implied in a certain context, such as a terminal or text editor. Each is merely the representation of a byte, which has exactly the same arrangement of binary digits in either representation, as well as other representations. It makes more sense to invert your statement 'ASCII code for p is 112'. Actually, the byte is 112 (in decimal), and its ASCII representation is 'p'. Expressed in hexadecimal, the very same arrangement of binary digits is 70. It's the very same byte, just represented differently for different purposes depending on context. The following bit of C code demonstrates this: // A single byte with a value unsigned char myByte = 112; // Three ways to print the value of the same byte printf( "%c %02X %d\n", myByte, myByte, myByte ); What gets confusing is when people want a printable, human-readable representation of a byte. If you just print the byte, it's default representation will be the ASCII version. In order for you to see the hexadecimal (or other radix) version, it needs to be represented by a string of two ASCII characters which can be printed. Each of those characters, of course, is a byte which has an appropriate ASCII representation.
Rod Nussbaumer
If you wish to covert the letter "p" in lowercase ASCII (= 112 decimal) to two characters representing this into a displayable ASCII format "70" you can do it using a 256 byte table, indexed by the value of the character in question. (In case the value exceeded its ASCII value range, it would be safer to use a 512 byte table to cater for all possible values up to X"FF"). The table below only shows the first 66 characters of this table (for brevity). However, you should be able to see that decimal 14 ('decimal shift out') will convert to characters "0E"; decimal 27 ('escape') to "1B" ; decimal 32 ('space') to " " and so on. Here is a link to an ASCII table [ http://simple.wikipedia.org/wiki/ASCII ] For [ http://en.wikipedia.org/wiki/EBCDIC ] characters you would need a 512 byte table, but still only a single assignment to extract the two characters. In low level languages (Assembler), you would convert the value of the ASCII or EBCDIC character to an offset into the table by left shifting by 1 bit first) (In all cases you can get the two byte hex string using just a 16 byte table but it requires about 4 or 5 instructions to convert, rather than a single instruction). 'C' example /* ASCII value to HEX character array (Hex 00-7F) */ static const char ASCII_HEX[] = { "00", "01", "02", "03", "04","05","06","07","08","09",0A","0B","0C,"0D","0E",0F" , "10", "11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F" ," "...etc to "7F"};
Kenneth Dakin
Related Q & A:
- How do I convert a PDF file to PDF/A in Delphi?Best solution by softwarerecs.stackexchange.com
- How do I convert a bitmap to vector?Best solution by Super User
- How do I convert the image into ASCII format?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- How do you convert an IP address to hexadecimal?Best solution by ncalculators.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.