Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file
Decoding a given string of data using a reference file Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
Go Back  Xtreme Visual Basic Talk > > > Decoding a given string of data using a reference file


Reply
 
Thread Tools Display Modes
  #1  
Old 01-12-2014, 02:27 PM
michals1984 michals1984 is offline
Newcomer
 
Join Date: Jan 2014
Posts: 3
Default Decoding a given string of data using a reference file


I don't even know where to start so any input will be greatly appreciated.

The application I'm trying to write should "decode" a given string of data.
For example something like this:

Gr04Hp300Y1900

using a reference file:

bl - blue
gr - green
wh - white
bc - black
etc

01 - Ford
02 - Mercedes
03 - Honda
04 - Doge
etc.

into something like this:

Green Doge, 300 horsepower, made in 1900

Any suggestions ?
Reply With Quote
  #2  
Old 01-13-2014, 09:55 AM
Gruff's Avatar
GruffDecoding a given string of data using a reference file Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Welcome to the forum michals. Do not forget to read the forum Posting Guidelines.

What you are talking about is parsing a string.
This is only possible if there is a consistent logic to your codes.
Since you have no delimiters separating your fields (spaces,commas,etc...)
you will have to depend on field size and order.

In this case [Color,Make,Horse Power, and Year] in that order.

The VB.net method you are going to need is .Substring() and select case statements.
Look them up in help.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 01-13-2014 at 10:00 AM.
Reply With Quote
  #3  
Old 01-13-2014, 11:27 PM
michals1984 michals1984 is offline
Newcomer
 
Join Date: Jan 2014
Posts: 3
Default

Thanks Gruff for giving me idea where to start.

I can edit the code and add delimiters if you think that this will make more sense.

I have one more question if you don't mind.
What kind of format should the reference file be? CSV, XML, some kind of DB? Because the way I see things it should be some kind of 2-columns table (array?)
Program searches the first column for a match (Gr) and when it finds it reads the value from nearby cell (Green)

Thank you for your invaluable help.
Reply With Quote
  #4  
Old 01-15-2014, 05:26 PM
Gruff's Avatar
GruffDecoding a given string of data using a reference file Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

For a simple table format you could get away with a csv format assuming you do not want to use commas in any of the column data. The added advantage to this format would be that you could always open the file in Excel if you needed to. As long as your file did not get really huge it would suffice. You would have to write your own search routines.

I do not think XML would buy you anything and your file would end up being much larger.

I would use a database if your data gets large or you need to make special cross referencing queries. i.e. "Show me all cars that are Dodge and Blue and older than 20 years." This is what a database shines at and the functionality is built in. Further you can
add or remove fields to your data without a lot of fuss.

(Fields are the equivalent of columns, Records are rows)

In a delimited file format like csv or a database you also have more flexibility as to your field encoded character size. The fixed character format you initially showed forces you to always use the same number of characters for each field.

Personally I would go with a database as it give you the most options.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #5  
Old 01-15-2014, 06:59 PM
Gruff's Avatar
GruffDecoding a given string of data using a reference file Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

On another note you can use the ODBC database object to read a CSV file.
If you put a header at the top of the file that defines the fields (Columns) names
then you can write your query with those names.

Here is an example of a csv that will work.

Color,Make,HP,Year
Blue,Dodge,300,1949
Blue,Chevrolet,200,1926
Green,Fiat,125,1966
Blue,Dodge,500,2001
Red,Olds,325,1972
Red,Ford,225,1942
Yellow,Dodge,2004

Code to read the above file into a DataGridView control.

Code:
Imports System.Data.Odbc Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim sFolder As String = "C:\AAA\" Dim sFile As String = "Cars.txt" Dim sConn As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};" sConn &= "Dbq=" & sFolder & ";" sConn &= "HDR=YES;" Dim Conn As New OdbcConnection(sConn) Dim CMD As OdbcCommand Dim DV1 As DataView Dim DT As New DataTable Dim DA As New OdbcDataAdapter ' The following query will select only those lines from the csv ' where the color is blue and the make is dodge. ' (Should be only two of those.) Dim sSQL As String = "SELECT * FROM " & sFile & " WHERE Color = 'Blue' AND Make = 'Dodge'" CMD = New OdbcCommand(sSQL, Conn) Conn.Open() DA.SelectCommand = CMD Try DA.Fill(DT) Catch ex As Exception MessageBox.Show("My Error: " & ex.Message) End Try DV1 = DT.DefaultView DV1.AllowNew = False DV1.AllowEdit = False Conn.Close() Grid1.DataSource = DV1 End Sub End Class
Attached Images
File Type: png CSV Grid.png (11.4 KB, 5 views)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #6  
Old 01-15-2014, 11:48 PM
michals1984 michals1984 is offline
Newcomer
 
Join Date: Jan 2014
Posts: 3
Default

Thank you again. You gave me way more than I expected.
Now I know at least where to start.
I will create a simple csv file that will look more or less like this:

cr01,ford
cr02,doge
cr03,mercedes
cr04,bmw
cr05,fiat
gr,green
bl,black
bk,black
wh,white
rd,red
hp200,200 Horsepower
hp340,340 Horsepower
hp140,140 Horsepower
hp220,220 Horsepower
hp160,160 Horsepower

that I will use as a reference for encoding the code: cr01|gr|hp200|
by using SPLIT function and once split I will use find and replace to make it readable.

Is there a way to have some control over the "translated" text?
Let's say instead of having: Ford Green 200 Horsepower
I would rather have something that makes little bit more sense.
"Green Ford with a 200 Horsepower motor"

Can I build some generic form (a sentence) with gaps that will be filled by the translated text ?

And last but not least. I owe you big time for all the help so I would like to buy you a coffee/beer/lunch, but since there is no dinner in NYC that delivers coffee to Oregon (weird right?) can I ask you for you paypal email so I can at least Say "thank you" in a little more appropriate way.
Reply With Quote
  #7  
Old 01-16-2014, 10:52 AM
Gruff's Avatar
GruffDecoding a given string of data using a reference file Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

First. Thank you for the kind thought, but no need. We are here to teach. When someone learns the craft that is reward enough.

I spent the better part of the 70's in NYC. Exciting times.

Second. If you feel you have to encrypt your data (gr=Green, cr01 = ford, Etc...)
You should use a separate CSV file to contain that functionality as it would interfere with
my previous sample. That is to say querying a CSV containing a list of cars.

BTW I don't see why it would be necessary to put "HP" in front of your horsepower data or "Y" in front of year data You do not need to translate those at all.

I guess I would read the encryption file in a more standard manner split each line by comma and populate a dictionary object in memory. This will make your translation lookups blindly quick.

Third. You can create more friendly output by joining text together using the ampersand character. (&)
Text can be literal. ("This is some text") or stored in a variable.

Code:
Private Sub Build_Output Dim sColor as string = "Green" Dim sMake as string = "Ford" Dim sHP as string = "200" Dim sYear as string = "1975" Dim sOutput as string = sColor & " " & sMake & " with a " & sHP & Horsepower motor" MessageBox.Show(sOutput) End Sub
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 01-16-2014 at 11:02 AM.
Reply With Quote
Reply

Tags
csv, decode


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
 
Decoding a given string of data using a reference file
Decoding a given string of data using a reference file
 
-->