Jul 25, 2008

Bin To Hex Converting?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Assembler and Delphi

free web hosting

Bin To Hex Converting?

Chez
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 SPIM
on Fedora Core platform

Reply

Cerebral Stasis
I'm assuming that by "Hx" you mean hexadecimal. You may want to try one of the free conversion scripts found here.

Reply

Chez
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 = 1
23 / 2 gives DIV=11 MOD=1, bin string = 11
11/2 gives DIV=5 MOD=1, bin string = 111
5/2 gives DIV=2 MOD=1, bin string = 1111
2/2 gives DIV=1 MOD=0, bin string = 01111
1/2 gives DIV=0 MOD=1, bin string = 101111

but 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?

Reply

Chez
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:

QUOTE

# 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.

 

 

 


Reply

DeveloperX
3 years ago I wrote HexToBin function.

CODE

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 1111111110100001
end;


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

And such code:
CODE

BinToHex = DecToHex(BinToDec(Bin)); //maybe works!


good luck!

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. bin to hex - 33.74 hr back. (1)
  2. 32bit bin to hex - 36.93 hr back. (1)
  3. hex to bcd conversion c program - 39.92 hr back. (1)
  4. bin hex - 77.25 hr back. (1)
  5. bin to hex converter - 81.30 hr back. (1)
  6. hex to bin function - 92.74 hr back. (1)
  7. mod 1000 hex - 96.87 hr back. (1)
  8. bintohex в delphi 7 - 105.63 hr back. (1)
  9. convert bin to hex - 106.60 hr back. (1)
  10. converting binary to hex asm programs - 116.52 hr back. (1)
  11. convert bin/hex codes in assembly language - 118.16 hr back. (2)
  12. c bin to hex - 132.94 hr back. (1)
  13. bin to hex asm - 133.39 hr back. (1)
Similar Topics

