Buenas,
alguien tiene construidas las siguientes funciones:
Hex2Bin, Bin2Hex, Dec2Hex, Hex2Dec, Dec2Bin, Bin2Dec
Y las quiere compartir ?
He encontrado lo siguiente http://www.c-sharpcorner.com/Blogs/3878/simple-functions-to-convert-hex2bin-bin2hex-dec2hex-hex2d.aspx
Muchas gracias.
Saludos
Hex2Bin, Bin2Hex, Dec2Hex, Hex2Dec, Dec2Bin, Bin2Dec
- cmsoft
- Posts: 1297
- Joined: Wed Nov 16, 2005 9:14 pm
- Location: Mercedes - Bs As. Argentina
- Been thanked: 2 times
Re: Hex2Bin, Bin2Hex, Dec2Hex, Hex2Dec, Dec2Bin, Bin2Dec
Te puedo facilitar estas que construi alguna vez. Espero te sirvan:
Code: Select all | Expand
FUNCTION Dec2Bin(n)
LOCAL arr:="",i
FOR i = 1 to 8
arr := arr + STR(n % 2,1)
n = INT(n/2)
NEXT
RETURN arr
FUNCTION Bin2Dec(num)
LOCAL res,a:=ARRAY(8),i
FOR i = 1 to 8
a[9-i] := VAL(SUBSTR(num,i,1))
NEXT i
res := a[1]*128+a[2]*64+a[3]*32+a[4]*16+a[5]*8+a[6]*4+a[7]*2+a[8]*1
RETURN res
Re: Hex2Bin, Bin2Hex, Dec2Hex, Hex2Dec, Dec2Bin, Bin2Dec
Baxajaun,
Un tiempo atras estuve haciendo pruebas con este tipo de conversiones.
Aqui esta el codigo:
Saludos,
George
Un tiempo atras estuve haciendo pruebas con este tipo de conversiones.
Aqui esta el codigo:
Code: Select all | Expand
//--------------------------------------------------------------------------
FUNCTION HexToBinary(cHex)
LOCAL nLenBin, nX, nPos, cBinNumber, cCharHex, cBinBase
cBinNumber := ""
cHex := alltrim(cHex)
FOR nX := len(cHex) TO 1 STEP -1
cCharHex := substr(cHex, -nX, 1)
nPos := ascan(aHexBinary, { |a| a[1] == substr(cHex, -nX, 1)} )
cBinBase := aHexBinary[nPos,2]
cBinNumber := cBinNumber + cBinBase
NEXT
RETURN (cBinNumber)
//--------------------------------------------------------------------------
FUNCTION BinaryToHex(cBin)
LOCAL nLenBin, nX, nPos, cBinNumber, cHex := ""
cBin := alltrim(cBin)
cBin := atrepl(" ", cBin, "")
nLenBin := len(cBin)
aBinaryNumber := {}
// Validate the binary number
DO CASE
CASE nLenBin <= 4
cBin := "000"+cBin
cBin := right(cBin,4)
CASE nLenBin <= 8
cBin := "000"+cBin
cBin := right(cBin,8)
CASE nLenBin <= 12
cBin := "000"+cBin
cBin := right(cBin,12)
CASE nLenBin <= 16
cBin := "000"+cBin
cBin := right(cBin,16)
CASE nLenBin <= 20
cBin := "000"+cBin
cBin := right(cBin,20)
CASE nLenBin <= 24
cBin := "000"+cBin
cBin := right(cBin,24)
CASE nLenBin <= 28
cBin := "000"+cBin
cBin := right(cBin,28)
CASE nLenBin <= 32
cBin := "000"+cBin
cBin := right(cBin,32)
CASE nLenBin <= 36
cBin := "000"+cBin
cBin := right(cBin,36)
CASE nLenBin <= 40
cBin := "000"+cBin
cBin := right(cBin,40)
CASE nLenBin <= 44
cBin := "000"+cBin
cBin := right(cBin,44)
CASE nLenBin <= 48
cBin := "000"+cBin
cBin := right(cBin,48)
CASE nLenBin <= 52
cBin := "000"+cBin
cBin := right(cBin,52)
CASE nLenBin <= 56
cBin := "000"+cBin
cBin := right(cBin,56)
CASE nLenBin <= 60
cBin := "000"+cBin
cBin := right(cBin,60)
CASE nLenBin <= 64
cBin := "000"+cBin
cBin := right(cBin,64)
ENDCASE
// Create Array of Binary
nLenBin := len(cBin)
FOR nX := nLenBin to 1 step -4
cBinNumber := substr(cBin, -nX, 4)
aadd(aBinaryNumber, cBinNumber)
NEXT
// Convert Binary to Hexadecimal
nLenBin := len(aBinaryNumber)
FOR nX := 1 TO nLenBin
cBinNumber := aBinaryNumber[nX]
// Search Hex for this Binary
nPos := aScan(aBinary, cBinNumber,,,.t. )
IIF( nPos > 0, cHex := cHex + aHex[nPos], msginfo(cBinNumber+ " not found") )
NEXT
RETURN ("0X"+cHex)
//--------------------------------------------------------------------------
FUNCTION DecToHex(nDec)
// Use NumToHex() from harbour
RETURN (.T.)
//--------------------------------------------------------------------------
FUNCTION HexToDecimal(cHex)
LOCAL nDec := 0, nX, nLen, cHexVal, nDecVal
cHex := alltrim(cHex)
cHex := atrepl("0X", cHex, "")
nLen := len(cHex)
FOR nX := 1 TO nLen
cHexVal := substr(cHex, -nX, 1)
DO CASE
CASE cHexVal = "A"
nDecVal := 10
CASE cHexVal = "B"
nDecVal := 11
CASE cHexVal = "C"
nDecVal := 12
CASE cHexVal = "D"
nDecVal := 13
CASE cHexVal = "E"
nDecVal := 14
CASE cHexVal = "F"
nDecVal := 15
OTHERWISE
nDecVal := val(cHexVal)
ENDCASE
nDec := nDec + nDecVal * 16 ** (nX-1)
nDec := int(nDec)
NEXT
RETURN (nDec)
//--------------------------------------------------------------------------
FUNCTION Hex2Dec(cHex)
LOCAL nDec := 1, JJ, nLen := len(cHex)
FOR jj := 1 TO nLen
nDec := nDec*16 + at(substr(cHex, jj, 1), "123456789ABCDEF")
NEXT
RETURN (nDec)
Saludos,
George
Re: Hex2Bin, Bin2Hex, Dec2Hex, Hex2Dec, Dec2Bin, Bin2Dec
Buenos días,
he encontrado esto:
Lo he encontrado en http://www.hackerzvoice.net/ceh/CEHv6%2 ... ibrary.prg
Saludos
he encontrado esto:
Code: Select all | Expand
* ±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
FUNCTION BIN2HEX( cStr )
* ±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
LOCAL cRet := "", nPos
FOR nPos := 1 TO LEN(cStr)
cRet += dfDec2Hex( ASC(substr(cStr,nPos,1)) )
IF nPos < LEN(cStr)
cRet += ""
ENDIF
NEXT
RETURN cRet
#pragma BEGINDUMP
#include "extend.api"
#include <Gt.api>
#include <fm.api>
#include <filesys.api>
char * _dfDec2Hex( unsigned int iReg ){
static char fpHexBuf[5];
static char * fpChar ="0123456789ABCDEF";
//fpHexBuf[0] = fpChar[iReg/(16*16*16) & 0x0f ];
//fpHexBuf[1] = fpChar[iReg/(16*16) & 0x0f ];
fpHexBuf[0] = fpChar[iReg/(16) & 0x0f ];
fpHexBuf[1] = fpChar[iReg & 0x0f ];
fpHexBuf[2] = '\0';
return fpHexBuf;
}
HB_FUNC( DFDEC2HEX ){
_retc( _dfDec2Hex( _parni(1) ) );
}
#pragma ENDDUMP
Lo he encontrado en http://www.hackerzvoice.net/ceh/CEHv6%2 ... ibrary.prg
Saludos
Re: Hex2Bin, Bin2Hex, Dec2Hex, Hex2Dec, Dec2Bin, Bin2Dec
Buenas,
después de casi volverme loco, intentar reinventar la rueda me encuento con lo siguiente:
en la siguiente url https://github.com/zgamero/sandbox/wiki/2.2-String-manipulation#224-conversion-and-manipulation
Saludos
después de casi volverme loco, intentar reinventar la rueda me encuento con lo siguiente:
Code: Select all | Expand
2.2.4 Conversion and manipulation
HB_StrToHex( <cString> [ , <cSeparator> ] ) -> <cHexValues>
Converts a string (or buffer) into a string of it's corresponding hexadecimal values. Each byte of <cString> will be converted to an string of the two-digits hexadecimal value. You can optionally specify a <cSeparator> character to separate each hexadecimal value.
Examples:
? HB_StrToHex( "harbour" ) // "686172626F7572"
? HB_StrToHex( "harbour" + Chr( 0 ), ":" ) // "68:61:72:62:6F:75:72:00"
? HB_StrToHex( e"hello\nworld\0", ":" ) // "68:65:6C:6C:6F:0A:77:6F:72:6C:64:00"
HB_HexToStr( <cHexValues> ) -> <cBytes>
The inverse of HB_StrToHex() that convert a string of hexadecimal values into it's corresponding bytes stream.
? HB_HexToStr( "686172626F7572" ) // "harbour"
en la siguiente url https://github.com/zgamero/sandbox/wiki/2.2-String-manipulation#224-conversion-and-manipulation
Saludos