title: how to stuff a byte in an AnsiChar?
body: <p>Due to the incertitude about the data nature we began to read some bcd values to Chars.
  For example if the data in the file is 6 bytes long so we would read it like that:</p>
  
  <pre><code>var
    fifi     : TextFile;
    path, res: AnsiString;
    i        : Integer;
    tmp      : AnsiChar;
  begin
    assignFile(fifi, path);
    reset(fifi);
    for i := 1 to 6 do
    begin
     read(fifi, tmp);
     res := res + tmp;
    end;
  end.
  </code></pre>
  
  <p>As it is binary data doing a cast on each chars gives us back the data.</p>
  
  <p>This is something that would be hard to change now.</p>
  
  <p>I now have a custom made function that construct signed packed bcd values from a string in the form of an array of bytes.</p>
  
  <p>I then would like to do the opposite which is stuff each bytes of this array in an AnsiChar so that we could merge it with the ebcdic data produced earlier in the process.</p>
  
  <p>But what the read function does naturaly, is hard to reproduce. There is always some corruption or implicit conversion that is being done from what I constated.</p>
  
  <p>Here is what I tryed : </p>
  
  <pre><code>function memoryStreamToString(str: TMemoryStream): AnsiString;
    begin
      setString(result, PChar(str.memory), str.size div sizeOf(AnsiChar));
    end;
  
  function byteToStr(t: array of Byte): AnsiString;
    const
      digits: array[0..15] of AnsiChar =
        ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
    var
      i: Integer;
    begin
      result := '';
      for i := low(t) to high(t) do
        result := result + digits[(t[i] shr 4) and $0F] + digits[t[i] and $0F];
    end;
  
  var
     buff                 : array of Byte;
     bcdlen               : Integer;
     res                  : AnsiString;
     //str                : TMemoryStream;
  
  begin
  
    //code that init res, buff
  
    // way number one
    //str := TMemoryStream.Create;
    //str.writeBuffer(buff[0], bcdLen);
    //res := memoryStreamToString(str);
    //str.free;
  
    //way number two
    //res := byteToStr(buff);
  
    // way number three
    //setLength(res, bcdLen);
    //move(buff[0], res[1], bcdLen);
  
  end.
  </code></pre>
  
  <p>So the question I think is How do you stuff a byte in an AnsiChar without corruption?
  I believe it is not good practice to do it as the nature of the data is not respected but but we are too late in the implementation phase to change it.</p>
  
  <p>Thank you.</p>