Jump to content
Tuts 4 You

Ascii To Hex, Hex To Ascii Code


What

Recommended Posts

Just looking for ascii to hex and hex to ascii code, for like getting information using getdlgitemtext and set. Just need it for a couple of tools i am writing out to make things quicker. I tried searching google but it was kind of annoying because most results were ascii charts or some other useless info, so I thought it would be easier to ask here. Thanks for any help.

Link to comment

At least for Hex to String there's the wsprintf function.

.data

format db "%X",0

.data?

output db 64 dup (?)

.code

mov eax,13371337h

invoke wsprintf,addr output,addr format,eax

Not sure, if that's what you were after, but it's always worth to check the masmlib.hlp

in masm's help folder. It has lots of useful functions. There's also "dw2hex" and "htodw". ;)

.data

input db "13371337",0

.code

invoke htodw,addr input

..and eax will be 13371337

Edited by Ufo-Pu55y
Link to comment

No MASM code but C...

Maybe it helps, shouldnt be too hard to convert though:

unsigned long AHex2Ulong(char * HexString){
unsigned long Return = 0;
int i = 0; while((HexString[i] >= 'A' && HexString[i] <= 'F') ||
(HexString[i] >= '0' && HexString[i] <= '9') )
{
Return *= 16;
if(HexString[i] >= 'A' && HexString[i] <= 'F')
Return += HexString[i] - 'A' + 10;
else
Return += HexString[i] - '0';
i++;
} return Return;
}

In case you're wondering, unsigned long == DWORD

Link to comment

In C....

/********************************************************************************
int Bytes_To_Hex_String (char * ptr_To_Bytes_Buffer, int int_Length_Of_Binary_Data, char * ptr_to_Output_Buffer,int int_Size_Of_Output_Buffer);
PROTOTYPE: int Bytes_To_Hex_String (char * ptr_To_Bytes_Buffer,int int_Length_Of_Binary_Data, char * ptr_to_Output_Buffer,int int_Size_Of_Output_Buffer); PURPOSE: Converts a byte sequence to a hexadecimal string. PARAMETERS:
char * ptr_To_Bytes_Buffer : A pointer to the buffer containing the bytes we want to convert
int int_Length_Of_Binary_Data : The length of the binary data we want to transform
char * ptr_to_Output_Buffer :A pointer to the buffer that will receive the hexadecimal string.
int int_Size_Of_Output_Buffer : The length of the the buffer that will receive the hexadecimal string. EXAMPLE: Bytes_To_Hex_String("HVC",3,Buffer,sizeof(Buffer));********************************************************************************/int Bytes_To_Hex_String (char * ptr_To_Bytes_Buffer,
int int_Length_Of_Binary_Data,
char * ptr_to_Output_Buffer,
int int_Size_Of_Output_Buffer){
static int Counter = 0;
static signed char Current_Nibble; /* Check to see if the provided output buffer is big enough */ if (((2*int_Length_Of_Binary_Data)+1) >= int_Size_Of_Output_Buffer)
{
return ((int) FALSE);
}
for (Counter = 0; Counter <= (int_Length_Of_Binary_Data-1); Counter ++)
{ /* Get the high nibble of the current byte */ Current_Nibble = (((ptr_To_Bytes_Buffer[Counter])>>4)&(0x0F)); /* Convert binary to ASCII */ if (Current_Nibble <= 9)
{
Current_Nibble += 0x30;
}
else
{
Current_Nibble += 0x37;
} /* Save the high nibble */ ptr_to_Output_Buffer [(2*Counter)]= Current_Nibble; /* Get the low nibble of the current byte */ Current_Nibble = (signed char)((ptr_To_Bytes_Buffer[Counter])&(0x0F)); /* Convert binary to ASCII */ if (Current_Nibble <= 9)
{
Current_Nibble += 0x30;
}
else
{
Current_Nibble += 0x37;
} /* Save the low nibble */ ptr_to_Output_Buffer [(2 * Counter) + 1] = Current_Nibble;
} return ((int) TRUE);
}
/********************************************************************************  int Hex_String_To_Bytes (char * ptr_To_Hex_String_Buffer,int int_Length_Of_Hex_String, char * ptr_to_Output_Buffer, int int_Size_Of_Output_Buffer);	PROTOTYPE: int Hex_String_To_Bytes (char * ptr_To_Hex_String_Buffer,int int_Length_Of_Hex_String, char * ptr_to_Output_Buffer, int int_Size_Of_Output_Buffer);
PURPOSE: Converts a hexadecimal string to a byte sequence PARAMETERS:
char * ptr_To_Hex_String_Buffer : A pointer to the buffer containing the hexadecimal string we want to convert
int int_Length_Of_Hex_String : The length of the hexadecimal string we want to transform
char * ptr_to_Output_Buffer :A pointer to the buffer that will receive the binary data.
int int_Size_Of_Output_Buffer : The length of the buffer that will receive the binary data. EXAMPLE: Hex_String_To_Bytes(HexBuffer,lstrlen(HexBuffer),BinBuffer, sizeof(BinBuffer));********************************************************************************/int Hex_String_To_Bytes (char * ptr_To_Hex_String_Buffer,
int int_Length_Of_Hex_String,
char * ptr_to_Output_Buffer,
int int_Size_Of_Output_Buffer){
static int Counter = 0;
static signed char High_Nibble;
static signed char Low_Nibble;
static signed char Current_Byte; /* Check to see if the provided output buffer is big enough */ if (((int_Length_Of_Hex_String/2)) >= int_Size_Of_Output_Buffer)
{
return ((int) FALSE);
}
for (Counter = 0; Counter <= (int_Length_Of_Hex_String/2); Counter ++)
{ /* Get the High Nibble */ High_Nibble = ptr_To_Hex_String_Buffer[(2*Counter)]; /* Check for Erroneus Input */ if (High_Nibble < '0')
{
return ((int) FALSE);
}
else if (High_Nibble > '9')
{
if (High_Nibble < 'A')
{
return ((int) FALSE);
}
if(High_Nibble > 'F')
{
if (High_Nibble < 'a')
{
return ((int) FALSE);
}
else if (High_Nibble > 'f')
{
return ((int) FALSE);
}
}
} /* Convert ASCII to binary */ if (High_Nibble <= '9')
{
High_Nibble -= 0x30;
}
else if (High_Nibble <= 'Z')
{
High_Nibble -= 0x37;
}
else
{
High_Nibble -= 0x57;
} // Rotate 1 byte left the high nibble High_Nibble = High_Nibble << 4;
/* Get the low nibble */ Low_Nibble = ptr_To_Hex_String_Buffer[(2*Counter)+1]; /* Check for erroneus input */ if (Low_Nibble < '0')
{
return ((int) FALSE);
}
else if (Low_Nibble > '9')
{
if (Low_Nibble < 'A')
{
return ((int) FALSE);
}
if(Low_Nibble > 'F')
{
if (Low_Nibble < 'a')
{
return ((int) FALSE);
}
else if (Low_Nibble > 'f')
{
return ((int) FALSE);
}
}
} /* Convert ASCII to binary */ if (Low_Nibble <= '9')
{
Low_Nibble -= 0x30;
}
else if (Low_Nibble <= 'Z')
{
Low_Nibble -= 0x37;
}
else
{
Low_Nibble -= 0x57;
} /* Convert the two nibbles to a byte */ Current_Byte = High_Nibble + Low_Nibble; /* Save the byte */ ptr_to_Output_Buffer[Counter] = Current_Byte;
}
return ((int) TRUE);
}

