Hexadecimal question: How do I get a 00 hex value into a string?

I've tried ~h00 but that doesn't do anything since PB allows only ~h01 to ~hFF. I've tried inserting a decimal value to compensate for that but that doesn't do anything either.  
Basically what I want to do is insert a string and at the end of the said string I want to enter the following hex value 00 followed by 2 more bytes of hex values.

Any ideas on how to solve this problem?
theredsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HermeticCommented:
Try ~h0 .  You also might look for the Chr function (you could str = str + chr(0) ).  I am pretty sure it existed in PB5.0
0
buasuwanCommented:
char(0) means end of string, I think.

string ls_test
long      m
ls_test = "ABD"
m = len(ls_test)        //  m = 3
ls_test += string(char(0))
m = len(ls_test)        //  m = 3
ls_test += "ABD"
m = len(ls_test)        // m = 6

if you want to use char(0) then you should change your string variable to blob.

string ls_test
long      m

ls_test = "ABD"

blob  lb_test
lb_test = blob(ls_test)
m = len(lb_test)

lb_test += blob(" ")    // add Temp Space Char
m = len(lb_test)

BlobEdit(lb_test, len(lb_test), char(0))   // Change Temp Space Char to char(0)
m = len(lb_test)

lb_test += blob("ABD")
m = len(lb_test)
0
theredsAuthor Commented:
The following is the string that works correctly (manually changed to ensure it works right)
1234567890123456789012345678909999999999999999999999999999990000000000000000000000000000002222CCCCCCCCCCCCCCCCCCCCCCCCCCCC...

This is the corresponding hex for the above string:
31323334353637383930313233343536373839303132333435363738393039393939393939393939393939393939393939393939393939393939393930303030303030303030303030303030303030303030303030303030303032323232434343434343434343434343434343434343434343434343434343430001FF

Now the problem arises when I use the following PB code to create the string:
ls_string = '1234567890123456789012345678909999999999999999999999999999990000000000000000000000000000002222CCCCCCCCCCCCCCCCCCCCCCCCCCCC' + '~h00' + '~h63~hFF'

The hex generated by the above is as follows:
TAG1234567890123456789012345678909999999999999999999999999999990000000000000000000000000000002222CCCCCCCCCCCCCCCCCCCCCCCCCCCCc.

It even loses a byte (?!?)

~h0 doesn't work.
Char(0) doesn't work either.

Am I totally missing something here?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

theredsAuthor Commented:
Edit on Comment:

Ignore the 'TAG' on the last hex
0
buasuwanCommented:
that mean '~h00' and char(0) are equal to  ""
0
buasuwanCommented:
if you want to store char(0) or ~h00 to a string.
you should change your variable string to blob first.
cause of char(0) and ~h00 that mean end of string.

here is the sample codes to store 0 to variable.

string ls_string
ls_string = '1234'

blob     lb_blob
lb_blob = Blob(ls_string)    // convert to blob

// now prepare a temp character to store 0
lb_blob += Blob("A")

// store 0 to where that temp character is placed.
BlobEdit(lb_blob, len(lb_blob) , char(0))

// you can add more characters.
lb_blob += Blob('~h63~hFF')


ok, now is the sample code to get them back to hex string.


long     ll_index, ll_len

ll_len = len(lb_blob)

string     ls_result
ls_result = ""

for ll_index = 1 to ll_len
     int     li_data
     li_data = asc(string(BlobMid(lb_blob, ll_index, 1)))
     
     // just a sample code of int2hex function().
     string     ls_hex_string
     ls_hex_string = ""
     //
     int     ll_high, ll_low
     ll_high = li_data / 16
     if ll_high > 9 then
          ls_hex_string = char(ll_high + 55)
     else
          ls_hex_string = char(ll_high + 48)
     end if
     
     ll_low = mod(li_data, 16)
     if ll_low > 9 then
          ls_hex_string += string(char(ll_low + 55))
     else
          ls_hex_string += string(char(ll_low + 48))
     end if
     //
     
     ls_result += ls_hex_string
     
