0 Replies - 1678 Views - Last Post: 28 March 2009 - 12:12 PM

#1 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

HexString to Long

Posted 28 March 2009 - 12:12 PM

Description: Note that although this is case insensitive for the letters A-F, it has no error handling so the string must be correct.This function (cdecl calling convention) will convert a hex string into a long value.
; EXAMPLE C program:
;
; #include 
; #define DEBUG_DISINTVALUE(var) printf("%s = %dn", #var, var);
; 
; extern long hex2num(const char *); 
;
; int main() {
; 	 int a = hex2num("12AbCdEf");
; 	 DEBUG_DISINTVALUE(a);
;	 return 0;
; }


.386p
PUBLIC _hex2num

MODEL flat, C
_TEXT	segment dword public use32 'CODE'

_hex2num proc near ; extern "C" long hex2num(const char *ptr)
	push ebp							
	mov ebp, esp 						; set base pointer to stack pointer
		mov edx, dword ptr [ebp+8] 	; place the pointer into edx
		xor ecx, ecx 					; zero our return value
		and eax, 0FFh					; mask eax to just al.
beginWhile: 							; while (*ptr !=0) 
		cmp byte ptr [edx], 0 			; test to see if *ptr == 0
		je short endWhile 				; if so, jump over loop.
		mov al, [edx] 					; load a byte form the string
		sub al, 30h 					; al = al - '0' -- subtract 48
		
		cmp al, 09h						; if (al <= 9) jmp doCalc 
		jbe doCalc
		
		sub al, 07h						; al = al - ('A' - '0') + 10 
		
		cmp al, 0Fh						; if (al <= 15) jmp doCalc
		jbe doCalc
		
		sub al, 20h						; al = al - ('a' - 'A')
doCalc:		
		add ecx, ecx					; ecx = 2 ecx
		lea ecx, dword ptr [8*ecx]		; ebx = 8 * ecx = 16 * original_ecx neat eh! no need for mult
		add ecx, eax					; ecx += al
		inc edx
		jmp short beginWhile
endWhile:
		mov eax, ecx					; return the value
	pop ebp
	ret
_hex2num endp

_TEXT	ends

END


Is This A Good Question/Topic? 0
  • +

Replies To: HexString to Long

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: HexString to Long

Posted 28 March 2009 - 12:12 PM

Description: This is a C style function so you must pass the parameters on the stack. Push the pointer onto the stack before you call. *** added error checking will now only read 8 chars and will stop on invalid chars.This function (cdecl calling convention) will convert a hex string into a long value.
; EXAMPLE C program:
;
; #include <stdio.h>
; #define DEBUG_TESTCASE(testCase, var) var = hex2num( testCase); 
;    printf("Test: "%15s" = 0x%08X = %un",testCase, var, var);
; 
;extern long hex2num(const char *); 
;
; int main() {
;      unsigned long a;
;     DEBUG_TESTCASE("1234ABCD", a);
;     DEBUG_TESTCASE("5678efab", a);
;     DEBUG_TESTCASE("FfFfFfFf", a);
;     DEBUG_TESTCASE("3.141593", a);
;     DEBUG_TESTCASE("12345678ABCD", a);
;     DEBUG_TESTCASE("JAVABABE", a);
;     DEBUG_TESTCASE("CafeBabe", a);
;     DEBUG_TESTCASE("[email protected]", a);
;     DEBUG_TESTCASE("0000123^", a);
;     DEBUG_TESTCASE("10000000", a);
;     DEBUG_TESTCASE(   "10000", a);
;     DEBUG_TESTCASE(     "100", a);
;     DEBUG_TESTCASE(       "1", a);
;     DEBUG_TESTCASE("a0000000", a);
;     DEBUG_TESTCASE(   "A0000", a);
;     DEBUG_TESTCASE(     "F00", a);
;     DEBUG_TESTCASE(       "f", a);
;     DEBUG_TESTCASE("100x01", a);
;     return 0;
; }


.386p
PUBLIC _hex2num

MODEL flat, C
_TEXT    segment dword public use32 'CODE'

_hex2num proc near                     ; extern "C" long hex2num(const char *ptr)
    push ebp                            
    mov ebp, esp                       ; set base pointer to stack pointer
    push ebx
        mov edx, dword ptr [ebp+8]     ; place the pointer into edx
        xor ebx, ebx                   ; zero ebx
        xor ecx, ecx                   ; zero our return value
        and eax, 0FFh                  ; mask eax to just al.
beginWhile:                            ; while (*ptr !=0) 
        cmp byte ptr [edx], 0          ; test to see if *ptr == 0
        je short endWhile              ; if so, jump over loop.
        cmp ebx, 08h                   ; a long is only 8 bytes long
        je short endWhile              ; if we have read 8 chars end.
        mov al, [edx]                  ; load a byte form the string
        sub al, 30h                    ; al = al - '0' -- subtract 48

        cmp al, 09h                    ; if (al <= 9) jmp doCalc
        jbe doCalc
        cmp al, 11h                    ; Check chars between '0' and 'A'
        jb endWhile
        
        sub al, 07h                    ; al = al - ('A' - '0') + 10 (sub al, 11h, add al, 0xA)
        cmp al, 0Fh                    ; if (al <= 15) jmp doCalc
        jbe doCalc
        cmp al, 2Ah                    ; Check chars betweeen 'A' and 'a'
        jb endWhile
        
        sub al, 20h                    ; al = al - ('a' - 'A')
        
        cmp al, 0Fh                    ; if (al > 15) then we have an illegal char
        ja endWhile                    ;   so we exit the proc as is.
        
doCalc:        
        add ecx, ecx                   ; ecx = 2 ecx
        lea ecx, [8*ecx]               ; ebx = 8 * ecx = 16 * original_ecx neat eh! no need for mult
        add ecx, eax                   ; ecx += al
        inc edx                        ; inc ptr
        inc ebx                        ; inc count
        jmp short beginWhile
endWhile:
        mov eax, ecx                   ; return the value
    pop ebx
    pop ebp
    ret
_hex2num endp

_TEXT    ends

END

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1