简体   繁体   中英

Convert in reverse ascii to whole decimal in Java

Hi all and thank you for the help in advance.

I have scoured the webs and have not really turned up with anything concrete as to my initial question.

I have a program I am developing in JAVA thats primary purpose is to read a.DAT file and extract certain values from it and then calculate an output based on the extracted values which it then writes back to the file.

The file is made up of records that are all the same length and format and thus it should be fairly straightforward to access, currently I am using a loop and and an if statement to find the first occurrence of a record and then through user input determine the length of each record to then loop through each record.

HOWEVER. The first record of this file is a blank (Or so I thought). As it turns out this first record is the key to the rest of the file in that the first few chars are ascii and reference the record length and the number of records contained within the file respectively.

below are a list of the ascii values themselves as found in the files (Disregard the " " the ascii is contained within them)

"#¼ ä "

"#g â "

"ÇG @ "

"lj ‰ "

"Çò È "

"=¼ "

A friend of mine who many years ago use to code in Basic recons the first 3 chars refer to the record length and the following 9 refer to the number of records.

Basically what I am needing to do is convert this initial string of ascii chars to two decimals in order to work out the length of each record and the number of records.

Any assistance will be greatly appreciated.

Edit...

Please find below the Basic code used to access the file in the past, perhaps this will help?

    CLS
    INPUT "Survey System Data File? : ", survey$
    survey$ = "f:\apps\survey\" + survey$
    reclen = 3004
    OPEN survey$ + ".dat" FOR RANDOM AS 1 LEN = reclen
    FIELD #1, 3 AS RL$, 9 AS n$
    GET #1, 1
    RL = CVI(RL$): n = CVI(n$)
    PRINT "Record Length     = "; RL
    reclen = RL
    PRINT "Number of Records = "; n
    CLOSE #1

Basically what I am looking for is something similar but in java.

ASCII is a special way to translate a bit pattern in a byte to a character, and that gives each character a numerical value; for the letter 'A' is this 65.

In Java, you can get that numerical value by converting the char to an int (ok, this gives you the Unicode value, but as for the ASCII characters the Unicode value is the same as for ASCII, this does not matter).

But now you need to know how the length is calculated: do you have to add the values? Or multiply them? Or append them? Or multiply them with 128^p where p is the position, and add the result? And, in the latter case, is the first byte on position 0 or position 3?

Same for the number of records, of course.

Another possible interpretation of the data is that the bytes are BCD encoded numbers. In that case, each nibble (4bit set) represents a number from 0 to 9. In that case, you have to do some bit manipulation to extract the numbers and concatenate them, from left (highest) to right (lowest). At least you do not have to struggle with the sequence and further interpretation here …

But as BCD would require 8-bit, this would be not the right interpretation if the file really contains ASCII, as ASCII is 7-bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM