Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Converting BCD to Hex

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-16-2010, 03:41 PM PM User | #1 soper87 New Coder   Join Date: Jul 2009 Location: Scotland Posts: 44 Thanks: 2 Thanked 1 Time in 1 Post Converting BCD to Hex I have just got some software working where I can access and read in a serial port that the user has selected. As the information is read in I want to split it into 32byte segments, which I have got it doing, and then convert this information from BCD to Hex and put out onto the console. I have the conversion as being multiplying the top nibble of each byte by 10 and then adding this to the lower nibble but it doesn't appear to be correct. Code: ```void convert2Hex(byte* buffer, int byte_num) { int i, n0, n1; for (i = (byte_num-1); i >= 0; i--) { n0 = x; //Need the value of lower nibble. n1 = x * 10; //Need the value of higher nibble, multiply by 10. buffer[i] = n1 + n0; //Put hex value back into array. } }``` The array will then be converted to a string once all 32 bytes have been converted and output to console (or that's the theory at least...) Any ideas if this is the correct approach?
 03-16-2010, 05:21 PM PM User | #2 oracleguy Rockstar Coder   Join Date: Jun 2002 Location: USA Posts: 9,054 Thanks: 1 Thanked 322 Times in 318 Posts Where do you assign x a value? I don't see any code that does that. I think you are trying to do the wrong thing. If you want to be able to print the value of bytes to the console in hex, you need to store each hex number (as in every 4 bits of data) as one char. And to separate a byte out into each nibble, you use bitwise operators: unsigned char some_byte = 0xF3; unsigned char lower_nibble = some_byte & 0x0F; unsigned char upper_nibble = (some_byte & 0xF0) >> 4; Then you can convert lower_nibble and upper_nibble to ASCII and store them some place for output to the screen. __________________ OracleGuy
 03-16-2010, 05:30 PM PM User | #3 soper87 New Coder   Join Date: Jul 2009 Location: Scotland Posts: 44 Thanks: 2 Thanked 1 Time in 1 Post Was meant to be passing x across as the byte value (forgot to put it in with the other parameters). Read through what you have described and it makes sense with what I was thinking after I posted earlier and read through small pieces of information scattered through various forums. Changed the code to: Code: ```string convertHex(byte* sBuffer, int n_byte) { char output[32]; for (int i = (n_byte-1); i >= 0; i--) { int n0 = sBuffer[i] & 0x0F; int n1 = (sBuffer[i] & 0x0F) >> 4; output[i] = ((n1 + n0) << 4) * 10; } //Convert array into string. std::string str(reinterpret_cast(output), 32); //Return string to be displayed on console. return str; }``` Idea being that as the data is read in it is stored in a 32 byte array and then passed to this method to be converted and returned to display on screen, although still not 100% yet (hopefully not too far off though). Thanks for the help oracleguy

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:38 AM.