next

// then ls_result = '313233340063FF'

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
theredsAuthor Commented:
Thanks, buasuwan.
0
theredsAuthor Commented:
Buasuwan, once again thx for your help. I'm not sure if this is appropriate but what if you have a hex value stored in a variable? (say ls_hex = 'FF')
And you wanted to add this variable to a string in hex?

ie. ls_string = 'abc' + '~hFF'

Obviously you can't get that FF because its in a variable.
0
buasuwanCommented:
Hi,
I read your last post many times, :)
why I can't get FF?

I think my sample code above could be able to get FF from a variable. you will see the ls_result = '313233340063FF'

here is other sample code to get FF.

long     ll_index, ll_len
string   ls_string

ls_string = 'abc' + '~hFF'

int li_char

ll_len = len(ls_string)

for ll_index = 1 to ll_len
     li_char = asc(mid(ls_string, ll_index, 1))
     if li_char = 255 then // FF
          MessageBox("Hello", "FF is at position = " + string(ll_index))
     end if
next


I still don't understand all you said. please tell me more.

0
theredsAuthor Commented:
I'll try to explain more clearly.

ls_string = 'Sweet Mama'
ls_number = '66'

Hex equivalent of 66 is 42

So say I was hardcoding and the value I had to enter was 66 this is what I would do:

ls_string = ls_string + '~h42'

The hex for ls_string would look like this:

5377 6565 7420 4D61 6D61 42

Try typing the above in HexEdit and you'll see what I mean.

Now here is my question:

Since I am not hardcoding, I will always need to convert the ls_number to its hex equivalent, which I have done thru a function I created.  I've got the hex equivalent (ls_hex).  But now I don't know how to add it to ls_string

ls_string = ls_string + ls_hex is obviously not what I want

What I need is this:

ls_string = ls_string + '~hls_hex'  This won't work for obvious reasons.

Does this make sense?

0
buasuwanCommented:
ok, if I understand you well.

you have no need to convert ls_number = '66' to hex '42' before adding to ls_string.

string ls_string, ls_number
ls_string = 'Sweet Mama'
ls_number = '66'
ls_string = ls_string + char( integer(ls_number) )

or if your value 66 is an integer

string ls_string
integer li_number
ls_string = 'Sweet Mama'
li_number = 66
ls_string = ls_string + char( li_number )  


above codes will not work in case of ls_number > '00'


here is for all 0 - 255 , 00 - FF
like i said, you need to convert ls_string to blob
before adding any values.

string ls_string
integer li_number

ls_string = 'Sweet Mama'
li_number = 66

blob     lb_blob
lb_blob = Blob(ls_string)    // convert to blob

if li_number > 0 then
     lb_blob += Blob(char(li_number))
else
     // now prepare a temp character to store 0
     lb_blob += Blob("A")
     // store 0 to where that temp character is placed.
     BlobEdit(lb_blob, len(lb_blob) , char(0))
end if


0
buasuwanCommented:
correct a line,
above codes will not work in case of ls_number > '00'

changes to

above codes will not work in case of ls_number = '00'
0
theredsAuthor Commented:
Alright, I understand that I have no need to convert my li_number to a hex before inserting it to a string.  Here is what I have so far:

ls_string = 'Sweet Mama'
ls_number = '66'

Before I add ls_number to the string, I need to add a ~h00.

So, I first change to blob:

lb_blob = Blob(ls_string)
lb_blob += Blob("A")
BlobEdit(lb_blob, len(lb_blob) , '~h00')

Now after I have done this, I need to add the ls_number and close with a ~hFF:

lb_blob += Blob(Char(Integer(ls_number)) + '~hFF')

Does that look right?
0
buasuwanCommented:
i tested your scripts.
here is what stored in lb_blob.

lb_blob = '5377656574204D616D610042FF'

is it right?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.