hex string -> hex number
Author Message hex string -> hex number

Hi

I need a routine (basic, asm, whatever) that can convert a hexadecimal
string to a hexadecimal number (for calculations).
Can anyone help me with that? Thanks!

Mon, 21 Aug 2000 03:00:00 GMT  hex string -> hex number

Quote:

>Hi

>I need a routine (basic, asm, whatever) that can convert a hexadecimal
>string to a hexadecimal number (for calculations).
>Can anyone help me with that? Thanks!

BASIC only calculates in decimal (base 10).  You can convert the hex strings
to decimal, do the calculations and convert the answer back to a hex string:

LINE INPUT "Enter first number (in hex): ", h1\$
LINE INPUT "Enter second number (in hex): ", h2\$
h1 = VAL("&H" + h1\$)
h2 = VAL("&H" + h2\$)
sum = h1 + h2
hexsum\$ = hex\$(sum)
PRINT "The sum of "; h1\$; " and "; h2\$;" is "; hexsum\$

Tom Lake

Mon, 21 Aug 2000 03:00:00 GMT  hex string -> hex number

Hi Tom,

Quote:
> BASIC only calculates in decimal (base 10).  You can convert the hex strings
> to decimal, do the calculations and convert the answer back to a hex string:

That would be a solution yes. However, the freeware basic compiler I use
can't convert a hex string to a decimal one that way...:(
A good hex string -> decimal number would be great too, the trouble is
the conversion from a hex string to a number (either hexadecimal or
decimal).

Mon, 21 Aug 2000 03:00:00 GMT  hex string -> hex number

Quote:

> Hi Tom,

> > BASIC only calculates in decimal (base 10).  You can convert the hex strings
> > to decimal, do the calculations and convert the answer back to a hex string:

> That would be a solution yes. However, the freeware basic compiler I use
> can't convert a hex string to a decimal one that way...:(
> A good hex string -> decimal number would be great too, the trouble is
> the conversion from a hex string to a number (either hexadecimal or
> decimal).

Actually, BASIC generally calculates in binary.  Almost all computer arithmetic
is done in binary.  BASIC merely displays and accepts numbers in decimal.  They
are converted to binary for processing.  Here is a QBasic/QuickBasic program
that converts a Hex string to a LONG integer, and prints it in decimal.

'  **************************************************
'  *                                                *
'  *                  HEX2DEC.BAS                   *
'  *                                                *
'  *        Convert a Hex Number to Decimal         *
'  *                                                *
'  *              Judson D. McClendon               *
'  *              Sun Valley Systems                *
'  *              329 37th Court N.E.               *
'  *              Birmingham, AL 35215              *
'  *                 205-853-8440                   *
'  *                                                *
'  **************************************************
'
DECLARE FUNCTION Hex2Dec# (HexNbr AS STRING)
DIM Nbr AS LONG, HexNbr AS STRING

CLS
DO
INPUT "Enter Number in Hex (Enter to quit): "; HexNbr
Nbr = Hex2Dec#(HexNbr)
IF (Nbr < 0) THEN
PRINT "Invalid Hex Number"
ELSE
PRINT "Decimal: "; Hex2Dec#(HexNbr)
END IF
LOOP UNTIL (HexNbr = "")
END

FUNCTION Hex2Dec# (HexNbr AS STRING)
DIM Nbr AS LONG, Digit AS LONG, P AS INTEGER

Nbr = 0
FOR P = 1 TO LEN(HexNbr)
Digit = INSTR("0123456789ABCDEF", UCASE\$(MID\$(HexNbr, P, 1))) - 1
IF (Digit < 0 OR Digit > 15) THEN
Nbr = -1
EXIT FOR
END IF
Nbr = Nbr * 16 + Digit
NEXT
Hex2Dec# = Nbr
END FUNCTION
--
Judson McClendon          This is a faithful saying and worthy of all
Sun Valley Systems        acceptance, that Christ Jesus came into the

(please remove zzz from email id to respond)

Mon, 21 Aug 2000 03:00:00 GMT  hex string -> hex number

Thanks to everyone wo helped me with this. This is the assembly
subroutine I used and it works great!

Comment %

String => Number (*HEXADECIMAL* - 64 bit)
Input:  ECX     = Length of string 8 (obviously max 8)
DS:ESI  = Pointer to string (numbers A-F must be in uppercase)
Output: EDX:EAX (number value of input string)
Registers changed: EDX & EAX
Flags : none

%

str2hnum PROC
pushf
push ebx ecx edi esi ebp

mov EBP, 10h                    ; EBX =   Upperpart
xor eax, eax                    ; init
xor ebx, ebx                    ; init

nextchar:
xor edx, edx                    ; EDX=0 - needed ?
Imul ebx, ebx, 10h              ; Update upperpart
mul EBP                         ; Update lowerpart

movzx edi, byte ptr [esi]       ; fetch char
cmp edi, 40h
jl number
sub edi,7

number:
sub eax, 30h                    ; subtract "char" value

inc esi                         ; next letter
dec ecx                         ; oneless letter to go
jnz nextchar                    ; was it the last?

xchg edx, ebx                   ; output is EDX:EAX
pop ebp esi edi ecx ebx
popf
RET

str2hnum ENDP

Mon, 21 Aug 2000 03:00:00 GMT  hex string -> hex number

Quote:

>>I need a routine (basic, asm, whatever) that can convert a hexadecimal
>>string to a hexadecimal number (for calculations).
>BASIC only calculates in decimal (base 10).  You can convert the hex strings
>to decimal, do the calculations and convert the answer back to a hex string:

