Jump to content
xisto Community
Sign in to follow this  
Chez

Bin To Hex Converting?

Recommended Posts

I'm trying to convert a 32-bit Hex string into 32-bit binary and then back.Whats the theory behind this? I was checking online and some articles said check each char divided against a power of 10. ... ? A bit confused here. Any help appreciated.using SPIMon Fedora Core platform

Edited by Chez (see edit history)

Share this post


Link to post
Share on other sites

Yes, hexadecimal. But I was actually looking for code help, not an actual converter. thanks though.I can convert from decimal to binary simply using a recurring function with remainders...recurring function:47 / 2 gives DIV=23 MOD=1, bin string = 123 / 2 gives DIV=11 MOD=1, bin string = 1111/2 gives DIV=5 MOD=1, bin string = 1115/2 gives DIV=2 MOD=1, bin string = 11112/2 gives DIV=1 MOD=0, bin string = 011111/2 gives DIV=0 MOD=1, bin string = 101111but Hex is harder to code because it has letters as well as numbers. So I'm not sure how I can create a function that will do that.not looking for code, just how would you go about converting hex to binary and then back again?

Edited by Chez (see edit history)

Share this post


Link to post
Share on other sites

ok, so I figured it out myself after days of reading up on the complexities of MIPS architecture. I basically IO-ed the HEX input into a register and used 'lb' to grab each character (by cooredinating ascii value). this is the part I had problems with previously: I didn't know the loaded byte would be converted to binary in the register, so I was origionally comparing ascii values. But lo and behold, Assembly language did not completely let me down once I figured you could compare chars as in C or Java simply by bordering your target char with single quotes. Once I figured that out, the whole comparison was a peice of cake. Finding, say, '3' would run through the converter loop and input 0011 to a string in another register, srl 4, increase position, and continue. Fairly easy once I got the jist of it. code follows:

# This program inputs a Hex number and# converts it to Binary. Then alters the
# Binary number and outputs new Hex number.

.data
PROMPT: .asciiz "Enter Hex: "
ORI: .asciiz " "
RES: .asciiz " "
OUT: .asciiz " -> "
.text
main:
##################################
# INITIALIZE REGISTERS #
##################################
li $s0, 0 #INPUT HEX
li $s1, 0 #TEMP BYTE STORAGE
li $s2, 0 #I
li $s3, 0 #BINARY NUMBER
li $s4, 0 #TEMP BINARY NUMBER
li $s5, 0 #TEMP BINARY CHAR
la $s6, ORI #ORIGIONAL HEX
la $s7, RES #FINAL HEX

##################################
# MAIN #
##################################
j INPUT #PROMT FOR HEX AND STORE IT
START: j HIN #HEX INPUT TO BINARY STRING
L2: j BINC #BINARY CONVERTER
L3: add $s7, $s7, 7
j HOUT #HEX OUTPUT FROM BINARY STRING
L4: j PRINT #PRINT AND END

###################################
# READ AND STORE INPUT HEX #
###################################
INPUT: la $a0, PROMPT #LOAD THE ADDRESS OF PROMPT FOR HEX
li $v0, 4 #SYSTEM CALL FOR THE PRINT STRING
syscall #PRINT THE PROMPT
li $v0, 8 #SYSTEM CALL TO READ IN A STRING
move $a0,$s6
syscall #READ IN HEX
move $s0, $v0 #COPY ORIGIONAL HEX VALUE INTO s0
move $s0,$s6 #PUT TEMP HEX VALUE INTO s6
j START #CONVERT TO BINARY STRING

###################################
# HEX INPUT #
###################################

HIN: beq $s2,8,L2 #IF END OF HEX INPUT(8), BREAK
lb $s1,0($s0) #STORE CURRENT HEX CHAR IN $s1
sll $s3,$s3,4 #SHIFT LEFT 4 BITS FOR NEXT INPLACE
beq $s1,'0',IF0 #IF CHAR= 0
beq $s1,'1',IF1 #IF CHAR= 1
beq $s1,'2',IF2 #IF CHAR= 2
beq $s1,'3',IF3 #IF CHAR= 3
beq $s1,'4',IF4 #IF CHAR= 4
beq $s1,'5',IF5 #IF CHAR= 5
beq $s1,'6',IF6 #IF CHAR= 6
beq $s1,'7',IF7 #IF CHAR= 7
beq $s1,'8',IF8 #IF CHAR= 8
beq $s1,'9',IF9 #IF CHAR= 9
beq $s1,'a',IFA #IF CHAR= A
beq $s1,'b',IFB #IF CHAR= B
beq $s1,'c',IFC #IF CHAR= C
beq $s1,'d',IFD #IF CHAR= D
beq $s1,'e',IFE #IF CHAR= E
beq $s1,'f',IFF #IF CHAR= F
IF0: ori $s3,$s3,0
j RUTGER
IF1: ori $s3,$s3,1
j RUTGER
IF2: ori $s3,$s3,2
j RUTGER
IF3: ori $s3,$s3,3
j RUTGER
IF4: ori $s3,$s3,4
j RUTGER
IF5: ori $s3,$s3,5
j RUTGER
IF6: ori $s3,$s3,6
j RUTGER
IF7: ori $s3,$s3,7
j RUTGER
IF8: ori $s3,$s3,8
j RUTGER
IF9: ori $s3,$s3,9
j RUTGER
IFA: ori $s3,$s3,10
j RUTGER
IFB: ori $s3,$s3,11
j RUTGER
IFC: ori $s3,$s3,12
j RUTGER
IFD: ori $s3,$s3,13
j RUTGER
IFE: ori $s3,$s3,14
j RUTGER
IFF: ori $s3,$s3,15
j RUTGER

