<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Ok, assuming you are NOT going to use some standard library, here are the issues and some code</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Let us first discuss getting the character string from one machine to the other.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Either (a) it is fixed length, or (b) you put a size on the beginning, or (c) you put a flag at the end</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Fixed length is easiest, and least efficient.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Size is only best if the length is very long - its not going to be here.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
So instead we need a flag at the end. Lets do that - we will discuss how in a minute.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
So assuming that the characters arrive one at a time you get some receiver code that looks like</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
int i = 0;</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
while (getNextChar(&c)) {</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
adjust i using c</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
}<br>
<br>
Next problem. What characters can you send. Can you send ANY (0x00..0xff) character, or does the h/w and s/e connecting the two ends stop you from using NUL (0x00)? CR? LF? Ctrl-S? Ctrl-Q? others?. Let's assume that you can only send printable chars
' '..'~'.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
char firstSendableChar = ' ';</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
char lastSendableChar = '~';</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
int nSendableChars = ((int)(<span style="background-color:rgb(255,255,255); display:inline!important">lastSendableChar)<span> - (int)(<span style="background-color:rgb(255,255,255); display:inline!important">lastSendableChar)<span>) + 1;</span></span></span></span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Now the basic idea, we are going to have to improve it, is to do this at the transmitting end</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
int val = <the number to send></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
while (val > 0) {</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
char toSend = val % nSendableChars; // these two lines are the crux of the solution</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
val = val / <span style="background-color:rgb(255, 255, 255);display:inline !important">nSendableChars;</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="background-color:rgb(255, 255, 255);display:inline !important"> send((char)(toSend + <span style="background-color:rgb(255, 255, 255);display:inline !important">firstSendableChar<span> )</span></span>)</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="background-color:rgb(255, 255, 255);display:inline !important"> }</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="background-color:rgb(255, 255, 255);display:inline !important"><br>
</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="background-color:rgb(255, 255, 255);display:inline !important">
<div style="margin:0px;font-size:12pt"> int i = 0;</div>
<div style="margin:0px;font-size:12pt"> while (getNextChar(&c)) {</div>
<div style="margin:0px;font-size:12pt"> i = i*<span style="background-color:rgb(255, 255, 255);display:inline !important">nSendableChars + (c - <span style="background-color:rgb(255, 255, 255);display:inline !important">firstSendableChar);</span></span></div>
<div style="margin:0px;font-size:12pt"> }<br>
</div>
</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="background-color:rgb(255, 255, 255);display:inline !important"><br>
</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Okay, so now we only have two problems. (a) negative numbers, and (b) knowing when to stop.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
You could just do the above using unsigned int, and use </div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
unsigned int <span style="background-color:rgb(255, 255, 255);display:inline !important">unsignedIntermediate = (unsigned int)(input);</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="background-color:rgb(255, 255, 255);display:inline !important"> ...</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="background-color:rgb(255, 255, 255);display:inline !important"> send and receive</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="background-color:rgb(255, 255, 255);display:inline !important"> ...</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
int output = (int) unsignedIntermediate; </div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
and the only problem with that is -small is a very long this way.<br>
<br>
A better approach is to send the sign bit similarly to sending the count.<br>
<br>
The second problem is that there is no positive number corresponding to the most negative number, so you can't just do</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
int sign = (value > 0);</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
if (sign) value = -value;</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Instead you have to do </div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
if (!sign) value = -value;</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Now the above loop using % and / requires understanding how those operations work with -ve numbers.<br>
<br>
How to send the sign and end-of-number bits? One thing to do place them both in the last character. To do this,</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
reduce the </div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="background-color:rgb(255, 255, 255);display:inline !important">int nDigitChars =
</span><span style="background-color:rgb(255, 255, 255);display:inline !important">nSendableChars / 2</span><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px">;</span></span></span></span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><br>
</span></span></span></span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px">Now we can code the above loop</span></span></span></span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"> </span></span></span></span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px">
<div style="margin:0px;font-size:12pt"> while (val > 0) {</div>
<div style="margin:0px;font-size:12pt"> int nextVal = <span style="background-color:rgb(255, 255, 255);display:inline !important">val / </span><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="background-color:rgb(255, 255, 255);display:inline !important">nDigitChars<span> </span></span>;</span></div>
<div style="margin:0px;font-size:12pt"> char toSend = val % <span style="background-color:rgb(255, 255, 255);display:inline !important">
nDigitChars<span> </span></span>; // these two lines are the crux of the solution</div>
<div style="margin:0px;font-size:12pt"> if (nextVal == 0) toSend += <span style="background-color:rgb(255, 255, 255);display:inline !important"><span style="background-color:rgb(255, 255, 255);display:inline !important">nDigitChars<span> </span></span>;</span></div>
<div style="margin:0px;font-size:12pt"> val = <span style="background-color:rgb(255, 255, 255);display:inline !important">
nextVal<span> </span></span><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important">;</span></div>
<div style="margin:0px;font-size:12pt"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"> send((char)(toSend + <span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important">firstSendableChar<span style="margin:0px"> )</span></span>)</span></div>
<div style="margin:0px;font-size:12pt"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"> }</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><br>
</span></span></span></span></div>
and the receive loop</span></span></span></span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px"><br class="Apple-interchange-newline">
<div style="margin:0px;font-size:12pt"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important">
<div style="margin:0px"> int i = 0;</div>
<div style="margin:0px"> while (getNextChar(&c)) {</div>
<div style="margin:0px"> int atEnd = (c >= <span style="background-color:rgb(255, 255, 255);display:inline !important">
nDigitChars</span><span style="margin:0px"> );</span></div>
<div style="margin:0px"><span style="margin:0px"> if (atEnd) c -= <span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important">nDigitChars</span><span style="margin:0px;background-color:rgb(255, 255, 255)"> ;</span></span></div>
<div style="margin:0px"> i = i*<span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="background-color:rgb(255, 255, 255);display:inline !important">nDigitChars</span><span style="margin:0px"> </span>+ (c - <span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important">firstSendableChar);</span></span></div>
<div style="margin:0px"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"><span style="margin:0px;background-color:rgb(255, 255, 255);display:inline !important"> if (atEnd) break;</span></span></div>
<div style="margin:0px"> }<br>
</div>
</span></div>
<br class="Apple-interchange-newline">
I'll leave it as an exercise to the reader to place the sign bit in the last sent character.<br>
<br>
I'm not sure the complexity of the above is worth it.<br>
<br>
Those % and / are expensive. There are faster ways than the above - I spent a few months working on the integer to/from string in the 1990's because believe it or not it is an important operation in telephone systems.<br>
<br>
/Bevin</span></span></span></span></div>
</body>
</html>