BASIC calculates binary (zeros and ones)!
However, when you print a number, it is shown in decimal form.
But for the rest, you're right. Just use VAL("&H" + HexString\$)
to convert a 'hexadecimal string' to a number.

Kind regards cq. De groeten,

GoofY
-- SpamInfo at http://www.stack.nl/~goofy/Spam
A world that's far away,
where life is not unkind,
the movie in my mind...

Tue, 22 Aug 2000 03:00:00 GMT  hex string -> hex number

how can i use assembly subroutines in VB5 ?

Quote:
> Thanks to everyone wo helped me with this. This is the assembly
> subroutine I used and it works great!

> Comment %

>         String => Number (*HEXADECIMAL* - 64 bit)
>         Input:  ECX     = Length of string 8 (obviously max 8)
>                 DS:ESI  = Pointer to string (numbers A-F must be in uppercase)
>         Output: EDX:EAX (number value of input string)
>         Registers changed: EDX & EAX
>         Flags : none

>          %

> str2hnum PROC
>         pushf
>         push ebx ecx edi esi ebp

>         mov EBP, 10h                    ; EBX =   Upperpart
>         xor eax, eax                    ; init
>         xor ebx, ebx                    ; init

> nextchar:
>         xor edx, edx                    ; EDX=0 - needed ?
>         Imul ebx, ebx, 10h              ; Update upperpart
>         mul EBP                         ; Update lowerpart

>         movzx edi, byte ptr [esi]       ; fetch char
>         cmp edi, 40h
>         jl number
>         sub edi,7

> number:
>         sub eax, 30h                    ; subtract "char" value

>         inc esi                         ; next letter
>         dec ecx                         ; oneless letter to go
>         jnz nextchar                    ; was it the last?

>         xchg edx, ebx                   ; output is EDX:EAX
>         pop ebp esi edi ecx ebx
>         popf
>         RET

> str2hnum ENDP

Tue, 22 Aug 2000 03:00:00 GMT  hex string -> hex number

Or

Dec2Hex\$ = HEX\$(Value#)
Hex2Dec# = VAL("&H"+Value\$)

Quote:

>> Hi Tom,

>> > BASIC only calculates in decimal (base 10).  You can convert the hex
strings
>> > to decimal, do the calculations and convert the answer back to a hex
string:

>> That would be a solution yes. However, the freeware basic compiler I use
>> can't convert a hex string to a decimal one that way...:(
>> A good hex string -> decimal number would be great too, the trouble is
>> the conversion from a hex string to a number (either hexadecimal or
>> decimal).

>Actually, BASIC generally calculates in binary.  Almost all computer
arithmetic
>is done in binary.  BASIC merely displays and accepts numbers in decimal.
They
>are converted to binary for processing.  Here is a QBasic/QuickBasic
program
>that converts a Hex string to a LONG integer, and prints it in decimal.

>'  **************************************************
>'  *                                                *
>'  *                  HEX2DEC.BAS                   *
>'  *                                                *
>'  *        Convert a Hex Number to Decimal         *
>'  *                                                *
>'  *              Judson D. McClendon               *
>'  *              Sun Valley Systems                *
>'  *              329 37th Court N.E.               *
>'  *              Birmingham, AL 35215              *
>'  *                 205-853-8440                   *
>'  *                                                *
>'  **************************************************
>'
>DECLARE FUNCTION Hex2Dec# (HexNbr AS STRING)
>DIM Nbr AS LONG, HexNbr AS STRING

>CLS
>DO
>   INPUT "Enter Number in Hex (Enter to quit): "; HexNbr
>   Nbr = Hex2Dec#(HexNbr)
>   IF (Nbr < 0) THEN
>      PRINT "Invalid Hex Number"
>   ELSE
>      PRINT "Decimal: "; Hex2Dec#(HexNbr)
>   END IF
>LOOP UNTIL (HexNbr = "")
>END

>FUNCTION Hex2Dec# (HexNbr AS STRING)
>   DIM Nbr AS LONG, Digit AS LONG, P AS INTEGER

>   Nbr = 0
>   FOR P = 1 TO LEN(HexNbr)
>      Digit = INSTR("0123456789ABCDEF", UCASE\$(MID\$(HexNbr, P, 1))) - 1
>      IF (Digit < 0 OR Digit > 15) THEN
>         Nbr = -1
>         EXIT FOR
>      END IF
>      Nbr = Nbr * 16 + Digit
>   NEXT
>   Hex2Dec# = Nbr
>END FUNCTION
>--
>Judson McClendon          This is a faithful saying and worthy of all
>Sun Valley Systems        acceptance, that Christ Jesus came into the

>(please remove zzz from email id to respond)

Tue, 22 Aug 2000 03:00:00 GMT  hex string -> hex number

John, we were told his BASIC compiler did not have a Hex to Decimal
conversion like that.  See below.
--
Judson McClendon          This is a faithful saying and worthy of all
Sun Valley Systems        acceptance, that Christ Jesus came into the

(please remove zzz from email id to respond)

Quote:

> Or

> Dec2Hex\$ = HEX\$(Value#)
> Hex2Dec# = VAL("&H"+Value\$)

> >> That would be a solution yes. However, the freeware basic compiler I use
> >> can't convert a hex string to a decimal one that way...:(

Tue, 22 Aug 2000 03:00:00 GMT

 Page 1 of 1 [ 13 post ]

Relevant Pages