1

I'm going to a Web Service an Array of Byte, the issue that is going the bytes with values ​​from 0-255 and the Web Service accepts only value of -127 to 127, someone could help me?

A snippet of the code is this:

BytesFile : array [1..66000] of Byte;

AssignFile(fileB,'C:\img.jpg');
Reset(fileB,1);
BlockRead(fileB, BytesFile , SizeOf(BytesFile ), NumRead);
Jose Eduardo
  • 487
  • 9
  • 27
  • 1
    As a side note, you shouldn't be using AssignFile, Reset and BlockRead they're archaic. Look up TFileStream and use that. – Lloyd Nov 01 '12 at 11:46
  • 1
    that that your code would fail on readonly files, uch as network or CD-ROM. There is global `FileMode` variable. Set it to zero before `ReSet` to open read-only or set it to two to open read-write (default). However better use modern tools like TFileStream or IOUTils unit or David's fast file reader class. http://stackoverflow.com/questions/1642220 – Arioch 'The Nov 01 '12 at 13:26

3 Answers3

2

-127-127 is basically a signed byte.

For that in Delphi you can use a ShortInt, see here:

http://www.delphibasics.co.uk/RTL.asp?Name=ShortInt

Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • Sorry is the first time working with Byte, I was a little confused as to my ShortInt apply the concept that I quoted at the beginning? – Jose Eduardo Nov 01 '12 at 11:30
  • Yes. A byte is an unsigned integer between 0 and 255. ShortInt is basically the signed equivalent, meaning you have a range of -128 to +127. As bummi below said though, it's all about interpretation. – Lloyd Nov 01 '12 at 11:46
  • Excuse the ignorance, but I am not knowing how to apply this concept to my code (which is in the first post). – Jose Eduardo Nov 01 '12 at 11:52
  • Strange that when I try with an image (jpg) of Range Check Error, could tell me what it is? – Jose Eduardo Nov 01 '12 at 12:12
  • It depends what you're doing with it. You should be able to read images in just fine. Though like I said, I wouldn't use AssignFile/BlockRead for this. – Lloyd Nov 01 '12 at 12:18
  • I searched and could not use the TFileStream, at least not with the examples I found, as you would stage a quick example of how it would look? thank you – Jose Eduardo Nov 01 '12 at 12:22
  • I have nothing to hand, however there's an example here - http://delphi.wikia.com/wiki/TFileStream_Class – Lloyd Nov 01 '12 at 12:26
  • I almost forgot, I need to turn this "Array of ShortInt" or TFileStream on a String, TFileString could use, but do not know how transformalo in string ... If you can help me I am very grateful – Jose Eduardo Nov 01 '12 at 12:38
  • @eduardo, make a new question of that instead. – LU RD Nov 01 '12 at 14:11
2

Byte is Byte, it's a question of interpretation

var
 s:ShortInt;
 b:Byte;
begin
   s := -1;
   b := s;
   Showmessage(IntToStr(b));
   s := -127;
   b := s;
   Showmessage(IntToStr(b));

end;
bummi
  • 27,123
  • 14
  • 62
  • 101
0

This seems to be an issue with signed or unsigned datatypes.
If possible try to convert to the signed type of byte, which is ShortInt: Link
Or try to use ShortInt right from the beginning.

Strike
  • 248
  • 1
  • 8