-
November 27th, 2005, 08:04 PM
#1
Convert Char to binary
anyone know how to convert char to binary?
-
November 27th, 2005, 09:05 PM
#2
Re: Convert Char to binary
Originally Posted by preci0usGal
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.
-
November 27th, 2005, 09:36 PM
#3
Re: Convert Char to binary
i am trying to send hex and receive hex... as in i can display and see it in hex. but not in char...
-
November 27th, 2005, 09:48 PM
#4
Re: Convert Char to binary
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.
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.
-
November 27th, 2005, 11:17 PM
#5
Re: Convert Char to binary
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.
-
November 28th, 2005, 12:17 AM
#6
Re: Convert Char to binary
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...
This is how I interpret your problem, unless you care to explain in more details.
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.
-
November 28th, 2005, 12:46 AM
#7
-
November 28th, 2005, 01:13 AM
#8
Re: Convert Char to binary
Originally Posted by leojose
This is how I interpret your problem, unless you care to explain in more details.
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.
hi there... i tried... currently. using component of mscomm...
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??
Last edited by preci0usGal; November 28th, 2005 at 01:41 AM.
-
November 28th, 2005, 02:08 AM
#9
Re: Convert Char to binary
Originally Posted by preci0usGal
hi there... i tried... currently. using component of mscomm...
when i use 0x41 hex, i nv get A...instead, i got 3F....
Did you transmit a single byte as in
BYTE ValueA = (BYTE)0x41;
-
November 28th, 2005, 02:13 AM
#10
Re: Convert Char to binary
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>
-
November 28th, 2005, 03:04 AM
#11
Re: Convert Char to binary
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
-
November 28th, 2005, 03:46 AM
#12
Re: Convert Char to binary
-
November 28th, 2005, 08:33 PM
#13
Re: Convert Char to binary
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??
-
November 28th, 2005, 08:38 PM
#14
Re: Convert Char to binary
Originally Posted by preci0usGal
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.
-
March 1st, 2006, 03:47 AM
#15
Re: Convert Char to binary
Originally Posted by preci0usGal
anyone know how to convert char to binary?
simply:
int nResult = (int) chSource;
and then you can transfer nResult from decimal to binary.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|