??? 01/10/08 09:31 Read: times |
#149211 - Very common indeed! Responding to: ???'s previous message |
I think you are making the common beginner's mistake of confusing numbers with the text used to display them.
A byte is just an 8-bit number, which can take any value from 0 to 255 (or -128 to +127 if you treat it as signed, etc). You can write the value of a byte in many different notations; eg, Binary: 01111011 - uses 8 characters Octal: 173 - uses 3 characters Decimal: 123 - uses 3 characters Hexadecimal: 7B - uses 2 characters Roman Numerals: CXXIII - uses 6 characters When it comes to displaying or transmitting that number, many displays and transmission links do not support sending "raw" bytes - so it is very common to use some sort of code - and ASCII is one very common code. ASCII is a 7-bit code, used to encode letters, digits, various "special" characters (punctuation, etc) - and also has a number of "control" codes. The "control" codes are things like Carriage Return, Line Feed, Form Feed, TAB, etc... Here is a table of ASCII codes from http://www.asciitable.com/ So, if you want to use ASCII to display or transmit the value of a byte, it should now be obvious that you will need (in general) two characters - one for each hexadecimal digit. By looking at the ASCII table, you can see that the ASCII codes for the digits '0' to '9' are in sequence; Therefore you can easily convert a numeric value of 0-9 to its corresponding ASCII-coded digit character by simply adding the ASCII code for '0' Numeric ASCII Code ASCII Code Value for '0' for the digit ------- ---------- ------------- 0 0x30 0x30 1 0x30 0x31 2 0x30 0x32 3 0x30 0x33 4 0x30 0x34 5 0x30 0x35 6 0x30 0x36 7 0x30 0x37 8 0x30 0x38 9 0x30 0x30 You can see that the codes for 'A' to 'F' are also in sequence - but 'A' does not immediately follow '9'. This is why the conversion of hex digit values A-F has to be handled separately from the 0-9 values! Numeric ASCII Code Value for the digit ------- ------------- A 0x41 B 0x42 C 0x43 D 0x44 E 0x45 F 0x46 With a little thought, you should be able to see that the offset between the numeric values and their corresponding ASCII digit codes is the code for 'A' minus ten... In 'C', we can combine this as: char hex_digit_to_ascii( unsigned char hex_digit ) { if( hex_digit < 0x0A ) { // It must be 0-9 return hex_digit + '0'; } else if( hex_digit < 0x10 ) { // It must be A-F return hex_digit + 'A' - 0x0A; } else { // It is not a valid value for a single Hex digit! // You decide what action to take here! } } |
Topic | Author | Date |
How common is 2 Byte ASCII HEX? | 01/01/70 00:00 | |
That is an odd way of looking at is | 01/01/70 00:00 | |
Must not post when tired | 01/01/70 00:00 | |
Correct! | 01/01/70 00:00 | |
Very common indeed! | 01/01/70 00:00 | |
hex_digit_to_ascii | 01/01/70 00:00 | |
optimisation | 01/01/70 00:00 | |
you could, but | 01/01/70 00:00 | |
Widespread | 01/01/70 00:00 | |
ERR Thanks | 01/01/70 00:00 | |
C suggestion | 01/01/70 00:00 | |
and 10 times tougher .. | 01/01/70 00:00 | |
beware | 01/01/70 00:00 | |
C? sorry long post! | 01/01/70 00:00 | |
'C' - not rocket science | 01/01/70 00:00 |