cf.zip

Edited by HVC
Link to comment

... and MASM:

; HexStringToBytes PROC USES EDI ESI EDX ECX pszString :LPVOID, lpOutputBuffer :LPVOID, dwSizeOfOutputBuffer
.486pOPTION CASEMAP:NONE
.CODE
HexStringToBytes PROC USES EDI ESI EDX ECX pszString :LPVOID, lpOutputBuffer :LPVOID, dwSizeOfOutputBuffer LOCAL dwInputLength :DWORD
LOCAL dwNumberOfBytes :DWORD
Invoke lstrlen, pszString
MOV dwInputLength,EAX
MOV ECX,2
CDQ
IDIV ECX
.IF ((EDX!=0) || (EAX >= dwSizeOfOutputBuffer))
MOV EAX, FALSE
RET
.ENDIF
MOV dwNumberOfBytes,EAX
INVOKE RtlZeroMemory, lpOutputBuffer, dwSizeOfOutputBuffer
XOR EDI,EDI
MOV ESI, pszString
MOV ECX,lpOutputBuffer
.WHILE (EDI < dwNumberOfBytes)
MOV AL, BYTE PTR [ESI]
MOV AH, BYTE PTR [ESI+1]
.IF (AL<='9')
SUB AL, '0'
.ELSE
SUB AL, 37h
.ENDIF
.IF (AH<='9')
SUB AH, '0'
.ELSE
SUB AH, 37h
.ENDIF
XOR EDX,EDX
MOV DL,AL
SHL EDX, 4
OR DL,AH
MOV BYTE PTR [ECX + EDI], DL
ADD ESI,2
INC EDI
.ENDW
MOV EAX, TRUE
RET
HexStringToBytes ENDP

For the inverse function, i use wsprintf...

Edited by HVC
Link to comment

As another option, MASM actually has some built in procs to do this for you (in masm32rt.inc IIRC).

If you search for "ascnum2hex" on the MASM board you will find examples.

Link to comment
  • 2 weeks later...

I am messing around with SHA-1 can I use the wsprintf to convert the output so it can be displayed in a edit box?

ASM BTW

Edited by dustyh1981
Link to comment

What do you mean 'convert'?

Does the code not include something like

sFormat_SHA1 db '%.8x%.8x%.8x%.8x%.8x', 0

invoke wsprintf, addr sznumber1, addr sFormat_SHA1, h0_sha1, h1_sha1, h2_sha1, h3_sha1, h4_sha1

Link to comment
What do you mean 'convert'?

Does the code not include something like

sFormat_SHA1 db '%.8x%.8x%.8x%.8x%.8x', 0

invoke wsprintf, addr sznumber1, addr sFormat_SHA1, h0_sha1, h1_sha1, h2_sha1, h3_sha1, h4_sha1

No it don't but I just looked at the SND REverser tool in olly and was about to try something like that

by convert I mean I want to display what is in the raw hex dump and not the ascii

ex: 01 B3 07 AC BA 4F 54 F5 5A AF C3 3B B0 6B BB F6 CA 80 3E 9A | Ascii

Edited by dustyh1981
Link to comment

@Loki:

Man you are a life saver.... the sha1.inc I have zeroes out the buffers where the temp hash is, I cut that code out

and between your post and looking at the SND Reversers tool in olly I figured out where I was going wrong and got it working.

So either way I owe thanks to you once again.... so

Thanks :worthy:

Link to comment

Its weird that your source zero'd it out.... glad you got there!

Nice work debugging Reverser Tool too - good to see someone trying to solve problems for themselves :thumbsup:

Link to comment

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
×
×
  • Create New...