Recent

Author Topic: Storing a Byte Array into a string  (Read 10593 times)

DumDum

  • New Member
  • *
  • Posts: 32
Storing a Byte Array into a string
« on: January 05, 2018, 12:32:22 am »
I am having trouble with my code here. 

What I am trying to do is convert a byte array into a string, is this at all possible or am I out to lunch?

I did some reading up and found the way in which to convert a byte array into a char array.  If this is a necessary step to converting something into a string then I will roll with this.

So I guess my questions are:

1. Is it possible to convert a byte array into a string of text.
2. Is it possible to convert a char array into a string of text.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Storing a Byte Array into a string
« Reply #1 on: January 05, 2018, 12:38:50 am »
1. Is it possible to convert a byte array into a string of text.
That depends on what is inside that array of bytes and to what you wish to convert that to ? Octals value representation ? binary ? hexadecimal ? decimal ?
Quote
2. Is it possible to convert a char array into a string of text.
Just assign ?

PS: what (in your opinion) is a string of text ? AnsiString ? WideString ? RawByteString ? Unicode ? Something else ?
« Last Edit: January 05, 2018, 12:41:12 am by molly »

DumDum

  • New Member
  • *
  • Posts: 32
Re: Storing a Byte Array into a string
« Reply #2 on: January 05, 2018, 12:52:08 am »
Thanks for the reply Molly.

I am trying to assign hexadecimal values to a series of a string in order to do a search on that string.

In my mind a string would be a raw byte string.

Something like:

FAABBE7834BEB4B6B1

The end goal here is to be able to search for a specific series of hex values throughout a file.

DumDum

  • New Member
  • *
  • Posts: 32
Re: Storing a Byte Array into a string
« Reply #3 on: January 05, 2018, 12:56:30 am »
Quote
program search;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

var
  a: file of byte;
  y,x,count: integer;
  byteread: byte;
  BYTEARRAY: array [1..592] of byte;



begin

  y:=0;
  count:=0;
  assignfile(a, 'new.bin');
  writeln('File Loaded');
  reset(a);
  for x:=1 to 592 do
  begin
  y:=y+1;
  read(a, byteread);
  BYTEARRAY[y]:=byteread;
  write(' ',HexStr(BYTEARRAY[y],2));
  count:=count+1;
  end;
  close(a);
  y:=0;
  writeln;
  writeln;
  writeln(count, ' bytes in file');
  writeln;
  writeln;
  readln;

end.



Basically I am stuck here, this works to broadcast the found bytes in hex to the screen.  Just curious if I can take this a step further and convert what is outputted to the screen into a searchable string.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Storing a Byte Array into a string
« Reply #4 on: January 05, 2018, 12:56:53 am »
Note: our posts crossed

In that case, assuming that you do not know for sure the data you are searching in is perhaps not fitted for a string, would it not be more convenient to use a stream (can be any stream: file, memory, etc.) and search for the specified sequence ?

Assuming that you wish to provide the search term as a string you can convert a hexadecimal representation of a search-sequence into an array of bytes and search for that inside the stream.

DumDum

  • New Member
  • *
  • Posts: 32
Re: Storing a Byte Array into a string
« Reply #5 on: January 05, 2018, 01:02:39 am »
I have tried reading up on streams and have come up with more questions than answers.  Is there any direction you can point me in to getting a better idea of how to use streams and what they are/ their purpose.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Storing a Byte Array into a string
« Reply #6 on: January 05, 2018, 01:11:53 am »
remember that HEX in ascii isn't the actual MEMORY value..
to get the actual true memory byte value

 SomeByte := InToStr($FE); for example.

 SetLength(SomeDynamicArray, Length(SomeDynamicArray)+1);
 
 SomeDynamicArray[High(SomeDynamicArray)] := SomeByte;

You should make an procedure that accepts Arrays of Byte..

Think on that.
The only true wisdom is knowing you know nothing

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Storing a Byte Array into a string
« Reply #7 on: January 05, 2018, 01:15:12 am »
I have tried reading up on streams and have come up with more questions than answers.  Is there any direction you can point me in to getting a better idea of how to use streams and what they are/ their purpose.
Not wanting to be a party-pooper there but could you explain what exactly it is that you are not able to understand. This seems to be a repetitive question from your hand. That is no problem, but please try to explain to us what it is that is troubling you.

See here for an example using plain files (forget about the second example as it is windows specific). Here is something about streams (delphi docs seems to explain it a little bit better then our docs).

And this thread... well.... you might recognize it  :)

DumDum

  • New Member
  • *
  • Posts: 32
Re: Storing a Byte Array into a string
« Reply #8 on: January 05, 2018, 01:18:57 am »
I apologize, I will go back in my hole now lol.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Storing a Byte Array into a string
« Reply #9 on: January 05, 2018, 01:23:25 am »
I apologize, I will go back in my hole now lol.
There is no need to crawl back to whatever place it is you wish to crawl to. but it is very difficult to answer your questions when there are no exact questions asked.

