Topic: hex string to dec (1 of 13), Read 84 times
Conf: VEDIT Macro Language Support
From: Barry Levine
Date: Friday, February 09, 2001 12:02 PM

Is there an existing macro to convert hex string to decimal number?

 


Topic: Re: hex string to dec (2 of 13), Read 78 times
Conf: VEDIT Macro Language Support
From: Ted Green
Date: Friday, February 09, 2001 12:22 PM

At 12:03 PM 2/9/2001 -0500, you wrote:
>Is there an existing macro to convert hex string to decimal number?

The supplied hex-bin.vdm macro does this. Here is the code from it:

Begin_Of_File()
while (!At_EOF) {
if ( Cur_Char >= '0' && Cur_Char <= '9' ) {
#0 = Cur_Char - '0'
} else { if ( Cur_Char >= 'a' && Cur_Char <= 'z' ) {
#0 = 10 + Cur_Char - 'a'
} else { if ( Cur_Char >= 'A' && Cur_Char <= 'Z' ) {
#0 = 10 + Cur_Char - 'A'
} else {
)
Message("*****Bad input:\n")
Type(0)
Type_Newline()
Type(1)
Get_Key("Press any key to quit...")
Return
}}}
Char(1)
if ( Cur_Char >= '0' && Cur_Char <= '9' ) {
#1 = Cur_Char - '0'
} else { if ( Cur_Char >= 'a' && Cur_Char <= 'z' ) {
#1 = 10 + Cur_Char - 'a'
} else { if ( Cur_Char >= 'A' && Cur_Char <= 'Z' ) {
#1 = 10 + Cur_Char - 'A'
} else {
)
Message("*****Bad input:\n")
Type(0)
Type_Newline()
Type(1)
Get_Key("Press any key to quit...")
Return
}}}
#0 = (#0 << 4) + #1
Del_Char(-1)
Ins_Char(#0,OVERWRITE)
if ( Match("|X")==0 ) { Del_Char(Chars_Matched) }
}

Ted.

 


Topic: Re: hex string to dec (3 of 13), Read 80 times
Conf: VEDIT Macro Language Support
From: Barry Levine
Date: Friday, February 09, 2001 02:31 PM

Ted,

thanks. Is this the same macro that performs similar to the 'calculator' command line function that converts hex string to dec number string?


On 2/9/2001 12:22:15 PM, Ted Green wrote:
>At 12:03 PM 2/9/2001 -0500, you
>wrote:
>Is there an existing macro to
>convert hex string to decimal
>number?

The supplied hex-bin.vdm macro
>does this. Here is the code from
>it:

Begin_Of_File()
while (!At_EOF) {

>if ( Cur_Char >= '0' && Cur_Char <= '9'

Ted

Thanks, but does this macro convert to dec string like the "calculator" command line function does?

>) {
#0 = Cur_Char - '0'
}
>else { if ( Cur_Char >= 'a' && Cur_Char
><= 'z' ) {
#0 = 10 + Cur_Char -
>'a'
} else { if ( Cur_Char >= 'A' &&
>Cur_Char <= 'Z' ) {
#0 = 10 +
>Cur_Char - 'A'
} else {
)

>Message("*****Bad input:\n")

>Type(0)
Type_Newline()

>Type(1)
Get_Key("Press any key
>to quit...")
Return
}}}

>Char(1)
if ( Cur_Char >= '0' &&
>Cur_Char <= '9' ) {
#1 =
>Cur_Char - '0'
} else { if (
>Cur_Char >= 'a' && Cur_Char <= 'z' ) {

>#1 = 10 + Cur_Char - 'a'
} else { if
>( Cur_Char >= 'A' && Cur_Char <= 'Z' )
>{
#1 = 10 + Cur_Char - 'A'
}
>else {
)

>Message("*****Bad input:\n")

>Type(0)
Type_Newline()

>Type(1)
Get_Key("Press any key
>to quit...")
Return
}}}

>#0 = (#0 << 4) + #1
Del_Char(-1)

>Ins_Char(#0,OVERWRITE)
if (
>Match("|X")==0 ) {
>Del_Char(Chars_Matched) }
}

Ted.

 


Topic: Re: hex string to dec (4 of 13), Read 86 times
Conf: VEDIT Macro Language Support
From: Ted Green
Date: Friday, February 09, 2001 06:33 PM

At 02:31 PM 2/9/2001 -0500, you wrote:
>thanks. Is this the same macro that performs similar to the 'calculator' command line function that converts hex string to dec number string?

No, it is not. The macro only converts two hex digits into one byte.
This macro may do what you want:

Get_Input(10,"Enter hex number: ")
Reg_Set(10,"0x",INSERT)
#1=Num_Eval_Reg(10)
Num_Type(#1)

It lets you enter a hex number such as "DA6B" and it then
types its decimal value "55915".

Ted.

 


Topic: Re: hex string to dec (5 of 13), Read 71 times
Conf: VEDIT Macro Language Support
From: bone fonny
Date: Tuesday, December 10, 2002 07:35 PM

Any way to change the keyboard input so it searches a file
at a particular offset and reads the number,output to a text file, then go to the next value and so on (1000 times!).

I'm trying to do this as we speak.My file reads 0000194f and
i want it to write 6479 in a .txt file. It seems quite simple to me but haven't had any success yet.

Any help you guys could give would be appreciated.

 


Topic: Re: hex string to dec (6 of 13), Read 69 times
Conf: VEDIT Macro Language Support
From: Ted Green
Date: Wednesday, December 11, 2002 04:20 PM

At 07:36 PM 12/10/2002, you wrote:
>Any way to change the keyboard input so it searches a file
>at a particular offset and reads the number,output to a text file, then go to the next value and so on (1000 times!).
>
>I'm trying to do this as we speak.My file reads 0000194f and
>i want it to write 6479 in a .txt file. It seems quite simple to me but haven't had any success yet.

Your macro will have to take the following steps:

1. Copy the number to a text register and then to a temp buffer.
2. Precede it with "0X" to put it into VEDIT's "hex" format.
3. Read the number with the Num_Eval() command into a numeric register.
4. Switch to the output text file.
5. Convert the number into ASCII and convert it with the Num_Ins() command.

Ted.

 


Topic: Re: hex string to dec (7 of 13), Read 64 times
Conf: VEDIT Macro Language Support
From: bone fonny
Date: Sunday, December 15, 2002 12:36 PM

That's about what i did. I'm really new at all this.
I started encoutering FFFE 0000,FFFD 0000,... and 0002 0000,
0003 0000,... values. My code is really not up to that.
I based it on the code posted here and apparently it can't
deal with negative numbers. I made this line to compensate
"if(#5!=0){#6-=65536,Reg_Set(6,"-",INSERT)}", needless to say it doesn't 'convert' FFFD, FFFE,0001,0002, correctly.
Any way to solve this ?

Here's my code as of today, any advise would be appreciated . :)

----------

//Xtractor

File_Open("filename")
BOF
Ins_Text("Object1") //.raw format
Ins_Newline(1) //.raw format
Do{Call("loop")}while(!At_EOF)

:loop:
Repeat(5){Call("xtractor")} //(n)=# of columns in file
Goto_Pos(Cur_Pos+2)
Del_Char(-2) //Removes the < LF >
//Call("loop") //'fix' when loop stops early
return()

:xtractor:
#1=Cur_pos
#2=#1+4
#3=#1+8
#4=#1+1
BB(#1)
BE(#2)
Reg_Copy_Block(10,BB,BE) //usually the FFFF xxxx value
Reg_Set(10,"0x",INSERT)
#5=Num_Eval_Reg(10)
BB(#2)
BE(#3)
Reg_Copy_Block(11,BB,BE) //32bit integer
Reg_Set(11,"0x",INSERT)
#6=Num_Eval_Reg(11)
if(#5!=0){#6-=65536,Reg_Set(6,"-",INSERT)}//if FFFF...
if(#6!=1){Num_Ins(#6,LEFT+NOCR),Ins_Text(".000000")}else{del_char(-1),ins_newline(1)}//removes value block seperator
del_char(8)
GoTo_Pos(Cur_Pos+1)
Return()

 


Topic: Re: hex string to dec (8 of 13), Read 64 times
Conf: VEDIT Macro Language Support
From: Christian Ziemski
Date: Sunday, December 15, 2002 05:30 PM

It looks like VEDIT's online calculator and the Num_Eval() command have a little problem with negative hexadecimal numbers.

Try it in the command line:

$ -1
gives correctly
0XFFFF:FFFF

But a
. 0XFFFFFFFF
gives
0
and not -1 as expected.


All negative hex numbers evaluate to 0 (with Num_Eval too)...



Besides of that I don't fully understand your problem.
Perhaps you can post a short example of your source file and the appropriate target format?
Then we should be able to help.

Christian

 


Topic: Re: hex string to dec (9 of 13), Read 71 times
Conf: VEDIT Macro Language Support
From: bone fonny
Date: Sunday, December 15, 2002 10:23 PM

My input file looks like this:

00000001 00008978 FFFDFEC4 000026F8 00000001
0000E312 00013524 0000A084 00000001 00008E07
FFFFEC10 00011D70 00000001 0000D472 00017CAD
0000173A 00000001 00006DF1 FFFE5AA6 00006F69
00000001 0000AE4B 0001F8F2 00004337 00000001

(5 columns before lf/cr)

The 00000001 at the start of each are the 'separators'.
They change to 00000002 and 00000003 after a while, but i
don't want them. The three values in between are the x,y,z
coordinates of a vertice.

The output SHOULD look like this:

Object1
35192.000000 -316.000000 9976.000000 58130.000000
-51932.000000 41092.000000 36359.000000 -5104.000000
-58000.000000
54386.000000 -33619.000000 5946.000000 28145.000000
-42330.000000 28521.000000 44619.000000 -1806.000000
17207.000000

(9 columns before lf/cr) posting messed up the columns...

That is a .raw format for a mesh file. As you can see the second value, -316 is wrong, it should read -131388. My code is to blame for that (look at the previous post) as
i made a 'patch' to deal with negative numbers. Most data up to now always used FFFF so it worked with those. This file has many different values and has me scratching my head
as to how can i get the 32bit long integer out of it without having to split it in 2 16bit shorts like i have up to now. I feel i'm repeating code and am missing a much simpler answer.

I'm actually learning all this at the same time. It is more of a hobby and my work/life doesn't depend on it so no pressure. I sure appreciate your help up to now though !
Thanks a lot. :)

 


Topic: Re: hex string to dec (10 of 13), Read 76 times
Conf: VEDIT Macro Language Support
From: Christian Ziemski
Date: Monday, December 16, 2002 02:36 PM

Hello bone fonny!

Theoretically it's easy to manage the task with some loops.
For a sample macro see below. (Sorry for the strange form. Webboard isn't easy to handle with code ...)

Only the problem with negative numbers is a bit tricky.
I marked my solution with "// -- patch" in the macro.
But that patch should be deleted when VEDIT could work with negative hex numbers again!
(Ted, are you listening here?)


Christian




// File_Open("input.txt")

#90 = Buf_Num
#91 = Buf_Free()

Buf_Switch(#91)
// File_Save_As("output.txt", OK)

Buf_Switch(#90)

BoF
while( ! At_EoF){

Buf_Switch(#91) // Insert object header into the output buffer
Ins_Text("Object1")
Ins_Newline(1)


Buf_Switch(#90)

for(#95=1 ; #95<=3 ; #95++){ // loop 3 times for three numbers => 9 items

Search("|X", ADVANCE+NOERR+ERRBREAK) // skip the first (unneeded) number

for(#99=1 ; #99<=3 ; #99++){ // read three values


Reg_Empty(98)

#80=0 // -- Patch to make negative hex values possible
#81=0 // --
if(Match("|{F,E,D,C,B,A,9,8}")==0){ // -- check the first hex digit
if(Match_Item == 8){ // -- "8" must be done separately due to number size
#80=2147483647 // --
#81=1 // --
}else{ // --
Reg_Set(80, "0x") // --
Num_Str(Match_Item, 80, LEFT+NOCR+APPEND) // -- build the value to subtract later
Reg_Set(80, "0000000", APPEND) // --
#80=Num_Eval_Reg(80) // --
#81=0 // --
} // --
Reg_Set(98, "0") // -- set the first digit to 0 in the following evaluation
Char(1) // --
} // --


#92 = Cur_Pos // remember the beginning of the current number
Search("|X", NOERR) // search end of current number
if(Error_Match){ // no extended whitespace found => EoF reached
return
}

Reg_Set(98, "0x", INSERT)
Reg_Copy_Block(98, #92, Cur_Pos, APPEND) // copy the current number in the hex prepared text register
#98 = Num_Eval_Reg(98) // evaluate the value


#98 = #98 - #80 - #81 // -- patch continued: subtract to have correct negative value


Buf_Switch(#91) // switch to output buffer
Ins_Text(" ")
Num_Ins(#98, LEFT+NOCR)
Ins_Text(".000000")

Buf_Switch(#90) // switch back to input buffer
Search("|X", ADVANCE+NOERR+ERRBREAK) // proceed to beginning of next number
}
}
Buf_Switch(#91) // object finished => insert CR/LF into output buffer
Ins_Newline(1)

Buf_Switch(#90) // switch back to input buffer
}

BoF
Buf_Switch(#91)
BoF

 


Topic: Re: hex string to dec (11 of 13), Read 67 times
Conf: VEDIT Macro Language Support
From: Ted Green
Date: Monday, December 16, 2002 03:07 PM

At 05:30 PM 12/15/2002, you wrote:
>It looks like VEDIT's online calculator and the Num_Eval() command have a little problem with negative hexadecimal numbers.
>But a
>. 0XFFFFFFFF
>gives 0 and not -1 as expected.

0XFFFFFFFF is 4Gigs-1 which is too big for the 32-bit VEDIT. It is NOT treated as a signed 32-bit number. That could be an option in the future, but not all (in fact very few) binary numbers are signed.

Consider 0xFFFF. If it is a 16 bit number, then it is 65535 unsigned or -1 signed. However, if it is a 32 bit number, then it is 65535 signed or unsigned.
To convert such numbers, the Num_Eval command would need to know the size of the number (8,16,32,64 bits) and whether or not it is signed.

I will add this fancier Num_Eval to the suggestion list, but it seems to be a low priority right now.

Thank you Christian for your macro to solve this problem.

Ted.

 


Topic: Re: hex string to dec (12 of 13), Read 75 times
Conf: VEDIT Macro Language Support
From: Christian Ziemski
Date: Monday, December 16, 2002 03:46 PM

I think I now have understood the problem with those signed numbers.

It was the working way vice versa that let me think it should work from hex to dec too.

COMMAND: $ -256
0XFFFF:FF00

But
COMMAND: . 0XFFFFFF00
0



Christian

 


Topic: Re: hex string to dec (13 of 13), Read 48 times
Conf: VEDIT Macro Language Support
From: bone fonny
Date: Monday, October 20, 2003 11:20 PM

I never replied, but thanks !

I managed to do exactly what i
needed, and more, thanks to your help :)