PDA

View Full Version : Removing non-ascii chars from a char array



TMagic*04
March 14th, 2010, 02:20 AM
Hi,

I'm sending packets across a network and after the transmission I seem to have a lot of what look like to me as non-ascii chars, do you know of a way that I can strip them from my array so I just have the data I want?


===========================
My Output As an Example of what I Mean
===========================
New Node!
andy|
69.254.0.3|
0:2:b3:45:eb:56����F@�������T6��F@������@���F
�����X6��F�����c6��F@��
�F��F
������6��F
�F������'6��F�a6p
��T'� X����F��F(�����5��FT'� �@�H��� ���F
�����]��9�T'�x���o"��9���6F x���T'�T'� ����{e� �
;\�T'�� ��������������� ���������������� ���������o����������9�X�� �������h����X��������XŸtazz�

Thanks for any help,

LKjell
March 14th, 2010, 02:22 AM
What is the input and do you do any conversion?

MadCow108
March 14th, 2010, 02:28 AM
A char can only hold ascii chars as its exactly as wide as an ascii table (255), there is no room for more in a single char. You probably mean ascii printable chars.

But it looks to me as if the array is not null terminated an thus printing garbage. If so fix that and your output will be fine.

if not then its a simple matter of looping over the array and copying only the ascii printable chars into a new array.
Look it up in an table which ones these are.

era86
March 14th, 2010, 02:28 AM
Maybe you are actually sending binary data accross? If you have control over what you are sending over, you can change the fact that the binary data is being included in between your text.

If you have no control over it, you can do something (which may be idiotic) to parse the incoming string and replace non-ascii chars with spaces:



string recvd;

for ( int i = 0; i < recvd.length(); i++ ) {
if isAlpha(recvd[i]) {
recvd[i] = " ";
}
}


You can figure out isAlpha yourself. This is the naive approach, you can find something more efficient but you get the idea.

TMagic*04
March 14th, 2010, 02:59 AM
Thanks very much for the tips guys, in the end I just ensured that the array was properly null terminated before transmission and it worked a treat... thanks again!

dwhitney67
March 14th, 2010, 02:14 PM
Thanks very much for the tips guys, in the end I just ensured that the array was properly null terminated before transmission and it worked a treat... thanks again!

From the comment above, I have to assume that you were using strlen() to obtain the length of the message within the array.

Without the null-terminator, strlen() would have read contiguous memory, starting at the address of your array, until it found a null-character.

Thus regardless of whether you were sending data across a socket or across the universe, your program had a bug in it. A null-terminator is not required to send data across the socket; you only need specify the correct amount of bytes that you wish to send. Relying on strlen() required you to insert the null-terminator.

P.S. Make sure you insert a null-character at the end of the data on the receiving end too!