Giving you a 1:1 tutorial on streams is out of my scope. There are books for that, or better yet learn by example/trying. But, if reading about streams is providing you more questions then answers, then ask those question to get them answered.

DumDum

  • New Member
  • *
  • Posts: 32
Re: Storing a Byte Array into a string
« Reply #10 on: January 05, 2018, 01:26:11 am »
I am so new to this language that my mind cannot even come up with an appropriate question. I have been doing trial and error with what I know and it has worked to some degree but I think that my lack of knowledge of streams is what is keeping me from a good solution.

I will take what you have given me and read more into streams and hopefully can figure something out eventually.

Thanks for your input Molly and jamie :)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Storing a Byte Array into a string
« Reply #11 on: January 05, 2018, 01:37:00 am »
Are you new to OOP (Object Oriented Programming) as well ? In short that means working with objects/classes.

If not mistaken then the other thread provides some (working) examples. Just pick one and try to analyze the code. Inspect the variables (just write out their values on each run, using writeln if necessary) to get a better understanding of what the code is actually doing.

In case something is unclear then simply ask what is unclear to you.

If you are new to OOP and/or do not know the working of a particular class/Object then that might take some time to understand how such class/object works exactly. That is even true for the most experienced programmers amongst us that get acquainted with a class that they've never used before. Good documentation helps in suich cases, but is sometimes lacking, leaving the path of trial and error.

DumDum

  • New Member
  • *
  • Posts: 32
Re: Storing a Byte Array into a string
« Reply #12 on: January 05, 2018, 02:14:57 am »
Very new to it all unfortunately, looks like I have a lot of sleepless nights in front of me.

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Storing a Byte Array into a string
« Reply #13 on: January 20, 2018, 05:33:29 pm »
Very new to it all unfortunately, looks like I have a lot of sleepless nights in front of me.

It is pretty hard to get classes and Object Oriented programming, until you have dealt with it.
You should read up on classes, records, data passing, etc.

Streams are OO heavily, and you should be familiar in some language of how they work. I am new to pascal myself, but I know a bit of C, C++, and VB which are all OO -> so that helped.

Programming starts with logic, and code, then actualy syntax. If you don't have the logic and understanding of stuff like OO programming, then syntax is the least of your issues!

Read up and do pratice programs. best thing is , find a project and work on it. You will start with it being pretty bad, then you can change code as you get better. Thats how you lean!

Good luck!

-Nugax

circular

  • Hero Member
  • *****
  • Posts: 4220
    • Personal webpage
Re: Storing a Byte Array into a string
« Reply #14 on: January 20, 2018, 06:00:41 pm »
Hi Dumdum,

You don't need objects, you can search into a file like this :
Code: Pascal  [Select][+][-]
  1. program find;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes,
  10.   sysutils;
  11.  
  12. var
  13.   data: RawByteString;
  14.  
  15. procedure DisplayData;
  16. var
  17.   i: integer;
  18.  
  19. begin
  20.   writeln('File content:');
  21.   for i := 0 to high(data) do
  22.   begin
  23.     write(hexStr(ord(data[i]), 2));
  24.     if i mod 24 = 23 then
  25.       writeln
  26.     else
  27.       write(' ');
  28.   end;
  29.   writeln;
  30. end;
  31.  
  32. procedure LoadData(AFilename: string);
  33. var
  34.   f: File;
  35. begin
  36.   AssignFile(f, AFilename);
  37.   Reset(f,1);
  38.   SetLength(data, FileSize(f));
  39.   BlockRead(f, data[1], length(data));
  40.   CloseFile(f);
  41. end;
  42.  
  43. procedure Search(AHexString: string);
  44. var dataToSearch: RawByteString;
  45.   nextValueStr: string;
  46.   nextValueByte: byte;
  47.   errorPos, index: integer;
  48. begin
  49.   repeat
  50.     AHexString := Trim(AHexString);
  51.     nextValueStr := copy(AHexString,1,2);
  52.     delete(AHexString,1,2);
  53.     val('$'+nextValueStr, nextValueByte, errorPos);
  54.     if errorPos = 0 then
  55.     begin
  56.       dataToSearch += chr(nextValueByte);
  57.     end;
  58.   until AHexString = '';
  59.   if length(dataToSearch) > 0 then
  60.   begin
  61.     index := pos(dataToSearch, data)-1;
  62.     if index = 0 then
  63.       writeln('Data not found')
  64.     else
  65.       writeln('Data found at position ', index);
  66.   end;
  67. end;
  68.  
  69. begin
  70.   LoadData('waterdrops.gif');
  71.   DisplayData;
  72.   Search('DE EA 9F');
  73.   readln;
  74. end.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018