Link to home
Start Free TrialLog in
Avatar of jdmaxey
jdmaxey

asked on

Converting Binary to Hex

I am looking for a short code segment to convert an eight digit Binary number to Hex. I don't need the code, just some suggestions on what path to take. Thanks.
Avatar of vdp09
vdp09

does your 8-digit binary represents 2-digit decimal number

eg..

one possiblity,
1001 1111 => 9 f

another possibility

1001 1111 => 159


for 1st case  just take out 4 digits and covert to hex

for second case, i think you have to covert,

binary=>decimal
decimal=>hex

Do you mean binary data to 0 - f in characters?
If you do, the shortest code to do it on an intel processor is:

for bits in al (ie. 0 - 0fh)

Do you mean binary data to 0 - f in characters?
If you do, the shortest code to do it on an intel processor is:

for bits in al (ie. 0 - 0fh)

SOLUTION
Avatar of Dancie
Dancie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jdmaxey

ASKER

vdp09 & Dancie...I asked the question initially from memory and I was off base. Let me try to clarify my problem now from the instructions. The actual instructions are "Convert an eight bit number from it's hex value to it's ASCII equivalent. The value to be converted is passed into the program by the AL register and returned in the AX register. Lower bit in AL and higher in AH. I have to use the 8540 command s al=? where ? is the value to convert.

I can't believe my memory is that bad, age I guess. I'm a returning student! I greatly appreciate your help. I am new to the forum and have started to use it for my job as well. Good group of professionals here!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jdmaxey

ASKER

EB...Thank you. I worked on it this weekend and that is the final approach I came up with. I cpoied the contents from AL to AH, then I used shift right and left to isolate the appropriate digit in the appropriate register, then I compared each register to "A", if it was below I added 30, if it was above I added 37. This worked out great, thanks for your input.
Thanks jdmaxey
Could someonep provide the code for this because I would like to learn now to do it.

Cheers

Claudio
Pallone
Here is the code for a more efficient way of doing the conversion
I presume you wanted it in assembly language?

;The number to be converted is in the DX register
bindec      PROC      near
      push      bp
        mov      bp,sp
        push      cx dx

        ;Get binary number to convert
        mov      cx,8
        mov      dx,0      

bin1:        ; Shift result 1 bit left
        mov      al,dl
        add      al,al
        daa
        mov      dl,al
        mov      al,dh
        adc      al,al
        daa
        mov      dh,al

        ;Get binary number to convert
        mov      ax,[bp+4]

        rol      al,1            ;shift operand 1 bit left
        mov      [bp+4],ax

        jnc      bin2            ;don't modify result if NC
        mov      al,dl            ;result = result + 1
        add      al,1
        daa
        mov      dl,al
        mov      al,dh
        daa
        mov      dh,al
        mov      al,ah
        daa
        mov      ah,al
bin2:        loop      bin1
        mov      ax,dx       
      pop      dx cx
      pop      bp
      ret      2
bindec      ENDP
Hi and many thanks for your reply.

Actually I am trying to learn how to do it in c# and would appreciate some code example. I am trying to convert from binary like 011000110 to HEX and then from HEX to 011000110.

Thanks for your help.

Cheers,

Claudio
Sorry Pallone
The number to convert was pushed onto the stack prior to callihg this subroutine

EB
Sorry but did you see my message?

Hi and many thanks for your reply.

Actually I am trying to learn how to do it in c# and would appreciate some code example. I am trying to convert from binary like 011000110 to HEX and then from HEX to 011000110.

Thanks for your help.

Cheers,

Claudio
Hi Pallone, do you want to convert an integer value to hex in ASCII form and back again? Like for display purposes?
Yes I got your reply but I was typing at the time... didn't see it until I reloaded the quesiton
Yeap, I want to learn how to work with binary and Hex in c#