Keywords : bin, hex, converting

  1. Converting A Blogspot Account Into A .com Domain
    (0)
  2. Converting Audio Files In Vista
    how do you do it? (7)
    Ok, so I have some .wav files that I'd like converted into .mp3. I knew how to do it in XP, but
    I can't find windows audio converter in Vista....does vista even have that, or is it under a new
    name? Also, is it possible to convert video files, like .flv into .avi?....
  3. Converting Textarea Return Characters To <br />
    (8)
    Hi guys, I have set up a form with a textarea. If I have text on multiple lines such as: CODE
    here is some content here is some more content then when I print it out as html it will be
    returned on one line like so: CODE here is some contenthere is some more content When i
    look at the content in the database it looks just like the original way i entered it (contains the
    multiple lines). Is there a function in php so that when i spit it out in html it retains the
    multiple line format? Thanks in advance. ....
  4. Pc Crashs When Converting Divx With Tmpgenc
    driver/RAM problem? (4)
    when i convert a divx or avi with TMPGEnc my Pc crashs. the divx will be half way done than it will
    crash. i dont know why this is happening. i hope someone here will know why this is happening. heres
    a pic of what happens when i convert with TMPGEnc. if i move a divx file to another folder i get
    this error when it moves. what does this mean? QUOTE OS: Windows XP Home Edition, SP2 CPU:
    AuthenticAMD, AMD AMD Athlon™ XP 3000+, MMX @ 2160 MHz Application data:
    VmVyc2lvbjogVVVKZFExQlRWbHRFVFVOSFF5c29PU2M1QndBNkpUOCt MVzk3WjJOck1DRWpQeU1rTTBGeGVIQjBaWE01DQpJbWF....
  5. Avi To Dvd
    A tut on converting and burning (10)
    Forenote: I DO NOT ENCOURAGE PIRATING MOVIES. You will also need QUITE a lot of hard drive
    space to do this procedure. I recommend at least 10Gb free. Now, to get started. Convert
    your movie to M2V DVD players should always be able to play M2V format. Please note this
    step MAY take up to 12 hours, but for me, about 5. I leave it going overnight when I'm not
    working on my PC. Look at the bottom of this guide for links to what you will need, download the
    TMPGEnc demo. Go into the TMPGEnc folder and run TMPGEnc.exe, when it opens, it should star....
  6. Converting Video Files To Ipod And Psp
    (5)
    http://www.winavi.com/en/download/download.htm use link to download WinAVI 3GP/MP4/PSP/iPod
    Video Converter after installing open and it will ask for a name and code. (wont give out unless
    Mig says that I can and I will edit post later) after that take the following steps to convert any
    video file to your IPod or PSP 1st......open. 2nd.....choose what format you want (IPod, PSP,
    etc.) 3rd......search your computer for the file you wish to convert. 4th......click open
    5th......next to the advance button check "transfer mp4 file to IPod directly" 6th......a....
  7. Converting Mp3 To Wma Format
    Convertion software (9)
    Does anyone know a good program to convert MP3's into WMA's? I don't need any fancy
    audio-edit but just the converting. Thanks. Topic title edited. Topic title is VERY important and
    next time could result in a warning. ....
  8. Converting Brushes
    (2)
    hey all i have got hold of adobe photoshop 6 and i have downloaded loads of brushes only to find
    that they dont work becaue it is an older version, and then it got me thinking that maybe there is a
    way to convert these brushes so that i can use them with m,y version of adobe photoshop so basically
    is it possible to convert bruses and things to work with an older version of photoshop, all help
    would be most appreciated and i need help becaue i wana use these brushes, thanks for the help and i
    look foward to hearin the result of thsi thread!!....
  9. Converting Characters In A Variable To Individual Values In An Array
    turning variables into arrays (2)
    Say I have a variable such as $nav_item and it had to contents Home . IE: CODE
    $nav_item = 'Home'; How would I make so that $nav_item was an array and
    had the following contents? CODE $nav_item = array ('h', 'o',
    'm', 'e'); With the case changing (ie H would become h and U
    would become u ) EDIT: Okay found out that I could change the case with
    array_change_key_case ($nav_item, CASE_LOWER); ....
  10. Converting Cod2 Recorded Movies To Avi Or Mpeg?
    how do you do that (2)
    I need some help guys, i recorded a COD2 movie but the file is .dm_1 extension, and you can only
    watch it in COD, i was wondering if there is a way to convert this .dm_1 file to a avi or mpeg file.
    Hope you can help me cya....
  11. Flv1 Codec Video Converting Help
    any software to convert it to avi? (6)
    My friend just sent me a video file using the FLV1 codec, is there anyway to convert this video file
    into .AVI format besides having to use VLC media player to play it? Thanks in advance....
  12. Joystick Problem
    Converting Game Port to USB (3)
    This looks like the closest matching forum for my problem; apologies if it's not. I have an old
    MS SideWinder Pro Plus joystick that I've had forever, mainly to play X-Wing vs. TIE Fighter. I
    recently got the urge to play it again, but when I dragged the trusty SideWinder out of the storage
    box, I discovered my Abit NF7-S2 mobo doesn't have a game port. It does have onboard sound, so I
    don't have the usual sound card game port either. I bought a Belkin DB-15 joystick game
    port-to-USB adapter off the net. It's supposedly specifically for SideWinders,....
  13. Converting Dates
    converting epoch to MM-DD-YYY (1)
    So this is probably a basic and lame question, but how can I convert dates so I can then sort them?
    Right now, I have each part of a date as a separate variable: $month, $day, $year.
    I'd like to store them in the database as epoch. This way when I do a query I can sort them by
    date. I can figure out the datatype for the database later. I would also need to convert an epoch
    to Month-Day-Year later on when I want to display this. I'm sure this isn't too complicated
    but I figured asking for some suggestions anyway. Thanks!....
  14. Converting To Unix Timestamp
    Function to convert standard time format to Unix timestamp (0)
    I don't know how many of you are familiar with IRC development, but all time/date information
    are saved in unix time stamp format. Unix timestamp is number of seconds elapsed, since 1st january
    1970. Unix timestamp is also used in phpBB forums, and possibly in other bulletin boards. It is very
    convenient for manipulation, mathematical calculations, and other things... Use these functions to
    work with Unix time stamps (this is fully working code, just copy/paste it): CODE Option
    Explicit Private Type SystemTime        wYear As Integer        wMonth As Integer....
  15. converting from any extension into mp3
    (19)
    i have a problem in converting audio files that work on real player into mp3 what piece of software
    that i may use to do this if possible....
  16. Converting Video Files From One Format To Another
    help plz :) (6)
    hey i have learnt this nice way to open mov files in imageready and insert them in sigs,graphics and
    other stuff its really cool,this only works with .mov files and i really need to know if sumone here
    can do that,i need to change a wmv file into a .mov file so i cant add the movie to a sig,i have
    tried many video format conversion programs but they dont accept the mov format,the mov format is a
    quicktime video format,so anyone here know how to change a video file into a quicktime mov video
    format? thnx in advance....

    1. Looking for bin, hex, converting

Searching Video's for bin, hex, converting
advertisement



Bin To Hex Converting?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE