anyone know how to convert char to binary?
Printable View
anyone know how to convert char to binary?
Not sure what you mean... any data a digital computer deals with is always binary, and so are the contents of a char variable.Quote:
Originally Posted by preci0usGal
i am trying to send hex and receive hex... as in i can display and see it in hex. but not in char...
It is still unclear what you mean by "hex" and "char". Hexadecimal is a base which can be used for displaying a numerical value as a character string - just as octal, decimal or binary. Hex is not a data type. As said above, all numbers are stored and treated in binary form in a digital computer - by its very nature. 'char', OTOH, is a data type of the C/C++ programming langue, which is always 8 bits (or one byte). The numerical value it contains can be displayed (by formatting it accordingly) as a number to any base - such as binary, octal, decimal or hex, or it can be treated as an index into a character set of a specific code (such as ASCII) and be displayed as that specific character.Quote:
Originally Posted by preci0usGal
However, "converting from char to binary" or "hex to char" doesn't make any sense: One is a a base, the other is a data type.
Use Binary Operators and store them in an array.
jUST LIKE THIS
int nNumA=8; // beginning value is 8
nNumA >> 2; // ending value is 2
0000 1000
IS CONVERTED TO
0000 0010
I THINK that will help you.
This is how I interpret your problem, unless you care to explain in more details.Quote:
Originally Posted by preci0usGal
You are sending a single byte value and trying to display it on the screen. A single byte value (0-255 hex) has an ascii representation and so you will be only able to see the ascii value.
For e.g. if you send single byte 0x41, then you will be able to see the letter A on your screen. Try performing an atoi() on that value you receive and then display it on screen. This will give you the decimal value.
You can use sscanf() to perform type conversions also.
this might help u ...
and possible check thi slink .. can give u some idea !!Code:
string hexData = "AABBCCDD01234567";
string binData = Conversion.HexToBin(hexData);
Hexadecimal, Binary, and Decimal conversions
if a post helps.. dont forget to "Rate this post"
hi there... i tried... currently. using component of mscomm...Quote:
Originally Posted by leojose
when i use 0x41 hex, i nv get A...instead, i got 3F....
can help?? tell me how am i gg to do it... to get A i mean...
btw, wats atoi()??
do u haf any sample that i can refer to??
Did you transmit a single byte as inQuote:
Originally Posted by preci0usGal
BYTE ValueA = (BYTE)0x41;
i put it this way...
<pre>
CString test = 0x50;
m_comm.SetOutput(COleVariant(test));
Sleep(1000);
VARIANT var;
var = m_comm.GetInput();
CString aa(var.bstrVal);
char CBuff[100];
sprintf(CBuff, "%x", aa.GetAt(aa.GetLength()-2));
MessageBox(CBuff);
</pre>
sorry..can't help you out with an MFC application... not my domain... :(
See if you can find something useful here
http://www.codeguru.com/forum/showthread.php?t=350757
for the program that i have written, when the input mode is 0 which is text. the correct format and answer appear. but when i change it to binary, which is 1. it is wrong. how can i change it to the correct answer??
Sorry, but what you are saying is rather cryptic. Which input mode, and which answer? Please give more details about your problem - possibly with source code, and with examples of your data: Both how you expect it to be, and what you actually get.Quote:
Originally Posted by preci0usGal
simply:Quote:
Originally Posted by preci0usGal
int nResult = (int) chSource;
and then you can transfer nResult from decimal to binary.