Converts a character or string into a binary string (a string of "0" or "1";s).
The use of std::string removes any need for dynamic allocation and pointer
maintenance.
From:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20525179.html
#include <string> using namespace std; string Byte2Binary(unsigned char c) { string S; unsigned char Msk = 0x80; for (int i = 0; i < 8; ++i) { if (c & Msk) S += '1'; else S += '0'; Msk >>= 1; } return S; } string String2Binary(const string &S) { const int StrLen = S.length(); string Result; for (int i = 0; i < StrLen; ++i) Result += Byte2Binary(unsigned char) S[i]); return Result; }
file: /Techref/language/ccpp/cppref/EXAMPLES/2binary_cc.htm, 1KB, , updated: 2006/3/31 15:37, local time: 2024/11/23 06:02,
3.145.106.7:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://ecomorder.com/techref/language/ccpp/cppref/EXAMPLES/2binary_cc.htm"> C++ Programming Example Convert to Binary</A> |
Did you find what you needed? |
Welcome to ecomorder.com! |
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! |
.