Bin2Hex, Hex2Dec, Dec2Bin and more..

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 622
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Bin2Hex, Hex2Dec, Dec2Bin and more..

Post by minimy »

Hi friends, I did this short code for educational purporse (is not fast :) )
May be interesting to show how work bits and diferent number systems.

But have a preety license.. lol :mrgreen:

Is joke you can burn the license, no problem :)

Code: Select all

; For educational purporse
; Copyright © 2020 Minimy
; Permission is hereby granted, free of charge, To any person obtaining a copy of this
; software And associated documentation files (the “Software”), To deal in the Software 
; without restriction, including without limitation the rights To use, copy, modify, 
; merge, publish, distribute, sublicense, And/Or sell copies of the Software, And To 
; permit persons To whom the Software is furnished To do so, subject To the following conditions:

; The above copyright notice And this permission notice shall be included in all copies Or substantial portions of the Software.

; THE SOFTWARE IS PROVIDED “As IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS Or IMPLIED, 
; INCLUDING BUT Not LIMITED To THE WARRANTIES OF MERCHANTABILITY, FITNESS 
; For A PARTICULAR PURPOSE And NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
; Or COPYRIGHT HOLDERS BE LIABLE For ANY CLAIM, DAMAGES Or OTHER LIABILITY, 
; WHETHER IN AN ACTION OF CONTRACT, TORT Or OTHERWISE, ARISING FROM, OUT OF Or 
; IN CONNECTION With THE SOFTWARE Or THE USE Or OTHER DEALINGS IN THE SOFTWARE.

; Con fines educativos
; Copyright © 2020 Minimy
; Por la presente se otorga permiso, sin cargo, a cualquier persona que obtenga una copia de este
; software y archivos de documentación asociados (el "Software"), para tratar el Software
; sin restricción, incluidos, entre otros, los derechos de uso, copia, modificación,
; fusionar, publicar, distribuir, sublicenciar y / o vender copias del Software, y para
; Permitir que las personas a las que se les proporcione el Software lo hagan, sujeto a las siguientes condiciones:

; El aviso de derechos de autor anterior y este aviso de permiso se incluirán en todas las copias o partes sustanciales del software.

; EL SOFTWARE SE PROPORCIONA "TAL CUAL", SIN GARANTÍA DE NINGÚN TIPO, EXPRESA O IMPLÍCITA,
; INCLUYENDO, PERO NO LIMITADO A, LAS GARANTÍAS DE COMERCIABILIDAD, APTITUD
; Para UN PROPÓSITO PARTICULAR Y NO INFRACCIÓN. EN NINGÚN CASO LOS AUTORES
; O LOS TITULARES DE LOS DERECHOS DE AUTOR SERÁN RESPONSABLES DE CUALQUIER RECLAMO, DAÑOS U OTRA RESPONSABILIDAD,
; YA SEA EN UNA ACCIÓN DE CONTRATO, AGRAVIO O DE OTRO MODO, QUE SURJA DE, FUERA DE O
; EN RELACIÓN CON EL SOFTWARE O EL USO U OTROS NEGOCIOS DEL SOFTWARE.

; OMG the license is bigger than the code..  :)_.oO

; Binario a Decimal
; Binario a Hexadecimal
; Decimal a Binario
; Decimal a Hexadecimal
; Hexadecimal a Decimal
; Hexadecimal a Hexadecimal

Procedure Bin2DecEx(binario.s)
  Protected.i decimal= Val("%"+binario)
  ProcedureReturn decimal
EndProcedure
Procedure.s Bin2HexEx(binario.s, bytes.b=8)
  Protected.s hexa= RSet(Hex(Val("%"+binario),#PB_Quad),bytes,"0")
  ProcedureReturn hexa
EndProcedure

Procedure.s Dec2HexEx(decimal.q, bytes.b=8)
  Protected.s hexa= RSet(Hex(decimal,#PB_Quad),bytes,"0")
  ProcedureReturn hexa
EndProcedure
Procedure.s Dec2BinEx(decimal.q, bytes.b=8)
  Protected binario.s= RSet(Bin(decimal,#PB_Quad),bytes,"0")
  ProcedureReturn binario
EndProcedure

Procedure Hex2DecEx(hexa.s)
  Protected.i decimal= Val("$"+hexa)
  ProcedureReturn decimal
EndProcedure
Procedure.s Hex2BinEx(hexa.s, bytes.b=8)
  Protected binario.s= RSet(Bin(Val("$"+hexa),#PB_Quad),bytes,"0")
  ProcedureReturn binario
EndProcedure

Procedure getBitDecEx(decimal.q, bit.b=0)
  Protected valor.i= -1
  Protected bytes.b= 8
  If  decimal>256        : bytes= 16
  ElseIf  decimal>65535      : bytes= 24
  ElseIf  decimal>16777215   : bytes= 32
  ElseIf  decimal>4294967295 : bytes= 64
  EndIf
;   Debug bytes
  binario.s= RSet(Bin(decimal,#PB_Quad),bytes,"0")
  If bit<bytes
    valor= Val(Mid(binario,Len(binario)-bit,1) )
  EndIf
  ProcedureReturn valor
EndProcedure
Procedure getBitHexEx(hexa.s, bit.b=0)
  Protected valor.i= -1
  Protected.i decimal= Val("$"+hexa)
  Protected bytes.b= 8
  If  decimal>256        : bytes= 16
  ElseIf  decimal>65535      : bytes= 24
  ElseIf  decimal>16777215   : bytes= 32
  ElseIf  decimal>4294967295 : bytes= 64
  EndIf
;   Debug bytes
  binario.s= RSet(Bin(decimal,#PB_Quad),bytes,"0")
  If bit<bytes
    valor= Val(Mid(binario,Len(binario)-bit,1) )
  EndIf
  ProcedureReturn valor
EndProcedure

Debug Bin2DecEx("11111111")
Debug Bin2HexEx("11111111")
Debug Dec2BinEx(255)
Debug Dec2HexEx(10)
Debug Hex2DecEx("000000FF")
Debug Hex2BinEx("000000FF")

Debug Bin2DecEx("11111111")
Debug Bin2DecEx("1111111111111111")
Debug Bin2DecEx("111111111111111111111111")
Debug Bin2DecEx("11111111111111111111111111111111")

Debug getBitDecEx(2,0)
Debug getBitHexEx("FFFF",1)
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Bin2Hex, Hex2Dec, Dec2Bin and more..

Post by Kwai chang caine »

Can be usefull
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Bin2Hex, Hex2Dec, Dec2Bin and more..

Post by davido »

@minimy,
Thank you for sharing. :D
DE AA EB
Post Reply