Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-21
14
Medium Priority
?
420 Views
Last Modified: 2013-12-26
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?
0
Comment
Question by:thereds
  • 7
  • 6
14 Comments
 
LVL 1

Expert Comment

by:Hermetic
ID: 8184490
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
 
LVL 5

Expert Comment

by:buasuwan
ID: 8185673
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
 

Author Comment

by:thereds
ID: 8186050
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:thereds
ID: 8186056
Edit on Comment:

Ignore the 'TAG' on the last hex
0
 
LVL 5

Expert Comment

by:buasuwan
ID: 8186066
that mean '~h00' and char(0) are equal to  ""
0
 
LVL 5

Accepted Solution

by:
buasuwan earned 300 total points
ID: 8186803
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
 

Author Comment

by:thereds
ID: 8189312
Thanks, buasuwan.
0
 

Author Comment

by:thereds
ID: 8202422
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
 
LVL 5

Expert Comment

by:buasuwan
ID: 8204795
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
 

Author Comment

by:thereds
ID: 8205295
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
 
LVL 5

Expert Comment

by:buasuwan
ID: 8208816
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
 
LVL 5

Expert Comment

by:buasuwan
ID: 8208830
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
 

Author Comment

by:thereds
ID: 8209120
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
 
LVL 5

Expert Comment

by:buasuwan
ID: 8209280
i tested your scripts.
here is what stored in lb_blob.

lb_blob = '5377656574204D616D610042FF'

is it right?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question