Recent

Author Topic: Convert String to Octet  (Read 3658 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Convert String to Octet
« on: January 19, 2022, 06:57:01 pm »
Platform: Windows Server 2016 - latest updates.  Lazarus 2.0.10,  FPC 3.2.0, Firebird 3x

ADDED THE LINE BELOW TO THE DESCRIPTION OF THE PROBLEM-BEGIN
The Account ID is a GUID, not a number stored as a string.  I need to ensure the customer didn't just make up a GUID like, GUID looking string of characters, that is not actually convertable to a GUID.  A real GUID is convertible to OCTET form.
ADDED THE LINE ABOVE TO THE DESCRIPTION OF THE PROBLEM-END


I have a "customer" table with an OCTET field as the unique identifier.  (CHAR(16) Character set OCTETS, Collation OCTETS.  The "customer" table also has a "Username" field that is a VARCHAR(50).  Customers can log in using either their Username (which is the normal way) or the Account ID (which is a string that converts to an OCTET, which is the "account recovery" mode if they forgot their Username.

I did this so I'd have an unlimited number of non-sequentially issued unique identifiers to assign, even larger than a BIGINT, and yet maintain a lighting fast query speed, since an OCTET is handled like an integer.

Goal:  I'm at the point where I need to validate that the Account ID string is a valid OCTET, before I try to use it in a query against the Firebird database.  Firebird will choke and error out if it cannot convert the queried string to an OCTET, so I want to ensure the string coming in the Username input from the web page is convertible to an OCTET in Pascal first.

I can't find a function to do this, only a function to do the opposite conversion  (OctStr) which is not what I want.  Here is what I have at the moment:

Code: Pascal  [Select][+][-]
  1. ...
  2. var login_id, login_id_oct, pwd, err_msg: String;
  3. begin
  4.  
  5.  
  6.   // Determine if the Login ID is an Account ID
  7.   if (Length(login_id) = 36) then
  8.     begin
  9.       if TryStrToOct(login_id, login_id_oct) then
  10.         begin
  11.           // Customer has entered their Account ID for lookup as an OCTET
  12.         end
  13.       else
  14.         begin
  15.           // Customer has entered something other than an Account ID, so treat it as an ordinary string
  16.         end;
  17.  
  18.     end;
  19.  

I need a "TryStrToOct" which doesn't seem to exist.

Any suggestions?












« Last Edit: January 19, 2022, 10:54:50 pm by RedOctober »

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Convert String to Octet
« Reply #1 on: January 19, 2022, 07:52:54 pm »
I need a "TryStrToOct" which doesn't seem to exist.
Any suggestions?
Write it!

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Convert String to Octet
« Reply #2 on: January 19, 2022, 10:49:41 pm »
I'm not sure what you mean by Octet.
If that is a number in octal notation (since you mention you need the reverse of OctStr), then you can simply use Val() on it.
Just prefix the string in question with a '&': Val('&10', V, Err) should set V to 8 and Err to 0.

And since Val() understand octal notation, so do all the TryStrToInt versions.

Bart

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: Convert String to Octet
« Reply #3 on: January 19, 2022, 10:55:36 pm »
I added the lines shown to describe the predicament better.  I shd have included that in the original post. 

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Convert String to Octet
« Reply #4 on: January 19, 2022, 10:59:03 pm »
Hi!

The confusion continues:

* Octet is sometimes used as high-brow synonym for a byte
* The Linux file rights are represented by an octal number:
    chmod 644 ./ThisFile

Winni

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Convert String to Octet
« Reply #5 on: January 19, 2022, 11:16:00 pm »
I always thought an OCTET was always 8 bits (0-255), Nibble always 4 bits. a Byte does not have to be 8 bits its hardware/architecture dependent.

So I to am confused.
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Convert String to Octet
« Reply #6 on: January 19, 2022, 11:49:14 pm »
Hi!

But the most systems where a byte had a different  length than 8 bit are dead:

* Telex
* PDP
* Univac
* IBM 1401 (my uncle worked on it)
....

ISO defines a byte with a minimum of 8 bits.

But I think in the very most cases it is used as a synonyme for 8 bits.

Winni


Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Convert String to Octet
« Reply #7 on: January 20, 2022, 12:00:18 am »
I guess he's referring to firebirds chatset OCTETS.
See https://www.firebirdsql.org/file/community/ppts/fbcon11/FbCon2011-Charsets-Heymann.pdf page 59.
Quote
Special Character Sets
● NONE: Plain octets, no character set applied. With
this character set setting, Firebird is unable to
perform conversion operations like UPPER() correctly
on anything other than the standard 26 latin letters.
● OCTETS: Same as NONE. Cannot be used as client
connection character set. Space character is #x00

Basically no encoding on the characters.
A bit like RawByteString.

And I think an OCTET here means a byte, since
Quote
OCTET_LENGTH: length of a string in bytes

The Account ID is a GUID, not a number stored as a string.  I need to ensure the customer didn't just make up a GUID like, GUID looking string of characters, that is not actually convertable to a GUID.  A real GUID is convertible to OCTET form.

So, now IIUC, the question is if the inputstring represents a proper GUID?

Bart

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: Convert String to Octet
« Reply #8 on: January 20, 2022, 12:34:20 am »
Correct Bart.  That's a better way of putting it than I did.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Convert String to Octet
« Reply #9 on: January 20, 2022, 12:59:15 am »
if its guid, sysutils has
function TryStringToGUID(const S: string; out Guid: TGUID): Boolean;
function StringToGUID(const S: string): TGUID;
function GUIDToString(const GUID: TGUID): string;
function IsEqualGUID(const guid1, guid2: TGUID): Boolean;
function GuidCase(const GUID: TGUID; const List: array of TGuid): Integer;   

would these work?
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: Convert String to Octet
« Reply #10 on: January 20, 2022, 01:09:27 am »
Bingo!  I think Josh got the right answer based on Bart's investigative powers.  I figured it would be something simple. Sorry for making you all go the "long way around".  I'm sure these will work Josh, I did not know they existed and thought I had to do a conversion to an OCTET.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Convert String to Octet
« Reply #11 on: January 20, 2022, 01:48:03 pm »
I always thought an OCTET was always 8 bits (0-255), Nibble always 4 bits. a Byte does not have to be 8 bits its hardware/architecture dependent.

...But the OP's question was phrased in a way that implied that OCTET was a data type etc. understood by a particular variant of SQL.

And I have a reasonable degree of confidence that nobody would capitalise OCTET thinking that it was an initialism (Ordered Content To Expedite Transfer?) :-)

(Even though even in this forum there are people who insist on capitalising WEB and suchlike).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018