Author Topic: Convert hex integer values to base-10 values  (Read 3798 times)

0 Members and 1 Guest are viewing this topic.

Offline shdqb64

  • Newbie
  • Posts: 3
    • View Profile
Convert hex integer values to base-10 values
« on: May 02, 2018, 08:35:05 am »
If this is the wrong spot for my question, I apologize, but this is my first post to the QB64 Forum ... ever.

I have a file containing fixed-length records, with each record containing fixed-length fields.  Records' fields contain mixed data types.  For example, some fields are ASCII alphanumeric, others a 2-byte integers containing hex values, and yet others are 8-byte reals.

The issue I have is converting the hex integer fields into base-10 decimal fields, so that when I print them, they are in a human-readable form, not in hex.

A sample record might start like this:


00 0C 31 30 30 30 30 2d 30 31 30 39 2d 30 32 2d 4d 30 31 20 20 20 20 20 20

The first two bytes are an integer, with a hex value of "000C," that I want to change to its base-10 equivalent, which is 12.  It is the 12 that I want to print in a report.

How can I do this in QB64?

I have hunted far and wide for a method, and tried various combinations of QB64's data manipulation/conversion functions (e.g., VAL), all to no avail.

Maybe a conversion is not possible, but that's why I ask.

Thanks in advance for any help!

FellippeHeitor

  • Guest
Re: Convert hex integer values to base-10 values
« Reply #1 on: May 02, 2018, 08:41:45 am »
Try adding "&H" before the values you want to convert with VAL() and make sure there are no spaces in the string:

Code: QB64: [Select]
  1. YourValue$ = "000C"
  2. PRINT VAL("&H" + YourValue$)

Welcome to the forum!
« Last Edit: May 02, 2018, 08:43:42 am by FellippeHeitor »

Offline shdqb64

  • Newbie
  • Posts: 3
    • View Profile
Re: Convert hex integer values to base-10 values
« Reply #2 on: May 02, 2018, 01:54:37 pm »
FellippeHeitor,

Thanks for the response.  Alas, it did not work.

After thinking about it, if I create a string that contains "000C," the string itself is 4 bytes long, with each byte containing the ASCII numeric value that corresponds to each alphnumeric character:  30 (zero), 30 (zero), 30 (zero), and 43 (the letter C).

In my case, however, the 2-byte hex integer fields are further subdivided into 4-bit (i.e., nibble) pieces, with each 4-bit chunk being able to contain a hex value from 0 - F.

By prefixing a string I create with the &H, the VAL command will, in fact, convert it correctly to a base-10 value.

But prefixing my 2-byte hex integer field with the &H yields incorrect results, because VAL is expecting to find valid ASCII values for the alphanumeric digits 0 - 9.

It looks like what I really need to be able to do is to break an 8-bit byte into 2 x 4-bit pieces, and then analyze each 4-bit piece.

I'm not sure QB64 can manipulate things down to a 4-bit level.  I may end up having to take up assembler in order to process my file.

Thanks again for any help!

FellippeHeitor

  • Guest
Re: Convert hex integer values to base-10 values
« Reply #3 on: May 02, 2018, 03:00:22 pm »
MKI$() converts an INTEGER into a two-byte string. CVI() converts said string back into an INTEGER.

MKL$() / CVL() for LONG.

MKS$() / CVS() for SINGLE.

MKD$() / CVD() for DOUBLE.

You get the point.

Try
Code: QB64: [Select]
  1. PRINT CVI(YourTwoByteSequence$)
« Last Edit: May 02, 2018, 03:10:12 pm by FellippeHeitor »

Offline shdqb64

  • Newbie
  • Posts: 3
    • View Profile
Re: Convert hex integer values to base-10 values
« Reply #4 on: May 03, 2018, 11:26:35 am »

FellippeHeitor,

Excellent!  I will try that command.

Given the amount of searching I did, I'm amazed I did not come across these commands.  Must be how I worded my searches, is all I can think of.

Thanks again!