简体   繁体   English

在 Java 中将反向 ascii 转换为整数十进制

[英]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.我有一个正在 JAVA 中开发的程序,其主要目的是读取 .DAT 文件并从中提取某些值,然后根据提取的值计算 output 然后将其写回文件。

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.该文件由长度和格式相同的记录组成,因此访问起来应该相当简单,目前我正在使用循环和 if 语句来查找记录的第一次出现,然后通过用户输入确定每条记录的长度,然后循环遍历每条记录。

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.事实证明,这第一条记录是文件 rest 的关键,因为前几个字符是 ascii 并分别引用文件中包含的记录长度和记录数。

below are a list of the ascii values themselves as found in the files (Disregard the " " the ascii is contained within them)下面是在文件中找到的 ascii 值本身的列表(忽略 " " ascii 包含在其中)

"#¼ ä " “#¼ ä”

"#g â " “#g-”

"ÇG @ " “ÇG@”

"lj ‰ " “C‰ ‰ ”

"Çò È " “ÇòÈ”

"=¼ " “=¼”

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.我的一位朋友多年前使用 Basic 编码,前 3 个字符指的是记录长度,后面的 9 个字符指的是记录数。

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.基本上我需要做的就是将这个 ascii 字符的初始字符串转换为两位小数,以便计算出每条记录的长度和记录数。

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.基本上我正在寻找的是类似的东西,但在 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; ASCII 是一种将字节中的位模式转换为字符的特殊方法,它赋予每个字符一个数值; for the letter 'A' is this 65.因为字母“A”是这个 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).在 Java 中,您可以通过将 char 转换为 int 来获得该数值(好的,这为您提供了 Unicode 值,但对于 ASCII 字符 Z7F6C02D96265DD1D347B1101 与 ASCII 值相同,DE1101 无关紧要)。

But now you need to know how the length is calculated: do you have to add the values?但是现在您需要知道长度是如何计算的:您必须添加这些值吗? Or multiply them?还是将它们相乘? Or append them?还是 append 他们? Or multiply them with 128^p where p is the position, and add the result?或者将它们乘以 128^p,其中 p 是 position,然后将结果相加? And, in the latter case, is the first byte on position 0 or position 3?而且,在后一种情况下,第一个字节是 position 0 还是 position 3?

Same for the number of records, of course.当然,记录数也是如此。

Another possible interpretation of the data is that the bytes are BCD encoded numbers.数据的另一种可能解释是字节是 BCD 编码的数字。 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).在这种情况下,每个半字节(4 位集合)代表一个从 0 到 9 的数字。在这种情况下,您必须进行一些位操作来提取数字并将它们从左(最高)到右(最低)连接起来。 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.但是由于 BCD 需要 8 位,如果文件确实包含 ASCII,这将不是正确的解释,因为 ASCII 是 7 位的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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