RUTGER:
addi $s2,$s2,1 #INCREASE I
addi $s0,$s0,1 #INCREASE POSITION
j HIN #RESTART LOOP
###################################
# BINARY CONVERTER #
###################################
BINC: li $s1,0 #CLEAR TEMP BYTE STORAGE
li $s2,0 #CLEAR I
srl $s4,$s3,1 #SHIFT RIGHT 1
xor $s3,$s3,$s4 #EXCLUSIVE OR TO CHANGE BINARY STRING
j L3 #NEXT STEP

###################################
# HEX OUTPUT #
###################################

HOUT: beq $s2,8,L4 #END OF BINARY STRING, BREAK
and $s5,$s3,0xf #GET CHAR
srl $s3,$s3,4 #'TYPEWRITTER' SHIFT RIGHT

beq $s5,0,IF0000 #IF BYTE= 0000
beq $s5,1,IF0001 #IF BYTE= 0001
beq $s5,2,IF0010 #IF BYTE= 0010
beq $s5,3,IF0011 #IF BYTE= 0011
beq $s5,4,IF0100 #IF BYTE= 0100
beq $s5,5,IF0101 #IF BYTE= 0101
beq $s5,6,IF0110 #IF BYTE= 0110
beq $s5,7,IF0111 #IF BYTE= 0111
beq $s5,8,IF1000 #IF BYTE= 1000
beq $s5,9,IF1001 #IF BYTE= 1001
beq $s5,10,IF1010 #IF BYTE= 1010
beq $s5,11,IF1011 #IF BYTE= 1011
beq $s5,12,IF1100 #IF BYTE= 1100
beq $s5,13,IF1101 #IF BYTE= 1101
beq $s5,14,IF1110 #IF BYTE= 1110
beq $s5,15,IF1111 #IF BYTE= 1111
IF0000: li $t0,'0'
sb $t0,0($s7)
j MUTGER
IF0001: li $t0,'1'
sb $t0,0($s7)
j MUTGER
IF0010: li $t0,'2'
sb $t0,0($s7)
j MUTGER
IF0011: li $t0,'3'
sb $t0,0($s7)
j MUTGER
IF0100: li $t0,'4'
sb $t0,0($s7)
j MUTGER
IF0101: li $t0,'5'
sb $t0,0($s7)
j MUTGER
IF0110: li $t0,'6'
sb $t0,0($s7)
j MUTGER
IF0111: li $t0,'7'
sb $t0,0($s7)
j MUTGER
IF1000: li $t0,'8'
sb $t0,0($s7)
j MUTGER
IF1001: li $t0,'9'
sb $t0,0($s7)
j MUTGER
IF1010: li $t0,'a'
sb $t0,0($s7)
j MUTGER
IF1011: li $t0,'b'
sb $t0,0($s7)
j MUTGER
IF1100: li $t0,'c'
sb $t0,0($s7)
j MUTGER
IF1101: li $t0,'d'
sb $t0,0($s7)
j MUTGER
IF1110: li $t0,'e'
sb $t0,0($s7)
j MUTGER
IF1111: li $t0,'f'
sb $t0,0($s7)
j MUTGER

MUTGER: sub $s7,$s7,1 #INCREASE POSITION
addi $s2,$s2,1 #INCREASE I
j HOUT #RESTART LOOP

###################################
# PRINT THE OUTPUT #
###################################
PRINT: sb $0,8($s6) #Seperate the inputs(/n -> 0)

la $s7, RES #RESET $s7
li $v0, 4 #LOADS SYSTEM CALL TO PRINT STRING
move $a0, $s6 #LOADS VALUE OF ORIGIONAL HEX TO CALL
syscall #PRINT MIN

la $a0, OUT #LOAD ADDRESS FOR FIRST STRING PART
li $v0, 4 #SYSTEM CALL TO PRINT STRING
syscall #PRINT STRING (" -> ")

li $v0, 4 #LOADS SYSTEM CALL TO PRINT STRING
move $a0, $s7 #LOADS VALUE OF FINALIZED HEX TO CALL
syscall #PRINT MIN

jr $ra #EXIT
#END PROGRAM


I'm sure it's not as optimimized as I would like, and I'm sure I could drop the number of registers down to around 4, but heck, it works and that'll all I care about.
*why Rutger and Mutger... I don't know. don't ask. I was running out of names for methods.

Share this post


Link to post
Share on other sites

3 years ago I wrote HexToBin function.

function HexToBin(Hexadecimal: string): string;const  BCD: array [0..15] of string =	('0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',	'1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111');var  i: integer;begin  for i := Length(Hexadecimal) downto 1 do	Result := BCD[StrToInt('$' + Hexadecimal[i])] + Result;end;procedure TForm1.Button1Click(Sender: TObject);begin  ShowMessage(HexToBin('FFA1'));  // Returns 1111111110100001end;

But for BinToHex(s:string) converting you can use standard BinToHex function in Math module.

And such code:
BinToHex = DecToHex(BinToDec(Bin)); //maybe works!

good luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.