Replace last character of hex string

Replace last character of hex string

yan_ken
Advocate Advocate
1,621 Views
10 Replies
Message 1 of 11

Replace last character of hex string

yan_ken
Advocate
Advocate

Hello,

 

How do I replace the last HEX character of a HEX string of any length with an asterisk* in my LISP code?

 

Thanks,

Ken

0 Likes
1,622 Views
10 Replies
Replies (10)
Message 2 of 11

cadffm
Consultant
Consultant
I can't understand, hex character/hex string/ asterix, please post an example

before you have Input <postyourexamplestring>
after work you like to have Output <newoutput>

Sebastian

0 Likes
Message 3 of 11

Ranjit_Singh
Advisor
Advisor

There might be many ways to do this depending on your specific needs. Top of my head

(strcat (vl-list->string (reverse (cdr (reverse (vl-string->list "your string"))))) "*")
0 Likes
Message 4 of 11

john.uhden
Mentor
Mentor

Another way is:

 

(strcat (substr str 1 (1- (strlen str))) "*")

where str is the symbol for your text value.

John F. Uhden

Message 5 of 11

Kent1Cooper
Consultant
Consultant

@cadffm wrote:
I can't understand, hex character/hex string/ asterix, please post an example ....

Just so you know....  "Hex" refers to a hexadecimal [base-16] number.  In AutoCAD, strings representing them are used for entity handles, for example in this entity data list:

Command: (entget (car (entsel)))
Select object: ((-1 . <Entity name: 1759c4e76e0>) (0 . "LINE") (330 . <Entity name: 175bf4951f0>) (5 . "1E6") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDbLine") (10 38.5837 21.8194 0.0) (11 35.0213 15.0763 0.0) (210 0.0 0.0 1.0))

 

"1E6" is the Line's handle, a hexadecimal number [=  (1 x 16 squared)  +  (14 [the E] x 16)  +  6].  Whether or not the request has anything to do with entity handles, if that's the example hex string, presumably they want to change that to "1E*".  @john.uhden's suggestion is probably the simplest way to do that.

Kent Cooper, AIA
Message 6 of 11

cadffm
Consultant
Consultant

 "presumably they want to change that to "1E*"
"Just so you know.." -
Thanks for explaining very much, I know what hex spelling/Syntax and an Asterix is, but

The request is not clearly described enough (for me) and I was not sure what the thread-opener meant.

Ranjit & John show just how the last character of a string can be changed, I've also typed on it,
But technically this has nothing to do with HEX and the solution sounds too easy for a question so formulated.
("hex String" ok, but "hex character" ? I hoped for a more exciting task ;D )
And "1E*" had make no sense for me. If handle: "Handle and the next 15 objects" would make sense, but "1E *"?

So I asked directly the only one that is surely knowing what he has and wants.
I will read continue here, thank you.

Sebastian

0 Likes
Message 7 of 11

yan_ken
Advocate
Advocate

Thanks for all the information, Folks.

 

Basically, I have a hex string,for example h=8B7EDF and I need to do a if statement if this string with the exception of the last hex character matches with any of the following hex strings

For example:

h=8B7EDA

h=8B7EDB

h=8B7EDC

h=8B7EDD

.

.

 

so I was thinking of replacing the last hex character h=8B7ED*

 

I don't know if wildcard match is possible with hex string.

 

Thanks very much

 

0 Likes
Message 8 of 11

Ranjit_Singh
Advisor
Advisor

Yes wcmatch should work. In your case if h is the variable then this

(wcmatch h "8B7ED*")

will return T for all your mentioned values of h. But note that * goes for anything (single character, or number or multiple characters /numbers. If you just want to check a single character, then use

 

(wcmatch h "8B7ED?")

 

0 Likes
Message 9 of 11

yan_ken
Advocate
Advocate

So if my hex variable is say "myhexstring" = 2H7EB

 

would the following replace the last hex character B with a *?

 

(strcat (substr myhexstring1 (1- (strlen myhexstring))) "*")

 

my variable length is not always 5 hex characters,,, could be any length... but I just want to replace the last hex character with *

 

Thanks again

0 Likes
Message 10 of 11

ВeekeeCZ
Consultant
Consultant

There are used 4 functions in the expression.

 

strcat

substr

1-

strlen

 

Please use the HELP to see what they do!!! No other functions are simpler!

0 Likes
Message 11 of 11

john.uhden
Mentor
Mentor
Hex codes are just strings, so wildcard matching is perfect.
Just be careful, because the * at the end can mean a match with any number
of following characters. Perhaps you want to match any of just the last
character, in which case use ? instead of *.

John F. Uhden

0 Likes